NullReferenceException in call to DataTable Select

  • Thread starter Thread starter headware
  • Start date Start date
H

headware

I'm trying to use the Select() method on a DataTable but I'm getting a
NullReferenceException. Here's the stack trace:

at System.Data.Select.Evaluate(Int32 record)
at System.Data.Select.FindFirstMatchingRecord()
at System.Data.Select.GetBinaryFilteredRecords()
at System.Data.Select.SelectRows()
at System.Data.DataTable.Select(String filterExpression, String sort)

I've found a work-around but I can't explain why it works. The work
around is to call the Select() method once first without a filter
argument, then to call it again with the actual filter argument you're
interested in. Like this:

DataRow[] records = ds.Tables["MyTable"].Select("", "C1, C2, C3, C4,
C5, ID"); //note the empty string as the filter

records = ds.Tables["MyTable"].Select(myFilter, "C1, C2, C3, C4, C5,
ID"); //myFilter contains the actual filter value

And for some reason the sorting string to the first Select() call has
to be the same string that's used in the second Select() call. So doing
this wouldn't work:

DataRow[] records = ds.Tables["MyTable"].Select("");

records = ds.Tables["MyTable"].Select(myFilter, "C1, C2, C3, C4, C5,
ID");

The reason I discovered this work-around is that if you copy and paste
the entire call to the Select() method into one of the debugger watch
windows, you'll get a message stating the same null reference exception
as the value of the expression. But if you replace the filter with ""
in the Select() call in the watch window, it will return the correct
result as the expression value. Then if you step through the actual
code it works (it doesn't throw the exception)! It's like the DataTable
needs to be re-initialized before you can call the Select() method,
even if you do it in the debugger watch window.
Does anyone have any ideas about what's going on here?

Thanks,
Dave
 
Hi,
Some Questions?:
1. Does this happen only when you are in the debugger?
2. Can you send more information on how I can reproduce this problem on my
machine, I would like to take a look?

Thanks,
Sushil.
 
It definitely does not only occur when I'm debugging. Let me see if I
can recreate the problem using data that's not sensitive to my company
or our customers. I'll get back to you.

Dave
 
Back
Top