0

I'm using a SqlCeDataReader to read results from a query and add them to a list of objects which may return 0 or many rows.

My code:

while (tourReader.Read())
{
    Tour newTour = TourDBA.RetrieveTour( (int)reader["Id"] );
    if (newTour != null)
        creater.Tours.Add(newTour); 
}

I would ASSUME that since the Select query should return 0 rows, Read() would return false and thus the loop would never be entered. (I know what's in the table for this test instance, and 0 rows is the expected behavior this test.)

Any idea how to get around this? (Using .HasRows also throws an exception.)

One other thing - the connection I'm using in this query is open, and was used in a different SqCeCommand other than the one being executed here before the method comes to this while loop. If that matters...

Logan Black
  • 567
  • 3
  • 10
  • 21
  • Is `tourReader` not `null` for sure? What exception do you get? – horgh Nov 29 '12 at 04:29
  • Yes, the tourReader is set by executing a SqlCeCommand which I know shouldn't return any rows. (though there are cases where it will.) The error is: "No data exists for the row/column", and it gets thrown in the first line of the while Statement, (before entering the RetrieveTour method. It gets thrown right after I press 'step in', so its the ........ reader["Id"] which is not tourReade["Id"]... theres the poblem. Shit. – Logan Black Nov 29 '12 at 05:10
  • The last word in your comment is definetely not in the format of this site – horgh Nov 29 '12 at 05:12

1 Answers1

0

Check it:

while (tourReader.Read()) // tourReader here
{
    Tour newTour = TourDBA.RetrieveTour( (int)reader["Id"] ); // reader here
    if (newTour != null)
        creater.Tours.Add(newTour); 
}

Are reader and tourReader the same?

horgh
  • 17,918
  • 22
  • 68
  • 123