开发者

sqldatareader error

开发者 https://www.devze.com 2023-02-22 03:23 出处:网络
I want to check if the sqldatareader is null or not.So tried the following code: if (x[k]!= DBNull.Value)

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 whil开发者_开发技巧e 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..


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消