This tutorial will show you how to use LINQ in Visual Studio.NET 2008 and C# to add data to a SQL database. With the addition of LINQ to the ASP.NET 3.5 Framework, we have a new way to approach data maniupulation. We will be looking at how we can use LINQ not only to display data from a SQL Server Database, but also how to add new records to the database. We are going to create a sample database and use a DataGrid to display the data with a LinqDataSource Control and LINQ to SQL Classes. We will also add TextBoxes and a Button to enable us to add new records to the database, and then tidy it up with some AJAX to create a better user experience.

This example was created with Visual Studio .NET 2008. It can be recreated in 2005 with the LINQ Preview downloaded from Microsoft, but this tutorial is directed at Visual Studio .NET 2008.

The first thing that we will do is to create the SQL Server Database. We will be using one table with three columns – id, name, position. If you have your own database that you want to use, skip to the next step. To add a new database, right-click on your project in Solution Explorer and then choose Add New Item > SQL Server Database. Once we have designed the table, we will add some sample content – just a few records.

Once the database has been designed and is saved, with some sample records, we will need to add the LINQ to SQL Classes. We do this in the same way as we added the database, but instead, choose LINQ to SQL Classes. This will open up a design view, in which we will need to drag the tables we will be working with. The LINQ to SQL Classes will be created and will represent the database structure. We will use these classes to interact with the database. Visual Studio will create the classes for us, so there is minimal coding for us to do. Once you have dragged onto the design area all the tables you need, make sure you save it.

Now we can start building our Web Application. We will add a LinqDataSource to our ASPX page from the Data Toolbox. It will look something like this:

To configure the Data Source, click on the Smart Tag in design view and choose Configure Data Source. This will bring up a window with all of our Data Context objects. We choose the one that we just created, and click Next. Then we choose the columns and tables we wish to work with and click Finish. You will now see on the Smart Tag that there are more options – we can choose to Enable Delete, Insert and/or Update. We will need to enable Insert to allow us to add new records.
Next, we will add a DataGrid Control to display the records. Then we can click on its Smart Tag and set its Data Source to the one we just created. The ASPX page will now look something like this:

This page will now display the records in the DataGrid if we run it, but we want more functionality than that. We will add two text boxes and a submit button to allow additions to the database. The ASPX page will then look like this:

Notice that the button has the onclick event handler. In the code-behind, we will add the following code to this event:

This code block is using LINQ to create an instance of the DataClass we created earlier, and then we are instantiating a new tblEmployee from the class, then we simply assign the values of the textboxes to the attributes of the instance we just created. Finally, we call the InsertOnSubmit and SubmitChanges methods to commit changes back to the database, and then bind the datagrid again.

Now if we run the application, we will be allowed to make additions to the database. But we can extend it further by using validation and AJAX. To make the addition process more dynamic, we can add a ScriptManager and an UpdatePanel like so:

This will make the DataGrid update without the whole page reloading when we add a new entry to the database. To add some simple validation, we can modify the code-behind like so:

Now the data will only be added to the database if the textboxes are not blank.

The entire code-behind will look something like this:

Download Source Files