Access a specific row in DataReader

JackofAll picture JackofAll · Mar 16, 2013 · Viewed 33.3k times · Source

I have a datareader to display a list of gameweeks in a js carousel.

I need to be able to add an if statement to change the div class of the current gameweek.

This is my current code:

if (dReader.HasRows) {
    while (dReader.Read()) {                
        gameweekList.Text += "<div class=\"item\"><h4>Gameweek " + 
            (dReader["gameweekID"].ToString()) + "</h4></div>";
    }
} else {
    gameweekList.Text = "Error Finding Gameweeks";
}
dReader.Close();
conn.Close();

In effect I want to add if(dreader[1]) then add the extra bit, how is this possible?

Answer

jiiri picture jiiri · Mar 16, 2013

I find it easier to use a DataTable or DataSet. Something like this:

DataTable dt = new DataTable();
dt.Load(dreader)

Then you can more easily reach a certain row using the DataTable.Rows property.