Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to populate GridView using SQL Data Source in ASP.NET

Author: Alanna Kremer

This example will illustrate that how to populate a GridView Control using SQL Data Source.

This article will be very helpful for users that want to learn GridView Control with SQL Data Source.

Let’s start our example.

  1. Open Visual Studio 2010
  2. File > New > Web Site
  3. Visual C# or Visual Basic > ASP.NET Empty Web Site > Click Ok
  4. Website > Add New Item > Web Form > Click Add
  5. Now Drag and Drop a “GridView” in Default aspx page from Data tab in Toolbox.
  6. Click on small arrow at the top right corner of the GridView
  7. Choose Data Source and Select New Data Source
  8. Select Database and Click Ok
  9. Select New Connection and provide detail for Server Name and Database. Your Server will be different and you can select other database. You can also attach database file. I have used Northwind database for this example. You have to download Northwind database and attach file to use for this example. Click Ok to proceed
  10. Save the Connection string and Click Next
  11. Now you have to configure the select statement. Check only asterisk (*) means you have selected all columns. If you want to select specific columns then you have to check those check boxes. You can also set “WHERE” and ORDER BY Clause here.
  12. Click Next and then Click Finish.
  13. You can also enable Paging, Sorting and Selection. Check the option you want to use.
  14. Now you can see code below in Default.aspx page. you can also copy and paste code below in your aspx file

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="ProductID" DataSourceID="SqlDataSource1" AllowPaging="True"
                <asp:CommandField ShowSelectButton="True" />
                <asp:BoundField DataField="ProductID" HeaderText="ProductID"
                    InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
                <asp:BoundField DataField="ProductName" HeaderText="ProductName"
                    SortExpression="ProductName" />
                <asp:BoundField DataField="SupplierID" HeaderText="SupplierID"
                    SortExpression="SupplierID" />
                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID"
                    SortExpression="CategoryID" />
                <asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit"
                    SortExpression="QuantityPerUnit" />
                <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
                    SortExpression="UnitPrice" />
                <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock"
                    SortExpression="UnitsInStock" />
                <asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder"
                    SortExpression="UnitsOnOrder" />
                <asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel"
                    SortExpression="ReorderLevel" />
                <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued"
                    SortExpression="Discontinued" />
                <asp:BoundField DataField="CategoryName" HeaderText="CategoryName"
                    SortExpression="CategoryName" />
            <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:C:\SQL SERVER 2000 SAMPLE DATABASES\NORTHWND.MDFConnectionString %>"
                SelectCommand="SELECT * FROM [Alphabetical list of products]">
  15.  Now press F5 and see the page in browser 


Add Article Comment:
Name :
Email Address :
Comments :
<< Partial Page Caching in ASP.NET

Disclaimer - Privacy
© 2002-2017