ArgumentException in DataTable Select statement
Under some circumstances, filtering a Datatable using its Select method may result in throwing following exception:
“Min (some value) must be less than or equal to Max (-1) in a Range object.”
This happens only for particular values when the columns in case are of unknown type resulting in filtering
string data with numbers. To avoid running into such a case, always supply the type of the column when
creating a Datatable:
DataTable dtTest = new DataTable("Customers"); // instead of //dtTest.Columns.Add("CustID"); // use dtTest.Columns.Add("CustID", typeof(Int32));
Although is uncommon working with unspecified columns types, this situation may occur. If for any reason the Datatable structure is out of reach, being known that all loaded data will be treated as strings, a workaround for this is to delimit the values in select expression with single quotes:
DataRow[] drs = dtCust.Select("StreetNo='" + _nStreetNo + "'");
or better yet, filter the Dataview to get same result, so instead of the line above, use:
dtCust.DefaultView.RowFilter = string.Format("StreetNo = '{0}'", _nStreetNo); DataTable dtFiltered = dtCust.DefaultView.ToTable();


Tags: 



One Response
July 7, 2010 1
Thanks for help.
Leave a Reply