How to use a SqlDataSource to bind your data



How to use a SqlDataSource to bind your data A more efficent method of binding data to your DataGrid, DataList, DropDownList etc is to use a SqlDataSource. It takes a lot less code and time than the previous methods.

Firstly, you need to create a ConnectionString in your web.config file.

Below is and example of this:
<configuration>
<connectionStrings>
<add name="ConnectionString1" connectionString="Data Source=servername;Initial Catalog=databasename;Persist Security Info=True;User Id=username;Password=password;" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

Notice the name of the ConnectionString is 'ConnectionString1', you'll need to remember what you call it.

Now you can start to use this ConnectionString. Below is an example of a SqlDataSource using this ConnectionString:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" SelectCommand="SELECT * FROM DATABASETABLENAME ORDER BY DATABASEFIELDNAME" />

To see how to use a SqlDataSource with a DataGrid, click here


Send a link to this page via email Send a link to this page via email