Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15754

Value does not fall within the expected range.

$
0
0
I've run into a problem I'm having a hard time getting a purchase on, so I thought I'd ask here.

I have a very simple form that has nothing but a listbox and two buttons. In the constructor, I query a DB to get some values into a datatable, then bind that to the listbox. I then go through the datatable to see whether or not a stored value happens to be in there, and if it does, I set that to be the selected item in the listbox.

What I've found is that, if the table in the DB is empty, then I get the error that is the subject of this thread (Value does not fall within the expected range.) at some point during setup. This doesn't happen during the constructor when all the work I described is done. The datatable comes back with no rows, it gets bound to the listbox, the code then tries to find that selected value in the rows, but there are no rows, so it doesn't find it. All that is as it should be.

Following the constructor, there is something utterly trivial in the Load event handler. All I am doing is checking a Boolean value and showing a message if it is false. That works as expected, too. The Boolean is true, the messagebox doesn't show, execution steps to the End If....and the next thing that happens is the error message.

There is nothing in the Shown event handler, and there is no code that I wrote between the end of the Load handler and when the error shows up. For that matter, there is barely any more code in the whole form, as all that is happening is that the user makes a choice from the listbox, then the form goes away.

One thing I did to test this out is to put a record into the table that is queried, and the form then works as expected. That shows that this error has something to do with the fact that the datatable bound to the listbox has no rows, but it doesn't really suggest much to me for a solution. Technically, with no choices available, the form is kind of pointless, so there are ways to get around the problem. Still, there are times when that table will have no rows, and I'm curious as to what is causing this error. It isn't at all clear to me why this particular error would result from this situation. Which value? What range? If there is something wrong with binding an empty datatable to a listbox, there shouldn't be, and why this error anyways?

The one thing that seems possible is that the value member in the binding is set to a column in the datatable that should hold GUIDs, though there is no indication that the field should be any particular type.

Any suggestions as to what is causing the error?

Viewing all articles
Browse latest Browse all 15754

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>