I want to check if the sqldatareader is null or not. So tried the following code:
if (x[k]!= DBNull.Value)
{
box.Text = Convert.ToString(x[k++]);
if ((box.Text) != "")
current[j - 1] = Convert.ToDouble(box.Text);
else current[j - 1] = 0;
box.Enabled = false;
}
However while trying to check the value within the datareader, it throws an error,"invalid attempt to read data when no data is present". How else should i check to see if data is there or not.!! please help. here x is an sqldatareader SqlDataReader x = cmd.ExecuteReader(); and cmd is a select commnand..
2 Answers 2
You can use SqlDataReader.HasRows
- it is true if at least one row of data is present, alternatively you can read through all results using SqlDataReader.Read()
;
So for your example:
while(x.Read())
{
//read stuff
}
You can do a null check and Has Row before accessing the DataRows.
like this
if(null != x && x.HasRows)
{
//Your code
}
-
but still its showing the same error. !! although the if condition is satisfied, it is not able to execute the line box.Text = Convert.ToString(x[k++]); the fieldcount of x shows 6 but then why is it not able to read x[k] ? even when k = 0.scooby– scooby2011年04月06日 04:08:55 +00:00Commented Apr 6, 2011 at 4:08
-
Try Quickwatch the value of the x[k].Anuraj– Anuraj2011年04月06日 04:39:14 +00:00Commented Apr 6, 2011 at 4:39
-
is it possible to look into the value stored within the datareader? i mean, i dont think there is a way to peep into the value within x[0] or x[1]..scooby– scooby2011年04月06日 05:04:36 +00:00Commented Apr 6, 2011 at 5:04
-
Yes using Quick watch feature you can do this.Anuraj– Anuraj2011年04月06日 05:13:39 +00:00Commented Apr 6, 2011 at 5:13