Bound combobox update error (looking for a quick fix)

chad101

Member
Joined
Nov 21, 2007
Messages
10
Programming Experience
3-5
I can write some nasty unnecessary code to solve this problem. But, I would like to know the correct manner for fixing it.

Cut to the chase.

I have two comboboxes, one is bound to the other. So if I select a record in one combobox; only related records are displayed in the other. – This works fine

When I select a record in my first combobox and leave the first displayed value alone in my second combobox. I get a null value error when I update the database.

I noticed the SelectedIndexChanged Event never fires until I litterly select something from combobox # 2. When I do, the update works fine.

I want to update with the first item showing in combobox # 2 if I don't select another value.

'
'ComboBoxCustomer
'
Me.ComboBoxCustomer.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.objLabDemo, "tblLog.customerID"))
Me.ComboBoxCustomer.DataSource = Me.objLabDemo.tblCustomers
Me.ComboBoxCustomer.DisplayMember = "customer"
Me.ComboBoxCustomer.ValueMember = "CustomerID"
'
'ComboBoxPart
'
Me.ComboBoxPart.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.objLabDemo, "tblLog.partID"))
Me.ComboBoxPart.DataSource = Me.objLabDemo.tblCustomers
Me.ComboBoxPart.DisplayMember = "tblCustomerstblPartNumbers.partnum"
Me.ComboBoxPart.ValueMember = "tblCustomerstblPartNumbers.partId"

EDIT:
I found a post on here that was not resolved. He had the same problem I am trying to figure out
http://www.vbdotnetforums.com/showthread.php?t=18271
 
Last edited:
You could try setting the selected index property to 0 when you bind the second combobox, as this way something will always be selected as opposed to a null reference (or similar) error.
 
Hello Jon,

Thanks, I tried your idea and my second combo box is still throwing an error.

The combo box continues to go "blank" and an exception for using a null value is triggered at the update line for my data adaptor
OleDbDataAdapter1.Update(ChangedRows)
 
I'm very pissed off at myself...:rolleyes:

Before I state why, I want to say I try to do things the right way the first time. Don't take that statement as me saying: "I'm perfect". I'm not; I'm just a newb still learning who doesn’t want to invoke bad coding habits. In other words I do my best to make things work with the smallest amount of code needed, in a clean, readable manner.

To be honest I fear posting code to avoid ridicule lol. 90% of my com sci degree was based solely on C++. So VB.NET is very foreign. C++ was only good for studying data structures and learning program logic. Nobody uses it in my neck of the woods (i.e. employers).

I still don't understand "why" my combobox was switching to a blank (null) value when I update. That will continue to drive me nuts. Who knows, I'm sure I will see a thread or "common programming errors" block in a book some day and say "ahh!"

I was unaware VB(.NET) had two properties for retrieving the displayed text in comboboxes (i.e. selectedText and Text). I’m not sure how unorthodox this is in the ADO programming field. But, in my update function, I simply assigned the Text property to the SelectedText property. Not much code to it and it’s pretty straight forward.

VB.NET:
[COLOR="Red"]Dim strPartNumber As String
strPartNumber = Me.ComboBoxPart.[COLOR="Blue"]Text[/COLOR][/COLOR]

Me.BindingContext(objLabDemo, "tblLog").EndCurrentEdit()
objDataSetChanges = CType(objLabDemo.GetChanges, LabDatabaseDemo.LabDemo)
'Check to see if any changes have been made.
If (Not (objDataSetChanges) Is Nothing) Then
        If(newrecord)
                [COLOR="red"]Me.ComboBoxPart.[COLOR="Blue"]SelectedText[/COLOR] = strPartNumber[/COLOR]
        End If
.
.
.
 
Back
Top