I am having problems with this grid view. I am populating it with a query. However, it will not populate or even appear if I use a while(reader.Read()) structure. Without the
The SqlDataReader is forward-only. When you first iterate over the rows, there is "nothing left" in it to display afterwards.
I suggest that you use the reader to populate a strongly-typed list in memory, and then bind the GridView to the list instead. Example:
var myList = new List();
while (myReader.Read())
{
myList.Add(new TicketInfo
{
TicketCost = Convert.ToDecimal(myReader["TicketCost"]),
NumTickets = Convert.ToInt32(myReader["NumTickets"])
});
}
grdEvents.DataSource = myList;
grdEvents.DataBind();
The code example above assumes that you have a class called TicketInfo
defined as:
class TicketInfo
{
public decimal TicketCost { get; set; }
public int NumTickets { get; set; }
}
If you haven't used generics (such as List
in the example) before, I suggest you do some reading on the subject.