Search code examples
.netvb.netselectdatatableinvalidargumentexception

System.ArgumentException on DataTable.Select(String)


After taking a break and rewatching the code i found the mistake. See Answers. Thank you for your help.

Questions left:

  • Why didn't it give me an exception on most numbers? // I was not able to see the results. I just know that there was no exceptions on the others.
  • Why does the DataSetDesigner cast that integer (datbase) to a string in the generated class?

I am using the Visual Studio 2017 / Oracle DataSet Designer to get classes inside my application that represent my database. Since Oracle doesn't support Visual Studio 2017 yet I use Visual Studio 2015 to generate my .xsd DataSets.

Using the code below I am getting a weird exception message.

For Each numberRow As myOracleDataSet.CustomerTelNumberRow In numberTbl.Select("customerID = " & row.CustomerID)
    // Some more code
Next

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

Separating the In-clause I found, that the exception occures on the DataTable.Select call.

CutomerID is a String-Property given by the dataset designer and was checked for DBNULL before the select. Code works fine if DataSet.Select is executed with "1=1" or "CustomerID = CustomerID". I also checked the value of CustomerID and found out the exception occurs, when the given CustomerID is 250. Using a hard coded 250 instead of row.CustomerID gave me the same exception. So it probably has to do with the Data inside my Table. What can I do against it?

My String syntax should be correct since it's working for other IDs and the .Select-Method is not generated by myself.

I found the almost same exception on MSDN support.microsoft.com, but there it says that its only for .NET Framework 3.5-based applications. My application is using .NET Framework 4.5.2 .


Solution

  • I finally solved it!
    While the message Min (551) must be less than or equal to max (-1) in a Range object. is somehow confusing the System.InvalidArgumentException gave me the correct direction to go. It's the little things:

    CutomerID is a String-Property given by the dataset designer

    That in combination with numberTbl.Select("customerID = " & row.CustomerID) was the actual exception. While for most cases this String => Integer comparison worked somehow it did not for the 250. CustomerID is a String Property in both tables. So I was comparing String with Int without casting it.

    Following line correction fixed it.

    ̶n̶̶u̶̶m̶̶b̶̶e̶̶r̶̶t̶̶b̶̶l̶̶.̶̶s̶̶e̶̶l̶̶e̶̶c̶̶t̶("̶c̶̶u̶̶s̶̶t̶̶o̶̶m̶̶e̶̶r̶̶i̶̶d̶̶ ̶=̶ ̶"̶ ̶&̶ ̶̶r̶̶o̶̶w̶̶.̶̶c̶̶u̶̶s̶̶t̶̶o̶̶m̶̶e̶̶r̶̶i̶̶d̶)
    numberTbl.Select("customerID = '" & row.CustomerID & "'")
    
    "CustomerID = 250"
    "CustomerID = '250'"