I need help to resolve this problem:
I would like to access a MDB file and query some data from it, under the debug mode, Locals window, I'm able to see the data inside the reader object if i query for the whole table using the syntax as shown below:
using (OleDbConnection AccessConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; User Id=; Password=; Data Source=C:\PVCTData\Measurement_Result.mdb"))
OleDbCommand cmd = new OleDbCommand("SELECT * FROM Results order by TestTimeDate", AccessConnection);
AccessConnection.Open();
OleDbDataReader reader;
reader = cmd.ExecuteReader();
MessageBox.Show(reader.GetValue(0).ToString());
reader.Close();
AccessConnection.Close();
return sn;
}
however, if i just want to retrieve one record by using the query "SELECT top1 CellIDStr FROM Results order by TestTimeDate", the program will throw an exception:
exception {"No data exists for the row/column."}
System.Exception {System.InvalidOperationException}
May i know what is the problem in my code? Thanks.
VulpesPosted May 12, 2011, 10:51 AM
using (OleDbConnection AccessConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; User Id=; Password=; Data Source=C:\PVCTData\Measurement_Result.mdb"))
{
OleDbCommand cmd = new OleDbCommand("SELECT top1 CellIDStr FROM Results order by TestTimeDate", AccessConnection);
AccessConnection.Open();
object obj = cmd.ExecuteScalar();
string value = "";
if (!Convert.IsDBNull(obj)) value = obj.ToString(); //make sure not null before applying ToString()
MessageBox.Show("The value returned is " + value);
}
return sn;
Kok Keong ChuaPosted May 13, 2011, 12:22 AM
Suthish NairPosted May 12, 2011, 2:19 PM
while(reader.Read())
{
MessageBox.Show(reader.GetString(0));
}