0

I have the code

while (reader.Read())
{
    if (reader[incrementer]!=DBNull.Value){
        string playerToInform = reader.GetString(incrementer).ToString();
        string informClientMessage = "ULG=" + clientIP + ","; //User Left Game
        byte[] informClientsMessage = new byte[informClientMessage.Length];
        informClientsMessage = Encoding.ASCII.GetBytes(informClientMessage);
        playerEndPoint = new IPEndPoint(IPAddress.Parse(playerToInform), 8001);
        clientSocket.SendTo(informClientsMessage, playerEndPoint);
    }
    incrementer++;
}

which after debugging my code i see contains 4 entries. However only the first result is ever read from the reader. After the first iteration to find if the result returned is null or not the loop starts again and immediately finishes even though there are three more rows to read.

Any ideas as to why this may be occuring would be apprechiated.

edit - this is the reader i used

OleDbDataReader reader = dBConn.DataSelect("SELECT player1_IP, player2_IP, player3_IP, player4_IP FROM running_games WHERE game_name = '" + gameName + "'", updateGameList);
Richard
  • 106,783
  • 21
  • 203
  • 265
JOsh
  • 169
  • 2
  • 4
  • 15
  • What is `reader`? A StreamReader/DataReader or something else? What does your data look like? – Ian Apr 07 '16 at 12:19
  • Do `reader[incrementer]` or `reader.GetString(incrementer)` progress the reader by any chance? – Patrick Bell Apr 07 '16 at 12:19
  • That is what i am thinking, but i cant see any way around this. – JOsh Apr 07 '16 at 12:20
  • I know that i could simply check all 4 results using if statements but i know that this would not be as efficient. – JOsh Apr 07 '16 at 12:21
  • Check the status of the reader (position) after those calls, and see if they are progressing it. If they are, you could make one call to get all of the data you need from row n, then reuse that data for wherever it is applicable, and proceed to row n+1. – Patrick Bell Apr 07 '16 at 12:23
  • 3
    Do you realize on each Read() you use a different field? reader[index] does not refer to the row but to the field, if you whant to check the four fields on each row you must add a for surrounding the if instead of incrementing the incrementer on each row. – Gusman Apr 07 '16 at 12:24
  • Thanks, your right, i will do that know. – JOsh Apr 07 '16 at 12:24
  • @Gusman Thankyou that worked, if you post that as an answer i will mark it as one. – JOsh Apr 07 '16 at 12:35

3 Answers3

2

The indexer of DbDataReader (DataReader is something else) or a database specific subclass, returns the value of the specified (by index or name).

While DbDataReader.Read() moves to the next row.

If you want to apply the same logic to multiple columns you need to loop over the columns, and the rows:

while (db.Read()) {

  for (var colIdx = 0; colIdx < columnCount. ++colIdx) {
    if (!db.IsDbNll(colIdx)) {
      string value = db.GetString(colIdx);
      // Process value
    }
  }

}
Richard
  • 106,783
  • 21
  • 203
  • 265
0

Incrementer is unnecessary. reader.Read() advances to next record and returns false if there are no more rows.

Check documentation on msdn

Nino
  • 6,931
  • 2
  • 27
  • 42
  • the name incrementer is just a badly named variable, it is actually supposed to be to get data from each column not increment to the next row. – JOsh Apr 07 '16 at 12:26
0

You're incrementing "incrementer" as if that was the row number, but a DataReader holds only one row per Read() and the indexing is for the field number.

Use this:

while (reader.Read())
{
    for(int colNum = 0; colNum < 4; colNum++)
    {
        if (reader[colNum]!=DBNull.Value)
        {
            string playerToInform = reader.GetString(colNum).ToString();
            string informClientMessage = "ULG=" + clientIP + ","; //User Left Game
            byte[] informClientsMessage = new byte[informClientMessage.Length];
            informClientsMessage = Encoding.ASCII.GetBytes(informClientMessage);
            playerEndPoint = new IPEndPoint(IPAddress.Parse(playerToInform), 8001);
            clientSocket.SendTo(informClientsMessage, playerEndPoint);
        }
    }
}
Gusman
  • 14,905
  • 2
  • 34
  • 50