DataReader是流式向前读,绑定后就不能再进行读取数据,当然要错,可用DataSet

解决方案 »

  1.   

    我去掉绑定DataGrid2.DataSource = reader;
    DataGrid2.DataBind();也是错的,其实是因为错,所以加上去看是否有数据的
      

  2.   

    正如Seeko0所言,出错是必然的,看来你还要好好理解DataReader,加油吧!
      

  3.   

    DataReader is forward only. In your code,you use it as a DataSource.After Binding,the reader cannot be read again.So,if you use reader.GetInt32(0) after binding, you'll get an error.
      

  4.   

    string sqlCount = "select Count(*) Count from table1"; OleDbConnection conn = new OleDbConnection(ConfigurationSettings.AppSettings["ConnStr"].ToString());
    OleDbCommand comm = new OleDbCommand(sqlCount,conn);
    conn.Open();
     
    OleDbDataReader reader = comm.ExecuteReader();
    reader.GetInt32(0);
    conn.Close();各位兄弟,去掉这两句仍错,如何????
      

  5.   

    int counter=0;
    if (reader.Read())
     counter=reader.GetInt32(0);if still has error,pls change the sql string to "select Count(*) from table1"
      

  6.   

    //change 
          Response.Write(reader[0].ToString());
      

  7.   

    I know the reason: the method Read() is not used in your code.
    The method reader.Read() will set the cursor go to the first position of resultset.If not used,The object reader's value is equals to null (I think).
      

  8.   

    怎么都不行,Select * form Table1都不行
      

  9.   

    int cout = 0;
    if (reader.Read())
    {
    cout = reader.GetInt32(0);
    }
    执行cout = reader.GetInt32(0);时报错:“由于符号不匹配或数据溢出以外的其他原因,未能转换数据值。例如,数据在数据存储区中已损坏,但该行仍可以检索”
      

  10.   

    try:
    count=(int)reader.GetValue(0);
    If you select Sql server as your database,I recommend using the SqlClient namespace.To my opinion,the OleDbClient is garbage :(
      

  11.   

    if still has error,just use a string-type value to store the number,like belows:string strCount="";
    if (reader.Read())
      strCount=(reader.IsDBNull(0))?"no records":reader.GetValue(0).ToString();
      

  12.   

    问另外一个问题,我不用READER,改用DATASET,怎么取出数据?
    string strCount = ds.Tables["tabTemp"].Rows[0][0].ToString();可以 
    int bkCount = (int)ds.Tables["tabTemp"].Rows[0][0];不行
      

  13.   

    blackcatiii(ljh) 
    用string strCount="";
    if (reader.Read())
      strCount=(reader.IsDBNull(0))?"no records":reader.GetValue(0).ToString();
    可以解决这个问题