Access a specific row in DataReader

35,946

Solution 1

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();

Solution 2

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.

Solution 3

If I got you correctly, you can just count reader reads like this. Then when the count suits you, you can add something different:

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

But if you want to have current and subsequent read at the same time you should firs read onceand then start reading in a loop like this:

if (dReader.HasRows) {
    string previousRead = string.Empty;
    dReader.Read();
    previousRead = dReader["gameweekID"].ToString();
    while (dReader.Read()) {
          //use current and previous read 
          //dReader["gameweekID"].ToString()
          //previousRead
          //update previousRead for the next read
          previousRead = dReader["gameweekID"].ToString();
    }
} else {
    gameweekList.Text = "Error Finding Gameweeks";
}
dReader.Close();
conn.Close();

Solution 4

To get the first row, start from 0 and always put it into a Data Tables because you always get problems reading directly from Data Readers;

if (dReader.HasRows) {

DataTable dt = new DataTable();
dt.Load(dreader)
gameweekList.Text = dt.Rows[0]["gameweekID"]

}
Share:
35,946
JackofAll
Author by

JackofAll

Updated on June 01, 2020

Comments

  • JackofAll
    JackofAll almost 4 years

    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?