ASP.NET: Configure or Querying SqlDataSource to database for CRUD with DataControls.

Aman Sharma
In this article I will explain how to use SqlDataSource data control as a datasource to different data controls: Gridview, form view, details View etc.

I will explain how to Configure SqlDataSource i.e. connect to database and Create different Commands such as Select Command, insert Command, update and Delete Command.

With  SqlDataSource we can select, insert delete and update data using different data Controls.

Configure SqlDataSource:

1.      Add SQLDataSource on the webform after data control (gridview, formview or details view) by drag and drop.

<asp:SqlDataSource ID="SqlDataSource2" runat="server"></asp:SqlDataSource>

 In Design section:

2.      Click Configure Data Source: following page will be displayed

3.      Click New Connection button and Create connection with Database:

4.      Now Press Next  button after creating connection

5.      Save Connection string Name to Configuration File and Press ok:

6.      Configure the Select Statement. There will be two options, select first option if you want to create custom SQL Statement for Select, insert, Update and delete. Then Press Next:

7.      In Next Page Define custom statements for different tasks. We can create query manually or using Query builder. 
Query For Update:

8.      Query for Insert:

9.      Query For Delete:

10.  Query for Select:

11.   After creating all queries press Next Button. Next button will be enabled only after creating all queries.
12.  Test query. This will be the last step. If you want to test query then you can test here. Then press Finish to complete the configuration.

After configuration step, SqlDataSource Control in source section will be look like:

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:BlogConnectionString3 %>" DeleteCommand="DELETE FROM Student_info WHERE (Student_Id = @Studentid)" InsertCommand="INSERT INTO Student_info(Student_name, Age, Class) VALUES (@name, @age, @class)" SelectCommand="SELECT Student_name, Age, Class FROM Student_info" UpdateCommand="UPDATE Student_info SET Student_name = @name, Age = @age, Class = @class WHERE (Student_Id = @studentid)">
        <asp:Parameter Name="Studentid" />
        <asp:Parameter Name="name" />
        <asp:Parameter Name="age" />
        <asp:Parameter Name="class" />
        <asp:Parameter Name="name" />
        <asp:Parameter Name="age" />
        <asp:Parameter Name="class" />
        <asp:Parameter Name="studentid" />

In this article, I have learned how to Configure SqlDataSource control for select, insert, delete and update through DataControls using SqlDataSource.  If you have any doubt you can ask by leaving your query in comment section.

Post a Comment

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !