[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Clear/Populate combo box with cbo.SelectedValue instead of cbo.Text

Posted on 2006-05-18
17
Medium Priority
?
1,053 Views
Last Modified: 2008-03-17
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
0
Comment
Question by:TSFLLC
  • 9
  • 8
17 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16710593
Hi TSFLLC,

selectedindex = -1

Cheers!
0
 

Author Comment

by:TSFLLC
ID: 16710741
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.

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16710806
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"))
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:TSFLLC
ID: 16710883
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
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16710908
TSFLLC,
> I GET ERRORS.

Which error?
0
 

Author Comment

by:TSFLLC
ID: 16711021
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!.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16711049
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
0
 

Author Comment

by:TSFLLC
ID: 16711053
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.

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16711083
sorry I don't get it!

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

Author Comment

by:TSFLLC
ID: 16711105
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.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16711143
Can you try to add:
YourDataSet.AcceptChanges
after you manually add a row to your datatable

0
 

Author Comment

by:TSFLLC
ID: 16711205
>YourDataSet.AcceptChanges
I thought possibly you were onto something there.  I get the same error.
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 2000 total points
ID: 16711259
TSFLLC,

>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

It is working for physical records? Instead of dynamically adding rows, I often use a UNION clause to add a dummy entry to a datatable:

select subdivision_id, subdivision_description from tbl_subdivision
UNION
select -1, NULL
ORDER BY 1
0
 

Author Comment

by:TSFLLC
ID: 16711309
>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

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16711346
TSFLLC,

Have you tried my latest comment (the UNION in the SQL query)?
0
 

Author Comment

by:TSFLLC
ID: 16711398
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.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16711438
TSFLLC,

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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question