0

Here is the code i came up with::

reader = cmd.ExecuteReader();
reader.Read();
if (reader.Read())
    intQ = int.Parse(reader[0].ToString());
else
    intQ = 0;

txtblck.Text = intQ.ToString();
reader.Close();

But this causes it to always execute the else, and if i do this:

reader = cmd.ExecuteReader();
if (reader.Read())
    intQ = int.Parse(reader[0].ToString());
else
    intQ = 0;

txtblck.Text = intQ.ToString();
reader.Close();

The if always return true, how should do this?

1
  • Obviously the resultset doesn't contain what you expect... What query are you using to get the data from the database? Commented Apr 30, 2011 at 7:59

4 Answers 4

9

Check the HasRows property. Perhaps this is what you're looking for (your question is quite clear to me).

if( reader.HasRows )

HasRows returns a value if the resultset contains records. Do you want to achieve that ? Or, do you want to know whether a particular field of a certain record contains a value ?

How does your SQL Statement look like ?

Sign up to request clarification or add additional context in comments.

3 Comments

i thought of that too, but i dont know why, it always return true, and because it would execute intQ = int.Parse(reader[0].ToString()); when there is no data, it always throw an exception
@dreamer Are you sure the value of your data isn't NULL? Consider using int.TryParse() rather than int.Parse().
Your answer seems good for what he actually intends to ask, but somehow couldn't clear it. However there is minor correction, HasRows is a property in SqlDataReader Class and hence can not be directly used on the object returned by the ExecuteReader() function.
5

reader.Read() advances the reader to the next record, where the reader is initially set to before the first record. If calling reader.Read() returns false this means that it was unable to advance to the next record (i.e. the current record is the last record).

This means that if you wish to read the first record you need to call reader.Read() once, and if reader.Read() returns false it means there were no records - like so:

using (var reader = cmd.ExecuteReader())
{
    if (reader.Read())
    {
        intQ = int.Parse(reader[0].ToString());
    }
    else
    {
        intQ = 0;
    }
}
txtblck.Text = intQ.ToString();

FYI int.Parse will throw an exception if the first record is null - this is different from having zero rows. Perhaps you should check for null values, or use int.TryParse instead.

9 Comments

but if i use it once, it would always return true,
@dreamer If you always have at least 1 record then yes, it will always return true.
yea, but when it execute the next line, it thrown an exception because of incorrect data
@dreamer FYI If the first record isn't an integer (for example if it is null) then the int.Parse will throw an error - perhaps you should check for null values, or use int.TryParse instead.
I can confirm that this is the correct way of checking if there is a record in the set or not. If the Read method always returns true, it means that you always have at least one record. If that record doesn't contain what you expect, you have to find out what's returned from the database in order to figure out how to determine if you should use it or not.
|
2

Checking the the MSDN documentation for SqlReader reveals that is has a property called HasRows which you can use.

if (reader.HasRows)
{
   ...
}

Comments

1

Read() gets the next row of the result set.

So if you return one row the first read gets that row and the second Read() returns false as there is no second row - so the else happens.

1 Comment

but if i use it once, it would always return true,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.