# Insert data

## Insert data&#x20;

Let us add the below function to the program class in order to insert data

```clike
static void WriteData()
{
    try
    {
        using (SqlConnection connection = new SqlConnection(ConString))
        {
            var cmd = new SqlCommand("insert into Student values (105, 'Ramesh', 'Ramesh@dotnettutorial.net', '1122334455')", connection);
            connection.Open();
            
            var rowsAffected = cmd.ExecuteNonQuery();
            Console.WriteLine("Inserted Rows = " + rowsAffected);
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("OOPs, something went wrong.\n" + e);
    }
}
```

## Call the function&#x20;

We can then call the above function from the main method, the modified code structure must look as shown below:

<figure><img src="https://3784981401-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2cTTE1y1RLsV9w1miIRi%2Fuploads%2FMnM0IdHYz2gsANaKz8Vv%2Fimage.png?alt=media&#x26;token=d6e8231a-8c17-49ab-ab4d-aaddb25f2bac" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Can you try update and delete operations similarly?
{% endhint %}
