7

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?

jcubic
  • 61,973
  • 54
  • 229
  • 402
JackofAll
  • 517
  • 4
  • 13
  • 23
  • The code `if (dreaded[1])` would grab the value of the column in the `1` index. But you're trying to get at a specific row? Are you trying to get at the **next** row? – Mike Perrenoud Mar 16 '13 at 14:17
  • Just for concept I'm trying to get row 1. But that dreader[1] does not work – JackofAll Mar 16 '13 at 14:21
  • Yeah, the reason that doesn't work is because the data reader, by definition, reads and holds one row of data in memory at a time. It provides a forward only, fast, reader in which you could read millions of rows and not consume any significant memory. So, to get to the next record you're going to have to issue a `Read`. – Mike Perrenoud Mar 16 '13 at 14:24
  • Data Reader is like IEnumerable that does not support accessing elements at particular index. It can only be used to iterate over a list of items, so to reach the first row, you need to do a Read() and then apply your logic. As mentioned by Nikola. – Dinesh Mar 16 '13 at 14:35

4 Answers4

7

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.

jiiri
  • 330
  • 1
  • 12
4

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();
JamesWest
  • 66
  • 1
  • 3
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();
Nikola Davidovic
  • 8,556
  • 1
  • 27
  • 33
2

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"]

}
Eddy Jawed
  • 457
  • 6
  • 17