In the previous article ASP.Net Gridview Databind Using Sql DataSource you learnt the steps to configure the SQL DataSource, selection of Ms SQL Server connection provider, new connection string, database connection testing and binding the ASP.Net Gridview control to that SQL DataSource.
You can also use the following ASP.Net code to bind the Gridview with SQL DataSource:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
GridView control with DataSourceID="SqlDataSource1"
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName], [QuantityPerUnit], [UnitPrice], [UnitsInStock] FROM [Alphabetical list of products]">
</asp:SqlDataSource>
SQL DataSource connected through connection string:
In the web.config file you can add the following connection string:
<add name="NorthwindConnectionString" connectionString="Data Source=.;Initial Catalog=Northwind;User ID=sa"
</connectionStrings>
Above code does not need any VB or C# code to bind the data retrieved from the Ms SQL Server. Just the copy-paste the above code and reset the user id and password according to your SQL Server Authentication.
Currently rated 4.0 by 1 people
Tags: asp.net 2.0, asp.net gridview, asp.net sql datasource, asp.net gridview databinding, asp.net examples, asp.net web.config, asp.net sql connection string, asp.net gridview data bound, asp.net database connection, asp.net sql database connection, asp.net gridview paging, asp.net gridview sorting, c#, free source code, web.config connectionstrings
10/11/2008 3:34:14 AM