Link to home
Start Free TrialLog in
Avatar of TSFLLC
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.SelectedValue = drv("subdivision.id").ToString().  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_subdivision")
        InsertRowIntoTable(dtSubdivision, 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.SelectedValue = 0
        cmboPropertiesOwned.SelectedValue = 0
        cmboStreet.SelectedValue = 0
    End Sub

*Second error  
  Private Sub cmboPropertiesOwned_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmboProperty.SelectedIndexChanged
        If cmboPropertiesOwned.SelectedIndex <> 0 Then
            Dim drv As DataRowView = dvProperties(cmboPropertiesOwned.SelectedIndex)
            cmboSubdivision.SelectedValue = drv("subdivision_id").ToString
            cmboContact.SelectedValue = drv("contact_id").ToString
            cmboStreet.SelectedValue = drv("property_id").ToString
        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
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Hi TSFLLC,

selectedindex = -1

Cheers!
Avatar of TSFLLC
TSFLLC

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.

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.SelectedValue = convert.toint32(drv("subdivision_id"))
Avatar of TSFLLC

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(dr, 0)
    End Function
TSFLLC,
> I GET ERRORS.

Which error?
Avatar of TSFLLC

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 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("subdivision_id"))

>>Otherwise how do you get your combo boxes to display empty values when you clear the form

SelectedIndex = -1
Avatar of TSFLLC

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_SelectedIndexChanged 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.

sorry I don't get it!

on which line does the error occurs? what is the value of xValue?
Avatar of TSFLLC

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("subdivision_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_description")... AKA for example "Azalea Trails" it works fine.
I can also (immediately following the line of code above) put Msgbox(cmboSubdivision.SelectedValue) 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.
Can you try to add:
YourDataSet.AcceptChanges
after you manually add a row to your datatable

Avatar of TSFLLC

ASKER

>YourDataSet.AcceptChanges
I thought possibly you were onto something there.  I get the same error.
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TSFLLC

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(MainCurrRowIndex)
        Try
            cmboContact.Enabled = False
            cmboSubdivision.Enabled = False
            cmboProperty.Enabled = False
            cmboStreet.Enabled = False
            cmboContact.SelectedValue = dr("contact_id").ToString
            cmboSubdivision.SelectedValue = dr("subdivision_id").ToString
            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.SelectedValue = 0
        cmboPropertiesOwned.SelectedValue = 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_description") ' 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_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmboProperty.SelectedIndexChanged
        If cmboProperty.SelectedIndex <> 0 Then
            Dim drv As DataRowView = dvProperties(cmboProperty.SelectedIndex)
            cmboSubdivision.SelectedValue = drv("subdivision_id").ToString
            cmboContact.SelectedValue = drv("contact_id").ToString
            cmboStreet.SelectedValue = drv("property_id").ToString
            'cmboSubdivision.Text = drv("subdivision_description").ToString
            'cmboContact.Text = drv("contact_index").ToString
            'cmboStreet.Text = drv("property_address").ToString
        End If
    End Sub

TSFLLC,

Have you tried my latest comment (the UNION in the SQL query)?
Avatar of TSFLLC

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.
TSFLLC,

Your other method (dynamically adding a row) also works but I can't see from your code what's wrong.