Search code examples
c#asp.netsqldatareaderdatareader

Access a specific row in DataReader


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?


Solution

  • How about...

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