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
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