Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Monday, 2 February 2015

Introduction:

In this article  I will explain how to bind GridView using SqlDataAdapter, DataTable and inline query in Asp.net. 

Implementation:

Create a Database e.g. "TEST" and a table under that DataBase in Sql Server and name it "EMPLOYEE" .







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.

Now in web.config file add the  connectionstring under <configuration> tag :
<connectionStrings>
    <add name="EmpCon" connectionString="Data Source=LocalServer;Initial Catalog=MyDataBase;IntegratedSecurity=True"/>
  </connectionStrings>



Add a GridView control in design page of your asp.net website under <BODY> tag
<fieldset style="width:230px;">
            <legend>Bind Gridview With Database</legend>
            <asp:GridView ID="EmpGridView" runat="server"AutoGenerateColumns="False"
                CellPadding="4" ForeColor="Black" BackColor="#CCCCCC"BorderColor="#999999"
                BorderStyle="Solid" BorderWidth="3px" CellSpacing="2"Width="223px">  
       <Columns>
        <asp:BoundField DataField="empname"  HeaderText="Name" />
        <asp:BoundField DataField="salary"  HeaderText="Salary" />
      </Columns>
         <FooterStyle BackColor="#CCCCCC" />
         <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
         <PagerStyle BackColor="#CCCCCC" ForeColor="Black"HorizontalAlign="Left" />
         <RowStyle BackColor="White" />
         <SelectedRowStyle BackColor="#000099" Font-Bold="True"ForeColor="White" />
         <SortedAscendingCellStyle BackColor="#F1F1F1" />
         <SortedAscendingHeaderStyle BackColor="#808080" />
         <SortedDescendingCellStyle BackColor="#CAC9C9" />
         <SortedDescendingHeaderStyle BackColor="#383838" />
 </asp:GridView>
        </fieldset>


C#.Net Code to bind gridview using SqlDataAdapter, DataTable and query in Asp.net


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)
        {
            BindEmpGrid();
        }   
    }

    private void BindEmpGrid()
    {
        DataTable dt = new DataTable();
        try
        {
            SqlConnection con = new    SqlConnection(ConfigurationManager.ConnectionStrings["EmpCon"].ConnectionString);
            SqlDataAdapter adp = new SqlDataAdapter("Select * from EMPLOYEE", con);
            adp.Fill(dt);

            if (dt.Rows.Count > 0)
            {
                EmpGridView.DataSource = dt;
                EmpGridView.DataBind();
            }
            else
            {
                EmpGridView.DataSource=null;
                EmpGridView.DataBind();
            }
        }
        catch(Exception ex)
        {
            Response.Write("Error Occured: " + ex.ToString());
        }
        finally
        {
            dt.Clear();
            dt.Dispose();
        }   
    }

VB.Net Code to bind gridview using SqlDataAdapter, DataTable and query in Asp.net

 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
        BindEmpGrid()
    End If
End Sub

Private Sub BindEmpGrid()
    Dim dt As New DataTable()
    Try
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("EMpCon").ConnectionString)
        Dim adp As New SqlDataAdapter("Select * from EMPLOYEE", con)
        adp.Fill(dt)

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

End Sub

0 comments:

Post a Comment