user3494110
user3494110

Reputation: 427

DataAdapter.fill null value return error

I have a small sql statement that checks to see if there are any records that match error criteria so I can send out an alert. However most of the time there should not be any records that match the criteria. My issue is that when I run my program gets to the 'dataAdapt.fill(returnTable)' step, it throws an exception because the value is null. How can I avoid this? If the value is null I just want it to continue to where I can close out the program, not throw an exception.

try
{
    //open connection
    netezzaConn = new OleDbConnection(connString);
    netezzaConn.Open();
    //execute sql statement
    OleDbCommand exe = new OleDbCommand(sqlStatement, netezzaConn);
    OleDbDataAdapter dataAdapt = new OleDbDataAdapter(exe);
    dataAdapt.Fill(returnTable);
}
catch (Exception ex)
{
    Console.WriteLine(ex);
}

if (returnTable.Rows.Count >= 1)
{
    recordCount = returnTable.Rows.Count;
    sendEmail.sendnotificationEmail(recordCount);
}

Upvotes: 1

Views: 4288

Answers (1)

LarsTech
LarsTech

Reputation: 81610

You need to instantiate the returnTable variable:

DataTable returnTable = new DataTable();

Without the "new", returnTable is a null object, and the DataAdapter will burp when it tries to use it.

Upvotes: 1

Related Questions