-
Apr 18th, 2021, 12:07 AM
#1
Thread Starter
Junior Member
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.
-
Apr 18th, 2021, 12:13 AM
#2
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.
-
Apr 18th, 2021, 03:15 AM
#3
Thread Starter
Junior Member
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.
-
Apr 18th, 2021, 03:46 AM
#4
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:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim data As New DataSet
Dim parentTable = data.Tables.Add("Parent")
Dim childTable = data.Tables.Add("Child")
With parentTable.Columns
.Add("ParentId", GetType(Integer))
.Add("ParentName", GetType(String))
End With
With parentTable.Rows
.Add(1, "Parent1")
.Add(2, "Parent2")
.Add(3, "Parent3")
End With
With childTable.Columns
.Add("ChildId", GetType(Integer))
.Add("ParentId", GetType(Integer))
.Add("ChildName", GetType(String))
End With
With childTable.Rows
.Add(1, 3, "Child1")
.Add(2, 1, "Child2")
.Add(3, 2, "Child3")
End With
data.Relations.Add("ParentChild", parentTable.Columns("ParentId"), childTable.Columns("ParentId"))
parentBindingSource.DataSource = parentTable
childBindingSource.DataSource = childTable
With parentComboBox
.DisplayMember = "ParentName"
.ValueMember = "ParentId"
.DataSource = parentBindingSource
End With
childNameTextBox.DataBindings.Add("Text", childBindingSource, "ChildName")
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.
-
Apr 18th, 2021, 05:38 AM
#5
Thread Starter
Junior Member
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.
-
Apr 18th, 2021, 07:05 AM
#6
Re: Getting OutofRange error after closing a program
I missed something in that previous example:
vb.net Code:
parentComboBox.DataBindings.Add("SelectedValue", childBindingSource, "ParentId")
-
Apr 18th, 2021, 07:22 AM
#7
Thread Starter
Junior Member
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?
-
Apr 18th, 2021, 12:38 PM
#8
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")
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 18th, 2021, 07:50 PM
#9
Re: Getting OutofRange error after closing a program
Originally Posted by .paul.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|