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

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
How to bind GridView and DropDownList controls using DataTable object

Author: Faraz
Download Source Code : 278_DataReaderandWebControls.zip

In this article I will try to explain you the technique of binding GridView and DropDownList control using DataTable object.

In .net framework 1.0, we have DataSet class which helps the developers in managing their data even when it is disconnected form the data source. Objects that are created using DataSet class works as a container for the the other objects that are created from the DataTable class. DataTable object creates a logical table in memory and most of the programming with the disconnected data is done using one or more DataTable objects. However, the .net framework 1.0 did not allow you to work directly with the DataTable objects. The limitation in using this object is you need to use the DataSet object to perform any operation on the DataTable. But it is not the case with .net framework 2.0. In ADO.Net 2.0 remove this limitation and allow you to work directly with the DataTable object.

 

In .net framework 2.0, you can load a DataTable in memory by consuming a data source using a DataReader. In this version you will find the flexibility in accessing the DataReader means write access code once and use again and again using DataReader. To see the functionality of DataTable consider an example that is created using VS2005.

 

To begin with, create new website in VS2005 adds the two controls; GridView control:

 

<asp:GridView ID="myGridView" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">

 

<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

 

<RowStyle BackColor="#EFF3FB" />

 

<EditRowStyle BackColor="#2461BF" />

 

<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

 

<AlternatingRowStyle BackColor="White" />

 

</asp:GridView>

 

And the second control is DropDownList control. You can add text before these controls using Label control or simple a span tag:

<asp:DropDownList ID="myDropDownList" DataValueField ="CustomerID" DataTextField="CustomerName"  runat="server" />

 

Now we see how we can handle these controls programmatically. Handling controls means how you can access your data source and fill your DataTable – logical table in memory – and display your access data in controls. To begin with this, first of all import two namespaces those are System.Data and System.Data.SqlClient. DataTable class is a member of System.Data namespace. You can create its object independently our as a member of DataSet. In this example I have created an independent DataTable object. After importing the namespaces create three global variables:

Dim DBConn As New SqlConnection("Server=YourServer;UID=YourID;PWD=XXX;Database=YourDS")

Dim DBCmd As New SqlCommand

Dim DBDR As SqlDataReader

 

On the Page_Load event add the following control:

 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

 

'Open DataBase connection

DBConn.Open()

 

If Not IsPostBack Then

 

'Get Data from Database

DBCmd = New SqlCommand("Select * from db_Customer", DBConn)

 

'Fill DataReader

DBDR = DBCmd.ExecuteReader()

 

'Create a DataTable object

Dim DT As New DataTable()

'Load Data from Data Reader to DataTable object

DT.Load(DBDR)

DBDR.Close()

 

'Fill the GridView Control

myGridView.DataSource = DT

myGridView.DataBind()

 

DBCmd = New SqlCommand("Select CustomerName from db_Customer", DBConn)

 

'Fill the DropDownList control

myDropDownList.DataSource = DT

myDropDownList.DataBind()

 

'store Nothing and close the connection

DBDR = Nothing

DBConn.Close()

 

End If

 

End Sub

Article Comments
Good but how can i bind gridview directly from database without using data reader or data table

Posted on 12/15/2006 4:32:50 AM by chirag

there is no way you can bind directly from database, you need a data-reader or data-table or data-set for binding

Posted on 12/28/2007 7:52:15 AM by garihc

very good

Posted on 1/3/2008 3:57:03 AM by balu

very nice explanation.....

Posted on 2/11/2008 3:42:37 AM by amol

GOOD

Posted on 2/28/2008 12:45:57 PM by giri

hey how can i bind the grid view using data set

Posted on 5/22/2008 6:16:53 PM by sandeep

Excelent explanation

Posted on 9/18/2008 10:11:12 AM by Gareth

Excellent boss....

Posted on 8/7/2009 4:47:49 AM by prakash

how can i store more than one value from dropdownlist to db using c sharp

Posted on 2/18/2010 5:52:24 AM by kalyan

It is excellent ........... It really help me out

Posted on 2/24/2010 5:30:18 AM by shoaib

Thanks...

Explanation is very good...

Posted on 3/3/2010 8:59:50 AM by Jignesh

DBCmd = New SqlCommand("Select * from db_Customer", DBConn)

DBCmd = New SqlCommand("Select CustomerName from db_Customer", DBConn)

Can i use the same variable name for both queries?

Posted on 5/25/2010 12:28:54 PM by preet

plz explain clearly

Posted on 6/8/2010 7:22:53 AM by sa

good bro this is nice which is help to me and solve the my problem thanks bye take care.

Posted on 6/24/2010 9:23:04 AM by firdaus mansuri

NICE BRO. THIS SOLVED MY PROBLEM

Posted on 6/28/2010 3:59:29 PM by vivek

thank youuuuuuu

Posted on 7/23/2010 10:10:53 AM by anghie

Nothing. we can directly connect to the db wt grid view wtout using of datatable .. but we need one source frm anywhere to bind the grid

Posted on 8/2/2010 10:50:27 AM by rafik

this article is too greate... thank you ...

Posted on 9/16/2010 5:54:37 AM by mukesh mahajan

this Nagarjuna this article is very good .i am saties fied with this one.

Posted on 11/9/2010 6:22:06 AM by Nagarjuna

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Using DataReader to fill a DataTable from an SQL Server Stored Procedure and display the data in a GridView control in ASP.NET 2.0

Disclaimer - Privacy
© 2002-2014 DevASP.net