NullObjectRef in DataRow
-
I use a small table (1..5 records) with big number of fields (about 400 doubles with default 0). All work ok but some writes (about 30) into the columns generates Null Object Reference. (Windows 2000 5.00.2195 srvpack 3, Framework 1.0 srvpack 2) My code:
... loading data ... MyTable.AcceptChanges(); foreach(DataRow dr in MyTable.Rows) { foreach(string fieldname in MySomeColumnsStringArray) { ... some computing ... double x=Result(...); x+=Convert.ToDouble(dr[fieldname]); // works ok, x is ok try { dr[fieldname]=x; // **sometimes** generates Null Object Reference } catch(... } }
What's wrong? Help, please. Sincerely AW -
I use a small table (1..5 records) with big number of fields (about 400 doubles with default 0). All work ok but some writes (about 30) into the columns generates Null Object Reference. (Windows 2000 5.00.2195 srvpack 3, Framework 1.0 srvpack 2) My code:
... loading data ... MyTable.AcceptChanges(); foreach(DataRow dr in MyTable.Rows) { foreach(string fieldname in MySomeColumnsStringArray) { ... some computing ... double x=Result(...); x+=Convert.ToDouble(dr[fieldname]); // works ok, x is ok try { dr[fieldname]=x; // **sometimes** generates Null Object Reference } catch(... } }
What's wrong? Help, please. Sincerely AWWhat is the value of x when your code fails?
-
What is the value of x when your code fails?
Unfortunately, any:
0 10 105.91 NaN
(I won't to write it in real application) Sincerely AW -
What is the value of x when your code fails?
DataRow[idx] fails as well DataRow.ItemArray[idx] works :laugh: Sincerely AW
-
I use a small table (1..5 records) with big number of fields (about 400 doubles with default 0). All work ok but some writes (about 30) into the columns generates Null Object Reference. (Windows 2000 5.00.2195 srvpack 3, Framework 1.0 srvpack 2) My code:
... loading data ... MyTable.AcceptChanges(); foreach(DataRow dr in MyTable.Rows) { foreach(string fieldname in MySomeColumnsStringArray) { ... some computing ... double x=Result(...); x+=Convert.ToDouble(dr[fieldname]); // works ok, x is ok try { dr[fieldname]=x; // **sometimes** generates Null Object Reference } catch(... } }
What's wrong? Help, please. Sincerely AWCould it be that the data in "fieldname" could be null? If so, you could try this:
protected object getField(string FieldName)
{
if (m_row != null)
{
object retval = m_row[FieldName];
if (retval != null && retval != System.DBNull.Value)
{
return retval;
}
else
{
return /* default value for data type e.g. 0 */;
}
}
else
{
return null;
}
}Jim Stewart
-
Could it be that the data in "fieldname" could be null? If so, you could try this:
protected object getField(string FieldName)
{
if (m_row != null)
{
object retval = m_row[FieldName];
if (retval != null && retval != System.DBNull.Value)
{
return retval;
}
else
{
return /* default value for data type e.g. 0 */;
}
}
else
{
return null;
}
}Jim Stewart
My data couldn't be null. Thanks Hi, AW