Results 1 to 10 of 10

Thread: OutOfRange error when filling new data to clear datatable with table adapter.

  1. #1

    Thread Starter
    New Member Mighty Ferengi's Avatar
    Join Date
    May 2013
    Posts
    12

    OutOfRange error when filling new data to clear datatable with table adapter.

    Okay guys,

    I have a list box that is bound to a datatable. I have multiple queries set for the tableadapter and when the user selects a specific item from a listbox on another form, this form will open and offer selections from the datatable in the listbox. I have a subroutine set to check each time one of these specific items are selected so it will load the corresponding data into the listbox on the form that appears.

    The problem occurs when that subroutine is called to load the new data into the datatable. I get an OutOfRange error, stating that there is no row at -1. What's worse is that after the error occurs, I can step into it and the data loads correctly!

    Code:
    Public Sub Reset()
            Me.OCCIDDataSet.Skills_List.Clear()
            Start()
            SkillExtraSelect.Refresh()
        End Sub
    I assume this error is thrown because I have cleared the datatable bound to the listbox, however, if I unbind the datasource from the listbox, I receive the same error only the data does not load into the listbox.

    Code:
    Public Sub Reset()
            Me.SkillExtraSelect.DataSource = Nothing
            Me.OCCIDDataSet.Skills_List.Clear()
            Start()
            Me.SkillsListBindingSource.DataSource = OCCIDDataSet.Skills_List
            SkillExtraSelect.DataSource = SkillsListBindingSource
            SkillExtraSelect.Refresh()
        End Sub
    What is this error referring to? Is it the listbox data or is something else being called out? It doesn't affect how the program runs, it's just annoying and I hate to have errors.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: OutOfRange error when filling new data to clear datatable with table adapter.

    Are you sure that the error is happening there? The fact that the error is saying there is no row at -1 suggests that it might be elsewhere. After all, -1 is what the listbox.SelectedIndex would be if nothing is selected, where as an empty datatable would have a row count of 0, so the items in the listbox would also be 0. So, what I'm wondering is whether or not you are doing something with the SelectedIndexChanged event, or anything like that, which could be the real source of the problem. When the error occurs, it might take you to the row with the error, but it might also take you to the line where the form is shown.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member Mighty Ferengi's Avatar
    Join Date
    May 2013
    Posts
    12

    Re: OutOfRange error when filling new data to clear datatable with table adapter.

    Hey Shaggy,

    I tried resestting the listbox.SelectedIndex by setting it to Nothing, but no good. The error itself is pointing to the dataset.designer:

    Code:
     <Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
             Global.System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")>  _
            Public Default ReadOnly Property Item(ByVal index As Integer) As Skills_ListRow
                Get
                    Return CType(Me.Rows(index),Skills_ListRow)
                End Get
            End Property
    It specifically points to index (of course), but in looking through the debug, I can't tell where it's drawing the -1 from, nor what for. As I said, the error pops up to let me know what's going on, but the tableadapter fills the datatable correctly and the listbox shows the correct items in the list.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: OutOfRange error when filling new data to clear datatable with table adapter.

    You can't set an Integer to Nothing, it will revert to whatever it has to be. Normally, this would be 0, but since the SelectedIndex property of a Listbox can't be set to anything other than the selected Index, I would guess that the attempt to set it to Nothing would result in it being set to -1....again.

    When the listbox shows the correct items, is one of them selected? If so, then the SelectedIndex will be the ordinal for that item. Otherwise the SelectedIndex will be -1, regardless of what you tried to set it to. This is still the most likely source for that -1.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member Mighty Ferengi's Avatar
    Join Date
    May 2013
    Posts
    12

    Re: OutOfRange error when filling new data to clear datatable with table adapter.

    I set it to 0 before filling the datatable and got the same error. When the listbox shows, there is nothing selected.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: OutOfRange error when filling new data to clear datatable with table adapter.

    If this listbox is empty, then you can't set the SelectedIndex to 0 (or at least it will have no impact), and since you say that nothing is selected, then the SelectedIndex will be -1, and there's nothing that you can do that will change that. The SelectedIndex will be the index of the selected item no matter what, and if no item is selected, then the value is -1.

    However, that may or may not be the issue. It seems likely to be at least part of the issue, because where else would the -1 be coming from? However, I'm not clear on exactly which line is throwing that exception. It may be that all you have to do is suppress that particular error, but that's almost never a good idea. After all there is a horrible cost to throwing an exception. You may actually see a hesitation due to the exception, and that would be bad. A better solution would be to figure out exactly which line is throwing the exception, figure out why the exception is happening, and write that line differently such that the exception doesn't occur.

    Finding the line may not be all that easy. This seems like a situation where it is quite possible that the IDE will take you to the wrong line when the exception occurs, but which line does it take you to?

    Also, do you have any handlers for SelectedIndexChanged for the Listbox, or anything that references the SelectedIndex in code?
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    New Member Mighty Ferengi's Avatar
    Join Date
    May 2013
    Posts
    12

    Re: OutOfRange error when filling new data to clear datatable with table adapter.

    The error always points me here:

    Code:
    <Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
             Global.System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")>  _
            Public Default ReadOnly Property Item(ByVal index As Integer) As Skills_ListRow
                Get
                    Return CType(Me.Rows(index),Skills_ListRow)
                End Get
            End Property
    This doesn't help me because this is all in the Dataset Designer.

    As for the list box, it's only SelectedIndex reference is here:

    Code:
     Private Sub SkillExtraSelect_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SkillExtraSelect.SelectedIndexChanged
            Skills.SkillName = OCCIDDataSet.Skills_List(Me.SkillExtraSelect.SelectedIndex)(OCCIDDataSet.Skills_List.SkillNameColumn)
            Skills.SkillBase1 = OCCIDDataSet.Skills_List(SkillExtraSelect.SelectedIndex)(OCCIDDataSet.Skills_List.Base_1Column)
            Skills.SkillBase2 = OCCIDDataSet.Skills_List(SkillExtraSelect.SelectedIndex)(OCCIDDataSet.Skills_List.Base_2Column)
            Skills.SkillLevel = OCCIDDataSet.Skills_List(SkillExtraSelect.SelectedIndex)(OCCIDDataSet.Skills_List.Per_LevelColumn)
        End Sub
    I'm not calling on the listbox anywhere else in my program. This is literally all the listbox is for. Once an item is selected, the appropriate variable is filled and that's it. The next time a specific item is selected on another form, this form opens and this listbox is loaded with the appropriate data.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: OutOfRange error when filling new data to clear datatable with table adapter.

    Change the code in the SelectedIndexChanged event to this:
    Code:
    If Me.SkillExtraSelect.SelectedIndex<>-1 Then
     Skills.SkillName = OCCIDDataSet.Skills_List(Me.SkillExtraSelect.SelectedIndex)(OCCIDDataSet.Skills_List.SkillNameColumn)
     Skills.SkillBase1 = OCCIDDataSet.Skills_List(SkillExtraSelect.SelectedIndex)(OCCIDDataSet.Skills_List.Base_1Column)
     Skills.SkillBase2 = OCCIDDataSet.Skills_List(SkillExtraSelect.SelectedIndex)(OCCIDDataSet.Skills_List.Base_2Column)
     Skills.SkillLevel = OCCIDDataSet.Skills_List(SkillExtraSelect.SelectedIndex)(OCCIDDataSet.Skills_List.Per_LevelColumn)
    End If
    After all, anything that clears the listbox, will cause the SelectedIndex to change to -1. Since that will be a change to the selectedIndex, the event will fire, which means that you will then try to call this:

    OCCIDDataSet.Skills_List(Me.SkillExtraSelect.SelectedIndex)

    with a value of -1, which will cause an IndexOutOfRange exception, which is what you are getting (though you are being taken to the wrong place).
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    New Member Mighty Ferengi's Avatar
    Join Date
    May 2013
    Posts
    12

    Re: OutOfRange error when filling new data to clear datatable with table adapter.

    That did it! The Out of Index exception does not go off now. I didn't put it together that clearing the datasource was causing the SelectedIndex subroutine to fire. Thanks for your help, Shaggy!

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: OutOfRange error when filling new data to clear datatable with table adapter.

    I've always thought that to be an inefficiency in the event system, but there isn't any getting around it. Clearing the items in the listbox by any means will usually trigger a SelectedIndexChanged event back to -1. I'm not sure whether it would do that if the index already was -1 (I would think that it should not, but I'm not sure).
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width