SIGN UP MEMBER LOGIN:    
ARTICLE

How Do I Bind Datagrid with SqlDataReader in C#

Posted by Ashish Singhal Articles | How do I October 29, 2005
This article will show, how do we bind our binding controls like DataGrid, DataList using SqlDataReader.
Reader Level:

This class will request to a database class to return SqlDataReader to Bind DataGrid Control.

using System;
using
System.Data.SqlClient;
using
System.Web;
Class BindGrid : System.Web.UI.Page
{
private Returneader returnReader;
private
DataGrid dataGrid;
public
BindGrid()
{
returnReader = new ReturnReader();
BindDataGrid();
}
Private
void
BindDataGrid()
{
dataGrid.DataSource = returnReader.CreateReader();
dataGrid.DataBind();
}
}
Class ReturnReader
{
private SqlDataReader sqlDataReader = null;
public
SqlDataReader CreateReader()
{
SqlConnection con = new SqlConnection("SERVER=.;UID=Ashish;PWD=Ashish;DATABASE =NorthWind");
SqlCommand objCommand =
new
SqlCommand("SELECT FirstName, LAstName from Employees", con);
sqlDataReader = objCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
return
sqlDataReader;
}
}

Login to add your contents and source code to this article
share this article :
post comment
 

Looks like comments control is working fine now.

Posted by Mahesh Chand Nov 05, 2005

This is good article, this will help user to bind grid with datareader insted of DataSet. Example is simple and short.

Posted by Bhasker Das Nov 01, 2005
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor