How to bind gridview using SqlDataAdapter, DataTable and Stored procedure in Sql server

In this article i have tried to explain how to bind GridView using SqlDataAdapter, DataTable and Stored procedure in Sql server in

Implementation: Let's create a demo web application to see it in action.
  • Create a Database e.g. "MyDataBase" and a table under that DataBase in Sql Server and name it "EMPLOYEE" as  shown in figure:

Binding gridview example in

Note: EMP_ID column is set to Primary key and Identity specification is set to yes with Identity increment and Identity seed equal to 1. Insert some data in this table that you  want to show in the Gridview.
  • Create a stored procedure in sql server as:
CREATE PROCEDURE GET_EMP_DATA                       
                SELECT * FROM EMPLOYEE                
  • Now in web.config file add the connectionstring under <configuration> tag :
    <add name="EmpCon" connectionString="Data Source=LALIT;Initial Catalog=MyDataBase;Integrated Security=True"/>

  • Add a GridView control in design page of your website under <BODY> tag
<fieldset style="width:230px;">
            <legend>Gridview Binding Example</legend>
            <asp:GridView ID="EmpGridView" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">    
         <AlternatingRowStyle BackColor="White" />
        <asp:BoundField DataField="EMP_NAME"  HeaderText="Name" />
        <asp:BoundField DataField="DEPT"  HeaderText="Department" />
        <asp:BoundField DataField="SALARY"  HeaderText="Salary" />  
         <EditRowStyle BackColor="#2461BF" />
         <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
         <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
         <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
         <RowStyle BackColor="#EFF3FB" />
         <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
         <SortedAscendingCellStyle BackColor="#F5F7FB" />
         <SortedAscendingHeaderStyle BackColor="#6D95E1" />
         <SortedDescendingCellStyle BackColor="#E9EBEF" />
         <SortedDescendingHeaderStyle BackColor="#4870BE" />
  • In the code behind file(.aspx.cs)  of your website write the code as 
Asp.Net C# Code

First include the following namespaces
using System.Data;
using System.Data.SqlClient;
using System.Configuration; 
then write code as:

   protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)

    private void BindEmpGrid()
        DataTable dt = new DataTable();
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EmpCon"].ConnectionString);
            SqlDataAdapter adp = new SqlDataAdapter("GET_EMP_DATA", con);
            adp.SelectCommand.CommandType = CommandType.StoredProcedure;

            if (dt.Rows.Count > 0)
                EmpGridView.DataSource = dt;
        catch(Exception ex)
            Response.Write("Error Occured: " + ex.ToString());

In the code behind file(.aspx.vb)  of your website write the code as

Asp.Net VB Code

First import the following namespaces

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration 
then write code as:

Protected Sub Page_Load(sender As Object, e As EventArgs)
                    If Not IsPostBack Then
                    End If 
End Sub

Private Sub BindEmpGrid()
                    Dim dt As New DataTable()
                                         Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("EmpCon").ConnectionString)

                                         Dim adp As New SqlDataAdapter("GET_EMP_DATA", con)
                                         adp.SelectCommand.CommandType = CommandType.StoredProcedure

                                         If dt.Rows.Count > 0 Then
                                                             EmpGridView.DataSource = dt
                                                             EmpGridView.DataSource = Nothing
                                         End If
                    Catch ex As Exception
                                         Response.Write("Error Occured: " & ex.ToString())
                    End Try
End Sub

Now over to you:
" I hope you have got How to bind Gridview and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..