ASP.Net Gridview Databinding Using Sql DataSource

Updated on 26 Jul 2012,
Published on 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.

Live Sample:

You can see the live samples and examples of GridView Databinding using SqlDataSource Control from the following link:

ASP.Net GridView SqlDataSource DataBinding

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 copy-paste the above code and reset the user id and password according to your SQL Server Authentication.

Output

Get the output for above discussed code from the following link:

ASP.Net GridView SqlDataSource DataBinding

Continue to next tutorial: ASP.Net Gridview Dynamic to learn how to load a gridview programmatically.

0 Responses to "ASP.Net Gridview Databinding Using Sql DataSource"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers