I'm trying to check to see if a DataTable object is null or not, but whenever the DataTable is null, my program keeps giving me an OleDBException.
I have this method to set up the DataTable
public DataTable setUpDataTable(String sql)
{
String connString = connDel;
using (OleDbConnection conn = new OleDbConnection(connString))
{
OleDbCommand cmd = new OleDbCommand(sql, conn);
try
{
conn.Open();
}
catch (NullReferenceException ex)
{
MessageBox.Show(ex.Message);
}
DataTable delTable = new DataTable();
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(delTable);
return delTable;
}
}
And here is where I call the method to initialize the DataTable object(the original method is in another class). I want to see if this DataTable is null.
DataTable duplicates = main.setUpDataTable(sql);
if (duplicates != null)
return false;
The program doesn't get to the if statement at all, but throws an exception right at the initialization of the DataTable object. The method works when there is something in the DataTable, but throws the exception when it is null. How can I fix this?