TSFLLC
asked on
Clear/Populate combo box with cbo.SelectedValue instead of cbo.Text
Do you have to clear/populate a combo box in some other way than setting SelectedValue? I am currently receiving the same error in two places when I attempt to set SelectedValue on a combo box programatically instead of populating it by clicking on it. I can set it with cmbo.Text = x ... only that requires me to modify several stored procedures to include outer joins of other tables to get description fields. I would prefer not to do that.
The error is 'Specified argument was out of the range of valid values'.
It first occurs when I press the Clear/New button to clear the fields using ClearFields() function. It is here that I am attempting to set SelectedValue = 0. I have a Public function that inserts a row into the datatable before initializing the dataview for the combo box.
It occurs again if I click/update a PropertiesOwned combo box that when updated also updates the cmboSubdivision combo box with
' cmboSubdivision.SelectedVa lue = drv("subdivision.id").ToSt ring(). Here again, it will work properly if I populate it with the description using the Text property.
Below is my code:
dtSubdivision = GetSubdivisionDS().Tables( "tbl_subdi vision")
InsertRowIntoTable(dtSubdi vision, 0)
dvSubdivision = dtSubdivision.DefaultView
With cmboSubdivision
.DataSource = dvSubdivision
.ValueMember = "subdivision_id"
.DisplayMember = "subdivision_description"
End With
*First Error
Private Sub ClearFields()
cmboContact.SelectedValue = 0
cmboSubdivision.SelectedVa lue = 0
cmboPropertiesOwned.Select edValue = 0
cmboStreet.SelectedValue = 0
End Sub
*Second error
Private Sub cmboPropertiesOwned_Select edIndexCha nged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmboProperty.SelectedIndex Changed
If cmboPropertiesOwned.Select edIndex <> 0 Then
Dim drv As DataRowView = dvProperties(cmboPropertie sOwned.Sel ectedIndex )
cmboSubdivision.SelectedVa lue = drv("subdivision_id").ToSt ring
cmboContact.SelectedValue = drv("contact_id").ToString
cmboStreet.SelectedValue = drv("property_id").ToStrin g
End If
End Sub
Is it improper to attempt to clear a combo box using SelectedValue or populate a combo box with SelectedValue even though you have ValueMember set in the initialization of combo box properties?
Phil Tate
The error is 'Specified argument was out of the range of valid values'.
It first occurs when I press the Clear/New button to clear the fields using ClearFields() function. It is here that I am attempting to set SelectedValue = 0. I have a Public function that inserts a row into the datatable before initializing the dataview for the combo box.
It occurs again if I click/update a PropertiesOwned combo box that when updated also updates the cmboSubdivision combo box with
' cmboSubdivision.SelectedVa
Below is my code:
dtSubdivision = GetSubdivisionDS().Tables(
InsertRowIntoTable(dtSubdi
dvSubdivision = dtSubdivision.DefaultView
With cmboSubdivision
.DataSource = dvSubdivision
.ValueMember = "subdivision_id"
.DisplayMember = "subdivision_description"
End With
*First Error
Private Sub ClearFields()
cmboContact.SelectedValue = 0
cmboSubdivision.SelectedVa
cmboPropertiesOwned.Select
cmboStreet.SelectedValue = 0
End Sub
*Second error
Private Sub cmboPropertiesOwned_Select
If cmboPropertiesOwned.Select
Dim drv As DataRowView = dvProperties(cmboPropertie
cmboSubdivision.SelectedVa
cmboContact.SelectedValue = drv("contact_id").ToString
cmboStreet.SelectedValue = drv("property_id").ToStrin
End If
End Sub
Is it improper to attempt to clear a combo box using SelectedValue or populate a combo box with SelectedValue even though you have ValueMember set in the initialization of combo box properties?
Phil Tate
ASKER
Thank you Eric. I changed it for ClearFields() and it worked properly.
One question regarding programming appropriateness in VB.NET with clearing forms....Is it proper, or maybe even necessary to insert a blank row into your datatable in order for combo boxes to display empty when clearing your form?
But I still have the second instance of the error when I attempt to populate with a working subdivision_id. Any insight??
Thanks.
By the way, I haven't had a chance to look at the backcolor issue that is still open.
One question regarding programming appropriateness in VB.NET with clearing forms....Is it proper, or maybe even necessary to insert a blank row into your datatable in order for combo boxes to display empty when clearing your form?
But I still have the second instance of the error when I attempt to populate with a working subdivision_id. Any insight??
Thanks.
By the way, I haven't had a chance to look at the backcolor issue that is still open.
TSFLLC,
> .Is it proper, or maybe even necessary to insert a blank row into your
> datatable in order for combo boxes to display empty when clearing your form?
I don't like to have my forms bound to datatable. I prefer to have independant controls and I feed them (almost) manually (I have a kind of dynamic mechanism). That way, you don't need to add empty rows to datatable and delete it if the user cancel. I only add a row to a datatable if the user actually click a OK button.
> when I attempt to populate with a working subdivision_id.
Since this field seems to be numeric, have you tried to cast it?
cmboSubdivision.SelectedVa lue = convert.toint32(drv("subdi vision_id" ))
> .Is it proper, or maybe even necessary to insert a blank row into your
> datatable in order for combo boxes to display empty when clearing your form?
I don't like to have my forms bound to datatable. I prefer to have independant controls and I feed them (almost) manually (I have a kind of dynamic mechanism). That way, you don't need to add empty rows to datatable and delete it if the user cancel. I only add a row to a datatable if the user actually click a OK button.
> when I attempt to populate with a working subdivision_id.
Since this field seems to be numeric, have you tried to cast it?
cmboSubdivision.SelectedVa
ASKER
Eric,
This is what I have figured out. If I PHYSICALLY add a record to each of these tables and I use SelectedValue = 0 (because 0 is a valid ValueMember...blank record) everything works fine. Including populating the field with the code that was originally creating the second error.
If I programmatically add a row to the datatables using my InsertRowInTable function, I GET ERRORS.
What is improper about this code below?
Public Function InsertRowIntoTable(ByVal xDataTable As DataTable, ByVal xValue As Int32)
Dim dr As DataRow
dr = xDataTable.NewRow
dr(0) = xValue
xDataTable.Rows.InsertAt(d r, 0)
End Function
This is what I have figured out. If I PHYSICALLY add a record to each of these tables and I use SelectedValue = 0 (because 0 is a valid ValueMember...blank record) everything works fine. Including populating the field with the code that was originally creating the second error.
If I programmatically add a row to the datatables using my InsertRowInTable function, I GET ERRORS.
What is improper about this code below?
Public Function InsertRowIntoTable(ByVal xDataTable As DataTable, ByVal xValue As Int32)
Dim dr As DataRow
dr = xDataTable.NewRow
dr(0) = xValue
xDataTable.Rows.InsertAt(d
End Function
TSFLLC,
> I GET ERRORS.
Which error?
> I GET ERRORS.
Which error?
ASKER
I tried the convert.toint32 and I continue to get the error 'Specified argument was out of the range of valid values'.
TSFLLC,
> I GET ERRORS.
Which error?
The same error 'Specified argument was out of the range of valid values'.
Regarding your comment about adding empty rows. I am only adding a blank row to combo boxes that are populated from tables other than the main data entry table used in the form. I insert a row into one of those datatables, and then use a dataview to initialize the combo box. When the form is closed regardless of whether a record is added or cancelled in the data entry table the datatable and dataview is released right? I'm not commiting the addition of the record. Otherwise how do you get your combo boxes to display empty values when you clear the form. If I don't do this they ALL display the first value in the list as opposed to blank.
Since we're getting beyond the original scope of my post I'll push the points to 500 for your additional comments. Thanks!.
TSFLLC,
> I GET ERRORS.
Which error?
The same error 'Specified argument was out of the range of valid values'.
Regarding your comment about adding empty rows. I am only adding a blank row to combo boxes that are populated from tables other than the main data entry table used in the form. I insert a row into one of those datatables, and then use a dataview to initialize the combo box. When the form is closed regardless of whether a record is added or cancelled in the data entry table the datatable and dataview is released right? I'm not commiting the addition of the record. Otherwise how do you get your combo boxes to display empty values when you clear the form. If I don't do this they ALL display the first value in the list as opposed to blank.
Since we're getting beyond the original scope of my post I'll push the points to 500 for your additional comments. Thanks!.
TSFLLC,
> I tried the convert.toint32 and I continue to get the error 'Specified
> argument was out of the range of valid values'.
That means that the value is not part of combo. Do you see values into your combo? What is the value of : convert.toint32(drv("subdi vision_id" ))
>>Otherwise how do you get your combo boxes to display empty values when you clear the form
SelectedIndex = -1
> I tried the convert.toint32 and I continue to get the error 'Specified
> argument was out of the range of valid values'.
That means that the value is not part of combo. Do you see values into your combo? What is the value of : convert.toint32(drv("subdi
>>Otherwise how do you get your combo boxes to display empty values when you clear the form
SelectedIndex = -1
ASKER
Sorry Eric,
I am not receiving an error in my InsertRecordIntoTable function. I am receiving the error 'Specified argument was out of the range of valid values' when attempting to populate my cmboSubdivision using the cmboProperty_SelectedIndex Changed code when I programmatically add a row to the datatable using InsertRecordIntoTable instead of manually adding a record to each of the tables associated with the combo boxes.
Needed to clarify.
I am not receiving an error in my InsertRecordIntoTable function. I am receiving the error 'Specified argument was out of the range of valid values' when attempting to populate my cmboSubdivision using the cmboProperty_SelectedIndex
Needed to clarify.
sorry I don't get it!
on which line does the error occurs? what is the value of xValue?
on which line does the error occurs? what is the value of xValue?
ASKER
Eric,
>That means that the value is not part of combo. Do you see values into your combo? What is the value of : convert.toint32(drv("subdi vision_id" ))
This is the crazy thing about it. Through debugging and Watch I can see that drv("subdivision_id") is a valid value that I am using to set SelectedValue.
Also, if I use cmboSubdivison.Text = drv("subdivision_descripti on")... AKA for example "Azalea Trails" it works fine.
I can also (immediately following the line of code above) put Msgbox(cmboSubdivision.Sel ectedValue ) and it displays the original value from drv("subdivision_id").
The catch is if I have a physical record in my reference/combo box tables as opposed to programmatically temporarily adding the row into the datatable.
>That means that the value is not part of combo. Do you see values into your combo? What is the value of : convert.toint32(drv("subdi
This is the crazy thing about it. Through debugging and Watch I can see that drv("subdivision_id") is a valid value that I am using to set SelectedValue.
Also, if I use cmboSubdivison.Text = drv("subdivision_descripti
I can also (immediately following the line of code above) put Msgbox(cmboSubdivision.Sel
The catch is if I have a physical record in my reference/combo box tables as opposed to programmatically temporarily adding the row into the datatable.
Can you try to add:
YourDataSet.AcceptChanges
after you manually add a row to your datatable
YourDataSet.AcceptChanges
after you manually add a row to your datatable
ASKER
>YourDataSet.AcceptChanges
I thought possibly you were onto something there. I get the same error.
I thought possibly you were onto something there. I get the same error.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>sorry I don't get it!
>on which line does the error occurs? what is the value of xValue?
Here is what I am doing.
1) I set up a datatable for cbo1, cbo2, cbo3.
2) I using my function InsertRecordIntoTable() for each of cbo1, cbo2, cbo3. Within this function xValue = 0. Each of these tables have an ID bigint field. The empty row
3) I create a dataview for each combo box.
4) I set datasource of each combo box to the dataviews.
5) If I display existing record, I use PopulateFields sub using the following and it works fine...go figure!!
Private Sub PopulateFields(ByVal MainCurrRowIndex As Integer)
Dim dr As DataRowView = dvsInvoice(MainCurrRowInde x)
Try
cmboContact.Enabled = False
cmboSubdivision.Enabled = False
cmboProperty.Enabled = False
cmboStreet.Enabled = False
cmboContact.SelectedValue = dr("contact_id").ToString
cmboSubdivision.SelectedVa lue = dr("subdivision_id").ToStr ing
cmboProperty.SelectedValue = dr("property_id").ToString
cmboStreet.SelectedValue = dr("property_id").ToString
Catch ex As Exception
MessageBox.Show(ex.Message , MsgBoxStyle.OKOnly Or MsgBoxStyle.Critical, Me.Text)
End Try
End Sub
6) If after having displayed a valid record the Clear/New button is clicked and ClearFields() sub is executed I get the 'Specified argument was out of the range of valid values' error when I set SelectedValue = 0. If I change it to SelectedValue = -1 I have no error.
Private Sub ClearFields()
cmboContact.Enabled = True
cmboSubdivision.Enabled = True
cmboProperty.Enabled = True
cmboStreet.Enabled = True
cmboContact.SelectedValue = 0
cmboSubdivision.SelectedVa lue = 0
cmboPropertiesOwned.Select edValue = 0
cmboStreet.SelectedValue = 0
End Sub
7) If I update cmboProperty with a valid Property owned by someone, I attempt to update the subdivision, contact (the owner), and the street address combo boxes. If I update with the description fields ' cmboSubdivision.Text = drv("subdivision_descripti on") ' it works fine. If I attempt to update with SelectedValue (those ID bigint values I was referring to above) I get the same error.
Private Sub cmboProperty_SelectedIndex Changed(By Val sender As System.Object, ByVal e As System.EventArgs) Handles cmboProperty.SelectedIndex Changed
If cmboProperty.SelectedIndex <> 0 Then
Dim drv As DataRowView = dvProperties(cmboProperty. SelectedIn dex)
cmboSubdivision.SelectedVa lue = drv("subdivision_id").ToSt ring
cmboContact.SelectedValue = drv("contact_id").ToString
cmboStreet.SelectedValue = drv("property_id").ToStrin g
'cmboSubdivision.Text = drv("subdivision_descripti on").ToStr ing
'cmboContact.Text = drv("contact_index").ToStr ing
'cmboStreet.Text = drv("property_address").To String
End If
End Sub
>on which line does the error occurs? what is the value of xValue?
Here is what I am doing.
1) I set up a datatable for cbo1, cbo2, cbo3.
2) I using my function InsertRecordIntoTable() for each of cbo1, cbo2, cbo3. Within this function xValue = 0. Each of these tables have an ID bigint field. The empty row
3) I create a dataview for each combo box.
4) I set datasource of each combo box to the dataviews.
5) If I display existing record, I use PopulateFields sub using the following and it works fine...go figure!!
Private Sub PopulateFields(ByVal MainCurrRowIndex As Integer)
Dim dr As DataRowView = dvsInvoice(MainCurrRowInde
Try
cmboContact.Enabled = False
cmboSubdivision.Enabled = False
cmboProperty.Enabled = False
cmboStreet.Enabled = False
cmboContact.SelectedValue = dr("contact_id").ToString
cmboSubdivision.SelectedVa
cmboProperty.SelectedValue
cmboStreet.SelectedValue = dr("property_id").ToString
Catch ex As Exception
MessageBox.Show(ex.Message
End Try
End Sub
6) If after having displayed a valid record the Clear/New button is clicked and ClearFields() sub is executed I get the 'Specified argument was out of the range of valid values' error when I set SelectedValue = 0. If I change it to SelectedValue = -1 I have no error.
Private Sub ClearFields()
cmboContact.Enabled = True
cmboSubdivision.Enabled = True
cmboProperty.Enabled = True
cmboStreet.Enabled = True
cmboContact.SelectedValue = 0
cmboSubdivision.SelectedVa
cmboPropertiesOwned.Select
cmboStreet.SelectedValue = 0
End Sub
7) If I update cmboProperty with a valid Property owned by someone, I attempt to update the subdivision, contact (the owner), and the street address combo boxes. If I update with the description fields ' cmboSubdivision.Text = drv("subdivision_descripti
Private Sub cmboProperty_SelectedIndex
If cmboProperty.SelectedIndex
Dim drv As DataRowView = dvProperties(cmboProperty.
cmboSubdivision.SelectedVa
cmboContact.SelectedValue = drv("contact_id").ToString
cmboStreet.SelectedValue = drv("property_id").ToStrin
'cmboSubdivision.Text = drv("subdivision_descripti
'cmboContact.Text = drv("contact_index").ToStr
'cmboStreet.Text = drv("property_address").To
End If
End Sub
TSFLLC,
Have you tried my latest comment (the UNION in the SQL query)?
Have you tried my latest comment (the UNION in the SQL query)?
ASKER
Wow,
I guess I learned something just then. I am a virgin when it comes to the experience of many things one can do with SQL.
I know more basic select statements.
You just resolved the problem. The SelectedValue worked flawlessly.
You also just eleviated the problem of having to add a row in code as opposed to adding to the SQL statement.
It worked GREAT! I don't know why my original coding wouldn't work...but it doesn't matter.
This is by far the best practice.
Thanks Eric.
I guess I learned something just then. I am a virgin when it comes to the experience of many things one can do with SQL.
I know more basic select statements.
You just resolved the problem. The SelectedValue worked flawlessly.
You also just eleviated the problem of having to add a row in code as opposed to adding to the SQL statement.
It worked GREAT! I don't know why my original coding wouldn't work...but it doesn't matter.
This is by far the best practice.
Thanks Eric.
TSFLLC,
Your other method (dynamically adding a row) also works but I can't see from your code what's wrong.
Your other method (dynamically adding a row) also works but I can't see from your code what's wrong.
selectedindex = -1
Cheers!