Results 1 to 9 of 9

Thread: Getting OutofRange error after closing a program

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2020
    Posts
    21

    Getting OutofRange error after closing a program

    To display data in a textbox I used
    TextBox1.Text = BindingSource.Item(0)(0)

    The textbox was able to display the data but after I close the program I get this error

    System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index'

    Correction: The error doesn't just appear when I close the program, it also appears when I simply close the form.
    Last edited by WatsonJohn; Apr 18th, 2021 at 03:10 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Getting OutofRange error after closing a program

    Is it really appropriate to be using that code at all? The idea with a BindingSource is that you bind it. Is there a good reason that you're not binding the TextBox to the BindingSource?

    Apart from that, the obvious solution to the problem of that code throwing an exception when the form is closed is to not execute that code when the form is closed. How exactly you do that is not clear because you haven't bothered to provide us with any relevant information relating to that.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2020
    Posts
    21

    Re: Getting OutofRange error after closing a program

    What would we the appropriate replacement to quickly call a data item? When I choose an combobox item, the data pertaining to that item would appear on textboxes. I didn't use data binding on the textbox itself because there is a weird phenomenon where when I change the text in the textbox, the text in the combobox would also change, and I plan un utilizing the textbox.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Getting OutofRange error after closing a program

    That's not a weird phenomenon. That's an indication that you did it wrong. As you haven't told us what you did, it's hard to say what was wrong with it. You still haven't provided us with a FULL and CLEAR explanation of what you're doing here either, so I'm just going to provide general advice.

    Let's say that you have a Parent table and a Child table where the latter has a ParentId column that is a foreign key to the former. When you display child records, you can use TextBoxes to display the text fields and a ComboBox to display a data field from the related Parent record instead of the ParentId:
    vb.net Code:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.     Dim data As New DataSet
    3.     Dim parentTable = data.Tables.Add("Parent")
    4.     Dim childTable = data.Tables.Add("Child")
    5.  
    6.     With parentTable.Columns
    7.         .Add("ParentId", GetType(Integer))
    8.         .Add("ParentName", GetType(String))
    9.     End With
    10.  
    11.     With parentTable.Rows
    12.         .Add(1, "Parent1")
    13.         .Add(2, "Parent2")
    14.         .Add(3, "Parent3")
    15.     End With
    16.  
    17.     With childTable.Columns
    18.         .Add("ChildId", GetType(Integer))
    19.         .Add("ParentId", GetType(Integer))
    20.         .Add("ChildName", GetType(String))
    21.     End With
    22.  
    23.     With childTable.Rows
    24.         .Add(1, 3, "Child1")
    25.         .Add(2, 1, "Child2")
    26.         .Add(3, 2, "Child3")
    27.     End With
    28.  
    29.     data.Relations.Add("ParentChild", parentTable.Columns("ParentId"), childTable.Columns("ParentId"))
    30.  
    31.     parentBindingSource.DataSource = parentTable
    32.     childBindingSource.DataSource = childTable
    33.  
    34.     With parentComboBox
    35.         .DisplayMember = "ParentName"
    36.         .ValueMember = "ParentId"
    37.         .DataSource = parentBindingSource
    38.     End With
    39.  
    40.     childNameTextBox.DataBindings.Add("Text", childBindingSource, "ChildName")
    41. End Sub
    You can then navigate the child data by associating a BindingNavigator with the appropriate BindingSource or use your own navigation controls and call the appropriate methods.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2020
    Posts
    21

    Re: Getting OutofRange error after closing a program

    I had data binding on my combobox with the choosing of an item changes the textbox.

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    TextBox1.Text = TableBindingSource.Item(0)(0)
    End Sub

    The code works in such that the textbox would display the the item but after closing the form the error would occur.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Getting OutofRange error after closing a program

    I missed something in that previous example:
    vb.net Code:
    1. parentComboBox.DataBindings.Add("SelectedValue", childBindingSource, "ParentId")

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2020
    Posts
    21

    Re: Getting OutofRange error after closing a program

    What do you think is wrong with the code? Why would it get the item but cause outofbounds error?

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Getting OutofRange error after closing a program

    The problem is with the way you're binding. Here's the correct way. Bind your controls when you bind your ComboBox...

    Code:
    TextBox1.DataBindings.Add("Text", datasource, "field")

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Getting OutofRange error after closing a program

    Quote Originally Posted by .paul. View Post
    The problem is with the way you're binding. Here's the correct way. Bind your controls when you bind your ComboBox...

    Code:
    TextBox1.DataBindings.Add("Text", datasource, "field")
    The problem is that he's not binding at all, but rather moving data around manually. That manual code is being executed on the SelectedIndexChanged event and that event is raised when the ComboBox is unbound when the form closes. If binding is used then there's no manual code so there's no exception.

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