ASP.Net Gridview Databinding Using Sql DataSource

by top54u.com 19 Feb, 2008

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">

</asp:GridView>

 

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:

ConnectionString = "<%$ ConnectionStrings:NorthwindConnectionString %>"

 

In the web.config file you can add the following connection string:

 

<connectionStrings>

<add name="NorthwindConnectionString" connectionString="Data Source=.;Initial Catalog=Northwind;User ID=sa"

providerName="System.Data.SqlClient" />

</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.

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

10/11/2008 3:34:14 AM




related videos.....
recent posts.....
top54u ezines.....