Question posted 2010 · +6 upvotes
I am accessing an MS Access 2007 database through C#, and I keep getting an exception whenever I try to read an empty cell.
Specifically, I am trying to read a “Date/Time” cell that may or may not be empty. I am using OLE DB, and have filled a DataSet. None of these conditions work:
DataSet dataSet = GetDataSet();
DataRow row = dataSet.Tables[0].Rows[0];
DateTime time = new DateTime();
time = (DateTime)row[5]; // Exception thrown
How to check if the cell is empty before trying to assign it? None of these work:
if(row[5] == null) ;
if(row[5] == DBNull) ;
if(row[5] == (String)"") ;
Edit: I should have mentioned: When I debug, it says that row[5] equals “System.DBNull,”, but I get an error when I try “if(row[5] == DBNULL)”. The error says “DBNULL is a type, which is not valid in the given context”.
Accepted answer +8 upvotes
You can check it like the following.
if (row[5] == DBNull.Value)
{
// value is null
}
else if (String.IsNullOfEmpty(Convert.ToString(row[5]))
{
// value is still null
}
else
{
// value is not null here
}
Top ms-access Q&A (6)
- How can I modify a saved Microsoft Access 2007 or 2010 Import Specification? +31 (2008)
- OleDbCommand parameters order and priority +28 (2009)
- Is there an equivalent to the SUBSTRING function in MS Access SQL? +26 (2009)
- What do I need to read Microsoft Access databases using Python? +25 (2009)
- MS Access library for python +24 (2009)
- is there any replacement of Access? +21 (2009)
ms-access solutions on this site
.