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.
Create a WebApplication by right click on the solution and choose Webform and give a name for that as SqlDataSource.aspx
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.
Choose SqlDataSource control and then choose smart tag of that and then choose Configure Data Source option, as shown in below.
A new window will open while click the configuration option.
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.
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.
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.
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.
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.
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.
Now, SqlDataSource connection string is created successfully. Now, drag & drop a GridView Control in your design page.
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.
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:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
<asp:BoundField DataField="Emp_Name" HeaderText="Emp_Name"
<asp:BoundField DataField="Emp_job" HeaderText="Emp_job"
<asp:BoundField DataField="Emp_Dep" HeaderText="Emp_Dep"
Now, you can able to see the result in output page just press F5 button in your keyboard and then see the result.
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.
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.