Using SqlDataSource Control in ASP.net


In this article I'm trying to explain how to work with SqlDataSource control to bind database data to our DataControls like Gridview, DataList etc.. During this process we can able to learn how to configure SqlDataSource control and how to connect with DataControl. This is very simple to communicate with our Database.

Using SqlDataSource Control in ASP.net:



In this article I'm trying to explain how to work with SqlDataSource control to bind database data to our DataControls like Gridview, DataList etc..

Follow below steps to achieve your goal.

Step-1:



Create a WebApplication by right click on the solution and choose Webform and give a name for that as SqlDataSource.aspx

1

Step-2:



Simply Drag & Drop SqlDataSource control available under Data Controls tab just open that and double click on SqlDataSource control then it's automatically displayed in Design view.

2

Step-3:



Choose SqlDataSource control and then choose smart tag of that and then choose Configure Data Source option, as shown in below.

3

A new window will open while click the configuration option.

Step-4:



If connection already created then choose that while click on the dropdown available in the below screen shot. If connections are not available then choose New Connection option to create connection purpose.

4

Step5:



When you click the New Connection a new window will appear for adding connections purpose, then choose Server name and Database name then click on Test Connection like below. If connection has been created successfully then click on OK button then connection is available in dropdown under configure DataSource tab. See the below screen shot for reference purpose.

5

Step-6:



Now, choose the available connection string and then click on Next Option available in the above attached screen shot. Then it's redirect to save the connection string to the application tab then click on Next. Refer below screen shot for your reference.

6

Step-7:



After click on Next button a new window will appear for configure select statements, you just choose “Specify columns from a table or view" radio button then choose table name and select the columns. In the below example I just choose Employee table and then I just choose Employee name, job , Department columns available in the below screen.

7

Step-8:



If we want to write complex queries then we can able to use Where clause and order by clause available in the below screen. If you want to wrote some Advanced logic then choose Advance option and give relations automatically the query will displayed in the bottom Select Statement just click on Next button available in the below screen.

8

Step-9:



A new window will appear for test the query purpose you just click Test Query button available in the below screen the result will displayed in that screen itself then click on Finish button.

9

Step-10:



Now, SqlDataSource connection string is created successfully. Now, drag & drop a GridView Control in your design page.

10

Step-11:



Click the smart tag of Gridview control and then choose “Choose Data Source" option available in the below screen shot then choose the Data Source and then see the Design of the Gridview control.

11

Step-12:



Just open the source code of the page then see the code for Gridview control.


<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [Emp_Name], [Emp_job], [Emp_Dep] FROM [tblEmployee]">
</asp:SqlDataSource>




<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

DataSourceID="SqlDataSource1"

>
<Columns>
<asp:BoundField DataField="Emp_Name" HeaderText="Emp_Name"
SortExpression="Emp_Name" />
<asp:BoundField DataField="Emp_job" HeaderText="Emp_job"
SortExpression="Emp_job" />
<asp:BoundField DataField="Emp_Dep" HeaderText="Emp_Dep"
SortExpression="Emp_Dep" />
</Columns>
</asp:GridView>


Step-13:



Now, you can able to see the result in output page just press F5 button in your keyboard and then see the result.

12

Conclusion:



I hope this article will help you those who are looking for the same and those who are new to configure SqlDataSource in your applications. Refer this article and learn how to configure SqlDataSource.

About Author:



This is Naveen, I completed my MCA and I have around 3 years of experience in ASP.net while implement Web Applications. For past 3 years I learn no.of Technologies while implement my applications apart from that I try to learn new technologies and I try to implement that while implement my project.


Article by naveensanagasetti
I hope you enjoyed to read my article, If you have any queries out of this then please post your comments.

Follow naveensanagasetti or read 139 articles authored by naveensanagasetti

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: