This tutorial will show you how to use C# and AJAX to create a Data Access Component that will display data from a SQL database and also allow you to delete records from the database.
In addition to creating our own Data Access Component to read data from our SQL database, we can also add a method to delete records from the database. In this tutorial, you will learn how to create a Data Access Component that will both read and display data from a SQL database and that also has the ability to delete records as well.

In this example, we will be using a SQL database with one table that has three columns – id, name and age. We will write a Data Access Component to access this data and use a GridView to display it. We will also add the functionality to delete records from the database. Finally, we will be adding AJAX to increase the usability of the system.

Once we have our database set up, we will begin to write the class. To read data from the database, we will need something like this:

This method uses SQL statements to delete database records with the id variable passed to it from whatever calls it. In this example, the ObjectDataSource will be calling it, which we will get to a little later.

So the entire code-behind for the class looks something like this:

This concludes our writing of the class. Now we just need to implement it into the ASPX page.
To do this, all we will do is include two controls: GridView and ObjectDataSource. By specifying a few attributes of these controls, we will unlock the functionality of the class we just created. The ASPX page will look something like this:

As a final touch, we will implement AJAX to create a desktop feel to the application. By adding a ScriptManager and an UpdatePanel control, we will hijack the postback and reload just the GridView and DataSource. The ASPX page will now look something like this:

Download Source Files