0

I have this statement:

if (sqlClass.Reader != null && sqlClass.Reader.HasRows)
    {
        do
        {
            data = sqlClass.Reader.GetString(0); //error line System.InvalidOperationException {"Invalid attempt to read when no data is present."}
        } while (sqlClass.Reader.Read());
    }

the object sqlClass.Reader is of type System.Data.SqlClient.SqlDataReader

In C# it gives me an InvalidOperationException but in VB it worked fine, what would the reason be and how do I solve this?

1
  • 1
    show the entire error and the line of error too. Commented May 23, 2014 at 10:21

1 Answer 1

4

Something like this:

if (null != sqlClass.Reader)
{
    while (sqlClass.Reader.Read())
    {
        data = sqlClass.Reader.GetString(0);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

damn you beat me to it. I was going to write just that. its a matter of using a different type of while loop as you demonstrated
You'd rather had preserved qlClass.Reader != null
sqlClass.Reader.HasRows if of no use here
HasRows checks if the reader has retrieved data. The null check will just check if the object is not null.

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.