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.
string sqlCount = "select Count(*) Count from table1"; OleDbConnection conn = new OleDbConnection(ConfigurationSettings.AppSettings["ConnStr"].ToString()); OleDbCommand comm = new OleDbCommand(sqlCount,conn); conn.Open();
int counter=0; if (reader.Read()) counter=reader.GetInt32(0);if still has error,pls change the sql string to "select Count(*) from table1"
//change Response.Write(reader[0].ToString());
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).
怎么都不行,Select * form Table1都不行
int cout = 0; if (reader.Read()) { cout = reader.GetInt32(0); } 执行cout = reader.GetInt32(0);时报错:“由于符号不匹配或数据溢出以外的其他原因,未能转换数据值。例如,数据在数据存储区中已损坏,但该行仍可以检索”
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 :(
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();
问另外一个问题,我不用READER,改用DATASET,怎么取出数据? string strCount = ds.Tables["tabTemp"].Rows[0][0].ToString();可以 int bkCount = (int)ds.Tables["tabTemp"].Rows[0][0];不行
blackcatiii(ljh) 用string strCount=""; if (reader.Read()) strCount=(reader.IsDBNull(0))?"no records":reader.GetValue(0).ToString(); 可以解决这个问题
DataGrid2.DataBind();也是错的,其实是因为错,所以加上去看是否有数据的
OleDbCommand comm = new OleDbCommand(sqlCount,conn);
conn.Open();
OleDbDataReader reader = comm.ExecuteReader();
reader.GetInt32(0);
conn.Close();各位兄弟,去掉这两句仍错,如何????
if (reader.Read())
counter=reader.GetInt32(0);if still has error,pls change the sql string to "select Count(*) from table1"
Response.Write(reader[0].ToString());
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).
if (reader.Read())
{
cout = reader.GetInt32(0);
}
执行cout = reader.GetInt32(0);时报错:“由于符号不匹配或数据溢出以外的其他原因,未能转换数据值。例如,数据在数据存储区中已损坏,但该行仍可以检索”
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 :(
if (reader.Read())
strCount=(reader.IsDBNull(0))?"no records":reader.GetValue(0).ToString();
string strCount = ds.Tables["tabTemp"].Rows[0][0].ToString();可以
int bkCount = (int)ds.Tables["tabTemp"].Rows[0][0];不行
用string strCount="";
if (reader.Read())
strCount=(reader.IsDBNull(0))?"no records":reader.GetValue(0).ToString();
可以解决这个问题