Help - Combobox & Binding Issue

Roey

Well-known member
Joined
Oct 10, 2002
Messages
238
Location
Canada
Been on this one all day to day and just cant seem to get it working !!! Hence this cry for help..


Have a form with one combobox and one text box bound to a dataset:
__________________________________________________
Try
dsCountry = objCountry.All call to middle-tier
Catch exc As Exception
MsgBox(exc.Message, , "error")
End Try

cboCountryCode.DataSource = dsCountry.Tables("Country")
cboCountryCode.DisplayMember = "CountryCode"
txtDescription.DataBindings.Add("Text", dsCountry.Tables ("Country"), "Description")
___________________________________________________

I want to be able to click a button called New:
___________________________________________________

Private Sub cmdNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNew.Click

blnNewTag = True

Me.BindingContext(dsCountry.Tables("Country")).EndCurrentEdit()
___________________________________________________

To indicate that I wish to add a new record. And then type in the new information into the combobox and the textbox and save it, using the Save button:
___________________________________________________

Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click

Dim NewRow As DataRow = dsCountry.Tables("Country").NewRow() retrieves a copy of the row structure

NewRow.Item("CountryCode") = cboCountryCode.Text
NewRow.Item("Description") = txtDescription.Text
dsCountry.Tables("Country").Rows.Add(NewRow)
BindingContext(dsCountry.Tables("Country")).EndCurrentEdit()
Dim dsChanges As DataSet = dsCountry.GetChanges()

Try
If Not dsChanges Is Nothing Then
blnRetVal = objCountry.Update(dsChanges) writes changes to the database via the n-tier architecture
dsCountry.AcceptChanges() commits the changes
End If
Catch exc As Exception
dsCountry.RejectChanges()
MsgBox(exc.Message, , "error")

End Try

If blnRetVal = True Then MsgBox("Record Successfully Saved", , "error")
Me.BindingContext(dsCountry.Tables("Country")).Position = 0
___________________________________________________

The error I am getting is that a blank record is being added in place of the combobox text, but the textbox text is being added OK. I think its a problem with databinding, but just cant seem to work it out...


Any help would be gladly welcomed as I have a headache after looking at this stuff.


Thanks
 
Still not geting very far with this one have tried here VBCity, and Google, makes me feel that I am totally approaching this incorrectly....

Have changed the code for the New button to:
___________________________________________________
Private Sub cmdNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNew.Click

Me.BindingContext(dsCountry.Tables("Country")).AddNew()
___________________________________________________

And the Save button to:

___________________________________________________
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click

Me.BindingContext(dsCountry.Tables("Country")).EndCurrentEdit
Dim dsChanges As DataSet = dsCountry.GetChanges

Try
If Not dsChanges Is Nothing Then
blnRetVal = objCountry.Update(dsChanges)
dsCountry.AcceptChanges()
End If
__________________________________________________

The effect of these changes is that when the New button is clicked the form is cleared ready for a new entry. When I click the Save button the objCountry object calls the DataAdapter Update which in turn writes to the database.

The only issue is that the data enetered into the combobox is saved as a blank field, and the data entered into the textbox is saved correctly.
 
Back
Top