Windows Develop Bookmark and Share   
 index > Windows Forms General > Min (5) must be less than or equal to max (-1) in a Range object
 

Min (5) must be less than or equal to max (-1) in a Range object

Hi

In my windows application, when i did the select operation from dataset, i got the error message as"Min (5) must be less than or equal to max (-1) in a Range object".I would like to know the reason for this error. My sample application code is pasting here.




string[] sSql = new string[1];
DataSet oDsTemp = new DataSet();
string sErr = string.Empty;
DataSet oDsFilter = new DataSet();
DataTable oTable ;
DataRow oNewRow;
string sMailId = string.Empty;
try
{

oTable = oDsFilter.Tables.Add();
oTable.Columns.Add("Mail_Id", typeof(string)).Caption = "Mail Id";
oTable.Columns.Add("From", typeof(string)).Caption = "From";
oDsTemp.ReadXml("C:\\Data.xml");

foreach (DataRow oRow in oDsTemp.Tables[0].Rows)
{
oNewRow = oTable.NewRow();
foreach (DataColumn oCol in oTable.Columns)
{
switch (oCol.ColumnName)
{
case "Mail_Id": oNewRow[oCol.ColumnName] = oRow["MAIL_ID"]; break;
case "From": oNewRow[oCol.ColumnName] = oRow["MAIL_FROM"]; break;
}
}
oTable.Rows.Add(oNewRow);
}
oDsFilter.AcceptChanges();

if (oDsFilter != null && oDsFilter.Tables.Count > 0 && oDsFilter.Tables[0].Rows.Count > 0)
{
//MessageBox.Show("Total number of rows:" + oDsTemp.Tables[0].Rows.Count);

foreach (DataRow oDr in oDsFilter.Tables[0].Rows)
{
DataRow[] oDrDSlist = null;
sMailId = oDr["Mail_Id"].ToString();
oDrDSlist = oDsFilter.Tables[0].Select(" Mail_Id=" + oDr["Mail_Id"].ToString());
}
MessageBox.Show("Completed");
}
}
catch (Exception Ex)
{
MessageBox.Show("Mail Id: "+ sMailId +'\n'+"Error:"+ Ex.Message.ToString());
}



The XML File :

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<TS_Table0>
<MAIL_ID>1</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>2</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>3</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>4</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>5</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>6</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>7</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>8</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>9</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>10</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>11</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>12</MAIL_ID>
<MAIL_FROM>LEABENS</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>13</MAIL_ID>
<MAIL_FROM>LEABENS</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>14</MAIL_ID>
<MAIL_FROM>LEABENS</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>15</MAIL_ID>
<MAIL_FROM>LEABENS</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>16</MAIL_ID>
<MAIL_FROM>IMTIAZA</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>17</MAIL_ID>
<MAIL_FROM>IMTIAZA</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>18</MAIL_ID>
<MAIL_FROM>IMTIAZA</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>19</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>20</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>21</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>22</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>23</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>24</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>25</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>26</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>27</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>28</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>29</MAIL_ID>
<MAIL_FROM>ADMIN</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>30</MAIL_ID>
<MAIL_FROM>LEABENS</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>31</MAIL_ID>
<MAIL_FROM>LEABENS</MAIL_FROM>
</TS_Table0>
<TS_Table0>
<MAIL_ID>32</MAIL_ID>
<MAIL_FROM>LEABENS</MAIL_FROM>
</TS_Table0>
</NewDataSet>


When i changed the datatype of 'Mail Id' to int , my problem got solved. But i need to know the reason for this error.



Regards
Dominic
Dominic Abraham  Friday, September 18, 2009 1:30 PM
My compliments on your post, I had no trouble reproducing the exception with it. Bonnie got it right. The exception is pretty horrid, it doesn't give you a good chance to guess what might be wrong.

DataTable builds an index on the columns to make Select() queries fast. That index is sorted by value, then it uses a binary search to select the range of records that matches the query expression. You query on the Mail_Id column, its column type is string. The sorted index thus sorts by string value which makes "14" sort before "7".

The problem starts with your query expression, it looks like a numeric expression to the query parser, not a string. That changes the way the column value evaluator calculates the value of the column. It calculates it as a number, not a string. Now the index becomes invalid, 14 sorts after 7. A binary search cannot locate certain rows anymore, it was designed to only search properly sorted collections. That depends on the values that are in the rows, your sample xml data trips up the algorithm at Mail_Id = 14. It finds the first match at index 5 but not the last match. It should be index 5 as well but binary search returns -1, "not found". That trips an internal exception in the Range class which makes sure that the range cannot be invalid.

The solution is simple enough, make the query expression evaluator understand that you are looking for a string, not a number:

oDrDSlist = oDsFilter.Tables[0].Select("Mail_Id='" + oDr["Mail_Id"].ToString() + "'");

Note the added single quotes. An even better solution is to use the proper column type. Mail_Id quacks like a number, make the column type numeric.

Consider posting your issue to connect.microsoft.com, there ought to be a better exception for this. It has to be a common mistake.

Hans Passant.
nobugz  Saturday, September 19, 2009 12:47 PM
My guess is because you're missing some quotes in your select. So, you have this:

oDrDSlist = oDsFilter.Tables[0].Select(" Mail_Id=" + oDr["Mail_Id"].ToString());

It probably should have been this:

oDrDSlist = oDsFilter.Tables[0].Select(" Mail_Id='" + oDr["Mail_Id"].ToString() + "'");

So, when your Mail_Id was an int instead of a string, you didn't need the quotes and it worked.


Try using string.Format. It's much clearer and less error-prone. So, back to the original string datatype, you'd use this:

oDrDSlist = oDsFilter.Tables[0].Select(string.Format(" Mail_Id='{0}'", oDr["Mail_Id"]));
~~Bonnie Berent [C# MVP]
BonnieB  Saturday, September 19, 2009 4:52 AM
My guess is because you're missing some quotes in your select. So, you have this:

oDrDSlist = oDsFilter.Tables[0].Select(" Mail_Id=" + oDr["Mail_Id"].ToString());

It probably should have been this:

oDrDSlist = oDsFilter.Tables[0].Select(" Mail_Id='" + oDr["Mail_Id"].ToString() + "'");

So, when your Mail_Id was an int instead of a string, you didn't need the quotes and it worked.


Try using string.Format. It's much clearer and less error-prone. So, back to the original string datatype, you'd use this:

oDrDSlist = oDsFilter.Tables[0].Select(string.Format(" Mail_Id='{0}'", oDr["Mail_Id"]));
~~Bonnie Berent [C# MVP]
BonnieB  Saturday, September 19, 2009 4:52 AM
Hi

Thank you for your reply.
This is a good solution. But my issue is not this.

I got the error when i used the previous code, as i noted.
But when i reduced the number of rows returning from the database or xml, there is no error is coming.
That means when i chaged number of rows from 32 to 10 , no error is coming. It looks strange.
What may be the reason.
Looking for your reply



Regards
Dominic Abraham
Dominic Abraham  Saturday, September 19, 2009 6:21 AM
My compliments on your post, I had no trouble reproducing the exception with it. Bonnie got it right. The exception is pretty horrid, it doesn't give you a good chance to guess what might be wrong.

DataTable builds an index on the columns to make Select() queries fast. That index is sorted by value, then it uses a binary search to select the range of records that matches the query expression. You query on the Mail_Id column, its column type is string. The sorted index thus sorts by string value which makes "14" sort before "7".

The problem starts with your query expression, it looks like a numeric expression to the query parser, not a string. That changes the way the column value evaluator calculates the value of the column. It calculates it as a number, not a string. Now the index becomes invalid, 14 sorts after 7. A binary search cannot locate certain rows anymore, it was designed to only search properly sorted collections. That depends on the values that are in the rows, your sample xml data trips up the algorithm at Mail_Id = 14. It finds the first match at index 5 but not the last match. It should be index 5 as well but binary search returns -1, "not found". That trips an internal exception in the Range class which makes sure that the range cannot be invalid.

The solution is simple enough, make the query expression evaluator understand that you are looking for a string, not a number:

oDrDSlist = oDsFilter.Tables[0].Select("Mail_Id='" + oDr["Mail_Id"].ToString() + "'");

Note the added single quotes. An even better solution is to use the proper column type. Mail_Id quacks like a number, make the column type numeric.

Consider posting your issue to connect.microsoft.com, there ought to be a better exception for this. It has to be a common mistake.

Hans Passant.
nobugz  Saturday, September 19, 2009 12:47 PM
Hi,


Thank you very much for your reply. It is very valuable information.






Regards
Dominic Abraham


Dominic Abraham  Tuesday, September 22, 2009 6:45 AM

You can use google to search for other answers

Custom Search

More Threads

• What event message is sent to a dialog, when clicking a dialog HEADER?
• Parse HTML from webBrowser control ??
• Error Message on Phone Dialer Startup
• Preservation of contents of the form in a database
• execute a common file in vb2005
• Undo Dataset/Datatable in memory
• returning a DataGridViewRow with set values
• insert into table values from diff two tables
• Office xp PIA Placing two documents together
• CheckBox.Text...text is misaligned?