Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

asp.net 2.0 weird datatable problem

Hi folks!
I am experiencing to me a weird problem with my datatables. I have a dataset which contains multiple datatables, generated from stored procedures and tables by the drag and drop interface in VS 2005. However when I am populating these tables i get an error message saying

A first chance exception of type 'System.NullReferenceException' occurred in Server.DLL

Server.DLL is my business layer. It appears that this appears on a non-regular basis but by stepping through my code I have located the error to be in the following snippet

IssuesDataset.IssueReportsReadableDataDataTable returnTable;
DataTable tempTable;

returnTable = new IssuesDataset.IssueReportsReadableDataDataTable();

tempTable = sqlServerImpersonation.ExecuteDataset([REMOVED BY AUTHOR, GETS A DATASET]).Tables[0];

//Here is the error thrown, sometimes the debugger points out that DataRow //dataRow is the reason, other times the keyword in and other times again //the bracket below the foreach statement
foreach (DataRow dataRow in tempTable.Rows)
{
returnTable.ImportRow(dataRow);
}

return returnTable;

The datatable is populated and it works, but this error is driving me insane...

Hopefully someone has an idea about what's causing this.
[1341 byte] By [omac] at [2007-11-11 7:23:53]
# 1 Re: asp.net 2.0 weird datatable problem
I would add code to ensure that tempTable and tempTable.Rows are not null:

tempTable = sqlServerImpersonation.ExecuteDataset(...).Tables[0];
if (tempTable != null)
{
if (tempTable.Rows != null)
{
foreach (DataRow dataRow in tempTable.Rows)
{
returnTable.ImportRow(dataRow);
}
}
}
Phil Weber at 2007-11-11 23:13:48 >