用SqlDataReader读取数值其中GetInt32(n)来读取Int类型的数据,当数据后台所读数据为0时!会出现错误窗口运行时程序界面提示错误如下(WEB):Invalid attempt to read when no data is present. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: Invalid attempt to read when no data is present.Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  
求教该如何读取

解决方案 »

  1.   

    不是零而是DbNull值吧?
    0是可以转换的。
      

  2.   

    用SqlDataReader读取值为0值有那么难吗?楼上两位总是在讲空话
      

  3.   

    System.Data.IDataReader idr = ...;
    if (idr.IsDBNull(n))
       return 0;
    else
       return idr.GetInt32(n);
      

  4.   

    System.Data.IDataReader idr = ...;
    try
    {
      if (!idr.Read()) return 0;
      if (idr.IsDBNull(n))
        return 0;
      else
        return idr.GetInt32(n);
    }
    finally
    {
       idr.Close();
    }
      

  5.   

    TO:
    用SqlDataReader读取值为0值有那么难吗?
    楼上两位总是在讲空话理论来说,当SqlDataReader读取值为0时,用sdr.GetInt32(index)是不会出错了,除非读取的是NULL值...具体看楼主是想干什么了?如果想在读到null值,得到0,那可以作一个判断即可...如果本以为是有值,但读出来却没值,那就要检查下数据库了...如果问题是出在代码上的,建议把代码都贴出来看下...