DataGrid does not display?

  • Thread starter Thread starter Mervin Williams
  • Start date Start date
M

Mervin Williams

I placed a datagrid on my webform, set the datasource to a datareader, and
bound the reader to the datagrid. However, my datagrid does not display.
Here is my code from the Page_Load method:

====================================
dgAgmtList = new DataGrid();
SqlConnection con;
string sql;
SqlCommand cmd;
SqlDataReader reader;

sql = "SELECT * FROM [User] WHERE user_id='" + Page.User.Identity.Name +
"'";
con = new
SqlConnection(ConfigurationSettings.AppSettings["OFS.ConnectionString"]);
cmd = new SqlCommand(sql, con);
con.Open();
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

int rowcount = 0;
if (reader.HasRows)
{
while(reader.Read())
{ rowcount++; }
}

if (rowcount == 0)
{
reader.Close();
Server.Transfer("nextpage.aspx");
}
else
{
dgAgmtList.DataSource = reader;
dgAgmtList.DataBind();
reader.Close();
}
 
Hello Mervin,

IDataReader is a forward only method of working through your rows.

In your if (reader.HasRows), you are iterating through the rows. To reset the reader, you will need to do another cmd.ExecuteReader prior to doing the DataBind().
 
Set the datagrid.datasource =
cmd.ExecuteReader(CommandBehavior.CloseConnection);
 
Back
Top