NullReferenceExeption in DataTable

G

Guest

Hello,

we use a DataTable-Object to manage some data. Sometimes we get
NullReferenceExeptions when we call DataTable.Select or when we change a row.
Below you can see the callstacks:

CallStack: at System.Data.RecordManager.NewRecordBase()
at System.Data.DataTable.NewRecord(Int32 sourceRecord)
at System.Data.DataRow.BeginEdit()
at System.Data.DataRow.set_Item(DataColumn column, Object value)
at System.Data.DataRow.set_Item(String columnName, Object value)

an other callstack:
CallStack: at System.Data.Select.FindClosestCandidateIndex()
at System.Data.Select.SelectRows()
at System.Data.DataTable.Select(String filterExpression)

or the last callstack:
CallStack: at System.Data.DataTable.get_LiveIndexes()
at System.Data.DataTable.SetNewRecord(DataRow row, Int32 proposedRecord,
DataRowAction action, Boolean isInMerge)
at System.Data.DataRow.EndEdit()
at System.Data.DataRow.set_Item(DataColumn column, Object value)
at System.Data.DataRow.set_Item(String columnName, Object value)

Does anyone know the reason for that exceptions?

Thanks
Andreas
 
W

W.G. Ryan - MVP

Is calling .Select alone throwing the exception or are you setting some
variable to the result? Actually, if you don't mind, can you post the code
snippet and the piece that's throwing the exception?
 
G

Guest

Here is the code who throws the exception:

public bool RenewLicense(string a_licenseclientGuid, string
a_singleLicenseGuid)
{
int retry=1;
oncemore:
try
{
//This line throws sometimes the exception
//m_dsClientLicenseLookup is a DataTable
DataRow[] rows =
m_dsClientLicenseLookup.Tables[0].SelectColumnLookupDsColumnGuid+"='"+a_singleLicenseGuid+"'");
// some other code
return false;
}
catch(NullReferenceException exNull)
{ string msg = String.Format("Error on renew Client {0}
Exception: {1}
CallStack:{2}",a_singleLicenseGuid,exNull.Message,exNull.StackTrace);
LicenseLogService.Fail("LicenseServer:RenewLicense",msg);
if ( retry > 0 )
{
retry--;
//This call creates a new DataTable-Object,
//fills it with data and assign the new DataTable-Object
to
//m_dsClientLicenseLookup
Priv_Reload();
goto oncemore;
//Often this retry-mechanism helps and there is no
exception in the
//second try, but sometime even the second try throw
this exception.
}
throw;
}
return false;
}

The retry-mechanism is an awful pice of code. But we don't know the reason
for the exception and we have no idea what we could change.
 
W

W.G. Ryan - MVP

Andreas - have you tested to ensure that table, columns and dataset aren't
null?
Andreas Rossi said:
Here is the code who throws the exception:

public bool RenewLicense(string a_licenseclientGuid, string
a_singleLicenseGuid)
{
int retry=1;
oncemore:
try
{
//This line throws sometimes the exception
//m_dsClientLicenseLookup is a DataTable
DataRow[] rows =
m_dsClientLicenseLookup.Tables[0].SelectColumnLookupDsColumnGuid+"='"+a_singleLicenseGuid+"'");
// some other code
return false;
}
catch(NullReferenceException exNull)
{ string msg = String.Format("Error on renew Client {0}
Exception: {1}
CallStack:{2}",a_singleLicenseGuid,exNull.Message,exNull.StackTrace);
LicenseLogService.Fail("LicenseServer:RenewLicense",msg);
if ( retry > 0 )
{
retry--;
//This call creates a new DataTable-Object,
//fills it with data and assign the new
DataTable-Object
to
//m_dsClientLicenseLookup
Priv_Reload();
goto oncemore;
//Often this retry-mechanism helps and there is no
exception in the
//second try, but sometime even the second try throw
this exception.
}
throw;
}
return false;
}

The retry-mechanism is an awful pice of code. But we don't know the reason
for the exception and we have no idea what we could change.


W.G. Ryan - MVP said:
Is calling .Select alone throwing the exception or are you setting some
variable to the result? Actually, if you don't mind, can you post the
code
snippet and the piece that's throwing the exception?
message
 
G

Guest

If dataset and the table would be null, you wouldn't get that callstack
CallStack: at System.Data.Select.FindClosestCandidateIndex()
at System.Data.Select.SelectRows()
at System.Data.DataTable.Select(String filterExpression)
The columns couldn't be null, because there is no code who deletes columns.

I'm sure dataset, table and columns are not null.


W.G. Ryan - MVP said:
Andreas - have you tested to ensure that table, columns and dataset aren't
null?
Andreas Rossi said:
Here is the code who throws the exception:

public bool RenewLicense(string a_licenseclientGuid, string
a_singleLicenseGuid)
{
int retry=1;
oncemore:
try
{
//This line throws sometimes the exception
//m_dsClientLicenseLookup is a DataTable
DataRow[] rows =
m_dsClientLicenseLookup.Tables[0].Select( ColumnLookupDsColumnGuid+"='"+a_singleLicenseGuid+"'");
// some other code
return false;
}
catch(NullReferenceException exNull)
{ string msg = String.Format("Error on renew Client {0}
Exception: {1}
CallStack:{2}",a_singleLicenseGuid,exNull.Message,exNull.StackTrace);
LicenseLogService.Fail("LicenseServer:RenewLicense",msg);
if ( retry > 0 )
{
retry--;
//This call creates a new DataTable-Object,
//fills it with data and assign the new
DataTable-Object
to
//m_dsClientLicenseLookup
Priv_Reload();
goto oncemore;
//Often this retry-mechanism helps and there is no
exception in the
//second try, but sometime even the second try throw
this exception.
}
throw;
}
return false;
}

The retry-mechanism is an awful pice of code. But we don't know the reason
for the exception and we have no idea what we could change.


W.G. Ryan - MVP said:
Is calling .Select alone throwing the exception or are you setting some
variable to the result? Actually, if you don't mind, can you post the
code
snippet and the piece that's throwing the exception?
message
Hello,

we use a DataTable-Object to manage some data. Sometimes we get
NullReferenceExeptions when we call DataTable.Select or when we change
a
row.
Below you can see the callstacks:

CallStack: at System.Data.RecordManager.NewRecordBase()
at System.Data.DataTable.NewRecord(Int32 sourceRecord)
at System.Data.DataRow.BeginEdit()
at System.Data.DataRow.set_Item(DataColumn column, Object value)
at System.Data.DataRow.set_Item(String columnName, Object value)

an other callstack:
CallStack: at System.Data.Select.FindClosestCandidateIndex()
at System.Data.Select.SelectRows()
at System.Data.DataTable.Select(String filterExpression)

or the last callstack:
CallStack: at System.Data.DataTable.get_LiveIndexes()
at System.Data.DataTable.SetNewRecord(DataRow row, Int32
proposedRecord,
DataRowAction action, Boolean isInMerge)
at System.Data.DataRow.EndEdit()
at System.Data.DataRow.set_Item(DataColumn column, Object value)
at System.Data.DataRow.set_Item(String columnName, Object value)

Does anyone know the reason for that exceptions?

Thanks
Andreas
 
G

Guest

I am seeing the exact same thing (and a quick Google search turns up many,
many hits on this problem). Perhaps there is an update on the issue?

Here is my stack trace:
StackTrace " at System.Data.Select.FindClosestCandidateIndex()
at System.Data.Select.SelectRows()
at System.Data.DataTable.Select(String filterExpression)
at Pathfinder.Entity.FindRows(String query) in
C:\\Development\\05.11.01\\ClassLibrary\\ProfileManager\\Entity.cs:line 3428"

The code snippet is:
DataRow[] rows = new DataRow[0];
try
{
rows = ((DataTable)this).Select(query);
}
catch (Exception ex)
{
Console.WriteLine("Unable to perform DataTable.Select: {0}", ex.Message);
}

'this', is my DataTable derived object and is most definitely not null. It
has seven columns and *all* are in the PrimaryKey on the table. At the point
where the exception is being thrown there are only 33 rows in the table. The
query string is non null and valid (it is filtering on four of the seven PK
values). FWIW, the query string is:

"PkgProgId = 13 and SettlementDt > '2005/11/07' and IssueDt <= '2005/11/01'"

PkgProgId is an Int32
SettlementDt and IssueDt are (of course) DateTime

This exception does not always occur and I can perform thousands of these
Select operations against this table without problem. But once the exception
is thrown it will always be thrown.

I have seen speculation that it may be due to having one or more DataView
objects created for the table and that, somehow, this messes up the internal
indexing. I have not seen that this speculation has been confirmed and there
is nothing in the MSDN KB for this issue (that I was able to find).

Anyway, I was hoping that there would be more information on this subject,
and perhaps confirmation from MS that it is a problem and that there is
something that can be done as a work-around.

Regards,
-- Thomas Brown
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top