locked
ArgumentException in DataTable Select statement RRS feed

  • Question

  • Hi there

     

    I hope this is the right forum for this!

     

    I use my software at events to capture data from a scoring system using TCP. I disect each packet and place it in various datatables in a dataset. I was doing this all last year on .net 2.0 and it was fine. I have moved this year to .net 3.5 and now I get this error - though I'm not sure its related to 3.5? I'm using C# with VS 2008.

     

    When a packet comes in I use a lookup to another table in the dataset to find info on the object in the packet. Sometimes, and it looks to be random, I get an exception which says:

     

    System.ArgumentException: Min (281) must be less than or equal to Max (-1) in a Range object.

    at System.Data.Select.GetBinaryFilteredRecords()

    at System.Data.Select.SelectRows()

    at System.Data.DataTable.Select(String filterExpression)

    at GetCurrentDriverIdx(string SeriesCode, string CarNumber)

    at LeaderBoardUpdate(LBRec lbr)

     

    The number in Min varies but the Max is always -1. The line in GetCurrentDriverIdx that calls Select looks like this:

     

    MotorsportDataSet.VehiclesRow[] vr = (MotorsportDataSet.VehiclesRow[])motorsportDataSet.Vehicles.Select("SeriesCode='" + _strSeriesCode + "' AND DisplayNum='" + _sDispNum + "'");

     

    Neither SeriesCode or DisplayNum are keys.

     

    This appears to happen after edits have been made to the DataTable, though I'm not positive. We shut the app down and start it up and everything is OK, so I'm assuming that the edits leave something bad lying around.

     

    Any info really appreciated - Please!!

     

    Dave


    Monday, February 25, 2008 12:07 AM

Answers

  • I'm coming in a little late, but hopefully can add some value.

     

    On multithreading, because we don't support multithreading, any changes to the framework and/or CLR can cause previously working multithreaded code to fail. It's highly recommend that you protect your writes in a lock - readers across threads should work correctly.

     

    For the core issue of the select, what are the datatypes? We have a complex set of casting rules in the Expression that can be easily broken, especially when converting from passed in strings. As a general rule, you should use the Convert function to explicitly convert your string arguments to the correct type. This will avoid exceptions and give you better performance (as we don't need to attempt a conversion to determine the correct type). Please give that a try and let me know if it helps.

     

    Thanks,

    Erick

     

    Sunday, March 9, 2008 9:37 PM

All replies

  • The first thing I'd suspect is that somewhere in your app you're doing multithreaded write operations to your DataSet, like maybe you're writing to some object in it both with a BindingSource and from an event handler running on the UI thread.  I haven't seen this particular issue before, but it sure reads like there's some kind of internal corruption in the DataSet, and unsynchronized write operations are a proven way to make that happen.

    Monday, February 25, 2008 5:52 PM
  • Hi Robert

     

    When you say "...writing to some object in it both with a BindingSource and from an event handler running on the UI thread. " I thought that would be OK if both are accesed by the same UI thread. Have I misunderstood what you are saying or what is allowed?

     

    Also, this hasn't changed since last year, and I never had the error last year, so could it be .net 3.5?

     

    Thanks

    Dave

     

    Bump...

    Tuesday, February 26, 2008 6:58 AM
  • Hi there

     

    A bit of extra info.

     

    I'm now debugging with the .net source and have found the function GetBinaryFilteredRecords()

     

    Code Snippet

    private Range GetBinaryFilteredRecords() {

    if (matchedCandidates == 0) {

    return new Range(0, index.RecordCount-1);

    }

    Debug.Assert(matchedCandidates <= index.IndexDesc.Length, "GetBinaryFilteredRecords : Invalid Index");

    int lo = FindFirstMatchingRecord();

    if (lo == -1) {

    return new Range();

    }

    int hi = FindLastMatchingRecord(lo);

    Debug.Assert (lo <= hi, "GetBinaryFilteredRecords : Invalid Search Results");

    return new Range(lo, hi);

    }

     

    The Debug.Assert is what I'm getting. Does anyone know what can cause "Invalid Search Results"?

     

    Thanks

    Dave

     

    Bump...Anyone?

    Wednesday, February 27, 2008 5:02 AM
  • The BindingSource appears to interoperate with its data source asynchronously, on a thread that it creates and manages itself.  If you have an event handler in the UI that is talking directly to the DataTable, you can encounter all manner of strange errors, all of which tie back to two threads making unsynchronized writes.

     

    There could conceivably be changes in the BindingSource in .Net 3.5 that make these exceptions more likely; I don't really know myself.

    Thursday, February 28, 2008 12:09 AM
  • Hi Robert

     

    Thanks for the explaination - I didn't know that was the case. For a moment I thought that may have been the problem but I've looked closely and I don't think so. I actually keep all the info as 2 distinct and different datasets. One for data that is entered by the operator (static) and one for the live data coming in from the datafeed. The operators can't edit the live dataset and the live data isn't entered into the static data dataset.

     

    Though I think maybe something like what you are saying is happening, I think (I haven't been able reproduce the error after numerous tries) that the static data needs to be edited for the problem to arrise. Each time it did happen I restarted the app and all went fine, so the dataset was re-initialized and all was good in the world again. Just not sure what editing it does - sometimes!

     

    If anyone else can think of something, maybe a MS programmer that wrote the dataset code?

     

    Cheers

    Dave

     

    Friday, February 29, 2008 1:26 AM
  • How is the static data being edited?  Via data-bound controls?

    Friday, February 29, 2008 9:01 AM
  • Hi Robert

     

    Yes. Via 4 DataGridViews. The one that is connected to the table that is causing the error has some Combo boxes that are lookups into another table, though I don't think that has anything to do with the problem.

    Just to add some more light, in the first post the line with the error:

     

    MotorsportDataSet.VehiclesRow[] vr = (MotorsportDataSet.VehiclesRow[])motorsportDataSet.Vehicles.Select("SeriesCode='" + _strSeriesCode + "' AND DisplayNum='" + _sDispNum + "'");

     

    is called from the code that is splitting up the incoming data, but the table it is reverencing (Vehicles) is in the static dataset.

     

    Cheers

    Dave

    Friday, February 29, 2008 8:08 PM
  • Are the DataGridViews bound to the table directly, or via a BindingSource?  If the latter, is there any other code that is making updates to the bound tables?

     

    Saturday, March 1, 2008 12:00 AM
  • Yes, they are bound via Bindingsources. No, only updates are through the DataGridViews. There are reads via Selects on the tables though.

     

    Saturday, March 1, 2008 5:40 AM
  • Reads shouldn't cause problems, only writes.  I may be barking up the wrong tree here; it's just the tree I know.

    Saturday, March 1, 2008 6:41 AM
  • Bump...

     

    Anyone got any ideas? I have to use this again next weekend and it looks pretty flakey when it has to be shut down and restarted. (On hands and knees) please...

     

    Monday, March 3, 2008 1:08 AM
  • Have u triued to set the Value into ' Quotes?
    • Proposed as answer by Anonymous3788 Thursday, December 18, 2008 12:18 AM
    Monday, March 3, 2008 11:38 AM
  • I was receiving the same error in .NET 2.0 when selecting records from a data table and I changed the values in the select statement to be wrapped by single quotes and the error disappeared.

     

    The interesting thing was that I didn't get this error when running my application on my local PC (ASP.NET dev server) but when I moved it to a Windows 2003 Server with IIS 6 it would give me the error.

     

    Tuesday, March 4, 2008 2:12 PM
  • Hi Buxo1978 & bdilla

     

    Mmmmmm...the plot thickens!

     

    After I had this problem and was trying to sort out what was going on, I changed this line:

     

    MotorsportDataSet.VehiclesRow[] vr = (MotorsportDataSet.VehiclesRow[])motorsportDataSet.Vehicles.Select("SeriesCode='" + _strSeriesCode + "' AND DisplayNum='" + _sDispNum + "'");

     

    to include single quotes around the _sDispNum. _strSeriesCode already was in quotes. Since making that change I haven't been able to break the code. Maybe thats fixed it? The error was difficult to force to happen but since that weekend I haven't been able to make it fail at all. Hopefully you guys have hit the nail on the head!

     

    It will be used again this weekend, so I'll report back on Monday to let you know if the problem came back.

     

    Thanks

    Dave

     

     

    • Proposed as answer by Anonymous3788 Thursday, December 18, 2008 12:19 AM
    Tuesday, March 4, 2008 11:09 PM
  • Well, okay, that's a completely different kind of problem, isn't it.

     

    Here's a useful tip for formulating filter expressions:

     

       string filter = String.Format("SeriesCode = '{0}' AND DisplayNum = '{1}'", seriesCode, displayNum);

     

    It's much easier to see where single quotes will be in the resulting filter expression if you use String.Format.

    Wednesday, March 5, 2008 9:44 PM
  • I'm coming in a little late, but hopefully can add some value.

     

    On multithreading, because we don't support multithreading, any changes to the framework and/or CLR can cause previously working multithreaded code to fail. It's highly recommend that you protect your writes in a lock - readers across threads should work correctly.

     

    For the core issue of the select, what are the datatypes? We have a complex set of casting rules in the Expression that can be easily broken, especially when converting from passed in strings. As a general rule, you should use the Convert function to explicitly convert your string arguments to the correct type. This will avoid exceptions and give you better performance (as we don't need to attempt a conversion to determine the correct type). Please give that a try and let me know if it helps.

     

    Thanks,

    Erick

     

    Sunday, March 9, 2008 9:37 PM
  •  

    You always must save your dataset to xml with schema. This why you get that error. If there is no schema, then all loaded data is treated as strings. So filtering string data with numbers causes this error
    Monday, March 17, 2008 2:10 PM
  • I haven't heard anything on this issue for a while, so I'm assuming that the issue is closed. I've marked the question as answered - if you are still having issues, please reopen the question.

     

    Thank you,

    Erick

     

    Wednesday, March 26, 2008 3:30 AM
  • Hi Erick & everyone else

     

    Sorry it took so long to get back, I've been round the world for sixpence!

     

    Putting quotes around _sDispNum did fix the problem.

     

    Code Snippet

    MotorsportDataSet.VehiclesRow[] vr = (MotorsportDataSet.VehiclesRow[])motorsportDataSet.Vehicles.Select("SeriesCode='" + _strSeriesCode + "' AND DisplayNum='" + _sDispNum + "'");

     

     

    In the above, _strSeriesCode & _sDispNum are both strings, but I wasn't originally quoting the _sDispNum. So even though _sDispNum should have been a string version of an integer, I think somewhere in the table was a rogue row that had a space or DBNull for the value. When I took the quotes out I was able to make it fail pretty much all the time. With the quotes in it works perfectly.

     

    Thanks to everyone for their suggestions.

     

    Cheers

    Dave

     

    Friday, March 28, 2008 11:20 PM
  • I was receiving the same error. Fixing it, as you suggested, was as simple as delimiting the value with single quotes. Thanks for the fix, buddy!

    Cheers!
    Wednesday, April 16, 2008 8:16 PM
  •  mfyuce wrote:

     

    You always must save your dataset to xml with schema. This why you get that error. If there is no schema, then all loaded data is treated as strings. So filtering string data with numbers causes this error



    That's right. The DataTable I was working with is defined by simply adding columns to it without specifying their types (I've no idea why someone would define a DataTable _that_ way but, hey, we do have some code like that). The issue was resolved by delimiting the value I was passing in single quotes.

    Cheers!
    Wednesday, April 16, 2008 8:25 PM
  • I concur with this response.  I updated my SELECT to wrap the value to test in single quotes, and the problem disappeared.

     

    Monday, April 21, 2008 8:36 PM
  • I just wanted to post that I ran into the same error doing a DataTable.Select with 

    Filter: GuidColumn1='8CB32061-B885-4efc-9309-7DB8C75FF7F7' AND GuidColumn2='B0DFF1EF-F38F-4347-A82E-12593BE4A4F2'

    Sort: <empty string>

    Error: System.ArgumentException: Min (11) must be less than or equal to max (-1) in a Range object.

     

    This was in a single-threaded application with no data binding, and the dataset was freshly loaded from the database, (still unmodified)

     

    As this error only seemed to occur on one particular machine I had access to, which had the following .NET Frameworks installed: (1.1, 2.0 SP1, 3.0) I uninstalled .NET 2.0, and then re-installed 2.0, plus all available updates for 2.0 & 3.0, and the problem went away.  I have heard of this happening on _some_ client machines as well however, so although the problem is uncommon, it was not unique to this specific machine.

     

    Wednesday, May 21, 2008 6:59 PM
  •  

    I can't speak to the order of updates, but if you are using Guids in a Select/Filter statement, you should explicitly convert the Guids. This will both fix issues like this, as well as increase performance.

     

    Thanks

    Erick

    Wednesday, May 21, 2008 8:38 PM
  • I am very late to this discussion, but I wanted to know the root cause of the error.

    As in my case I found, filtering a datatable with a expression say

    "columnvalue = n"  where n is a string representation of a integer value and column datatype is also integer.

     

    This expression may give range error for say value 10,11, 48 etc but not for others.

    Why its behaving this way, I know the solution for this, but I wanted to know if this is a bug in the datatable.select function of dot net.

    Wednesday, September 3, 2008 6:49 PM
  • I post to this thread, because I've the same problem as Akash_me and hope it would be solved.

    here is a code snipped:

    1  for (int i = System.Convert.ToInt32(start); i <= System.Convert.ToInt32(end); i++)
    2  {
    3                   System.Data.DataRow[] found = retTable.Select("ColumnValue = " + i.ToString());
    4                   if (found.Length == 0)
    5                        freeValues.Add(i);
    6  }


    I get the same error in line 3

    This is not multi threaded and i never change the DataTable as you see in the listening
    It works many times and breaks always on the same value of i.

    In my tests start = 100, end = 200 and the i value when the error occures is 192.
    The retTable has 71 rows. freeValues has 33 items when it occures.
    This is my error message: Min (61) must be less than or equal to max (-1) in a Range object.

    I don't know if the value 192 is found in the retTable, I think this is not importend because there are many values not found but the exception did not occure.



    Thursday, October 30, 2008 3:22 PM
  • MattBarber said:

    I just wanted to post that I ran into the same error doing a DataTable.Select with 

    Filter: GuidColumn1='8CB32061-B885-4efc-9309-7DB8C75FF7F7' AND GuidColumn2='B0DFF1EF-F38F-4347-A82E-12593BE4A4F2'

    Sort: <empty string>

    Error: System.ArgumentException: Min (11) must be less than or equal to max (-1) in a Range object.

     This was in a single-threaded application with no data binding, and the dataset was freshly loaded from the database, (still unmodified)



    I ran into a similar problem, but it seemed to be only on a particular ID (GUIDs). If I removed this problematic ID, all was OK. I tried to fix it by using the single quote as proposed earlier, but no luck.

    In stead of using:

    DataRow[] rows = ds.Select("SampleID = '[some ID]'");  
     
    foreach(DataRow r in rows)  
    {  
       do stuff with row....  

    I ended up with using a DefaultView in stead, and then converted it to a datatable. May not be the pretty solution, but it appears to solve my problem:

    ds.DefaultView.RowFilter = string.Format("SampleID = '{0}'", [some ID]);  
    DataTable dt = ds.DefaultView.ToTable();  
     
    foreach (DataRow r in dt.Rows)  
    {  
      do stuff with row...  
    Friday, December 19, 2008 7:50 AM
  • Single Quotes really workd for me. It's worth trying it first of all.

    Thanks Buxo1978!

    Cheers,

    Dumindu


    kuku
    Tuesday, June 1, 2010 5:05 AM
  • This worked for me too.

    ds.DefaultView.RowFilter = string.Format("SampleID =" + [some ID]);  

    DataTable dt = ds.DefaultView.ToTable();  
     
    foreach (DataRow r in dt.Rows)  
    {  
      do stuff with row...  

    ds.DefaultView.RowFilter = "";

     

    Cheers!

    Ben

    Wednesday, September 15, 2010 11:02 PM