Combobox Population (VB 2005 Express)

J

Jerry

Prewarning: I'm a newbie at VB. I just got done reading _Beginning
Visual Basic 2005_ (wrox) and I'm trying to put some of the things in
there to use.

I have a form where I can page through the records of a database, one
at a time. This form allows me to add new records, update records and
delete records. There are three items displayed on the form:
Application, Agency, and Database. Application is a databound textBox,
Agency and Database are databound comboBoxes. My data comes from a SQL
Server database with a table for each item.

tblApplication:
appID
appName
agID
dbID

tblDatabase:
dbID
dbName

tblAgency:
agID
agName

The initial view of the form works great. As I page through the
records, the comboBoxes show the correct data. When I click the New
button. This clears the Application textBox and it sets the two
comboBoxes' selectedIndex to 0.

Here's where the problem occurs. If I click the Add button or the
Cancel button, the form repopulates the fields. The Application textBox
displays the proper application name but the comboBoxes show the
selectedIndex of 0. This only occurs on the first record. The rest of
the records show the proper data. This is not being updated in the
database.

Below is the code I've been using to do this. It's lengthy but I cut
out what I thought was irrelevant. Can anyone point out to me what I'm
doing wrong and what I need to do to do it right?

********************************************************
Dim objConnection As New SqlConnection("ConnectionString")
Dim objCurrencyManager As CurrencyManager
Dim objDataAdapter As New SqlDataAdapter(SQL1, objConnection)
Dim objAGDataAdapter As New SqlDataAdapter(SQL2, objConnection)
Dim objDBDataAdapter As New SqlDataAdapter(SQL3, objConnection)
Dim objDataSet, objAGDataSet, objDBDataSet As DataSet
Dim objDataView, objAGDataView, objDBDataView As DataView


Private Sub frmUserInfo_Load(removed system args) Handles Me.Load
FillDataSetAndView()
BindFields()
End Sub


Private Sub FillDataSetAndView()
objCurrencyManager = CType(Me.BindingContext(objDataView),
CurrencyManager)

objDataSet = New DataSet()
objDataAdapter.Fill(objDataSet, "applications")
objDataView = New DataView(objDataSet.Tables("applications"))

objAGDataSet = New DataSet()
objAGDataAdapter.Fill(objAGDataSet, "agency")
objAGDataView = New DataView(objAGDataSet.Tables("agency"))

objDBDataSet = New DataSet()
objDBDataAdapter.Fill(objDBDataSet, "database")
objDBDataView = New DataView(objDBDataSet.Tables("database"))
End Sub


Private Sub BindFields()
txtApplication.DataBindings.Clear()
cboAgency.DataBindings.Clear()
cboDatabase.DataBindings.Clear()

txtApplication.DataBindings.Add("Text", objDataView, "appName")

cboAgency.DataBindings.Add("Text", objDataView, "agName")
cboAgency.DataSource = objDataView
cboAgency.DisplayMember = "agName"
cboAgency.ValueMember = "agID"

cboDatabase.DataBindings.Add("Text", objDataView, "dbName")
cboDatabase.DataSource = objDataView
cboDatabase.DisplayMember = "dbName"
cboDatabase.ValueMember = "dbID"

fillDropDowns()
End Sub


Private Sub fillDropDowns()
'this is to populate the comboBoxes from their database tables.

cboAgency.DataSource = objAGDataView
cboAgency.DisplayMember = "agName"
cboAgency.ValueMember = "agID"

cboDatabase.DataSource = objDBDataView
cboDatabase.DisplayMember = "dbName"
cboDatabase.ValueMember = "dbID"
End Sub


Private Sub btnNew_Click(removed system args) Handles btnNew.Click
cboAgency.SelectedIndex = 0
cboDatabase.SelectedIndex = 0
txtApplication.Clear()
txtApplication.Focus()
End Sub


Private Sub btnCancel_Click(removed system args) Handles
btnCancel.Click
Dim intPosition As Integer
intPosition = objCurrencyManager.Position

FillDataSetAndView()
BindFields()

objCurrencyManager.Position = intPosition
End Sub
********************************************************

Thanks for your suggestions!
 
J

Jerry

Jerry said:
Prewarning: I'm a newbie at VB. I just got done reading _Beginning
Visual Basic 2005_ (wrox) and I'm trying to put some of the things in
there to use.

I have a form where I can page through the records of a database, one
at a time. This form allows me to add new records, update records and
delete records. There are three items displayed on the form:
Application, Agency, and Database. Application is a databound textBox,
Agency and Database are databound comboBoxes. My data comes from a SQL
Server database with a table for each item.

tblApplication:
appID
appName
agID
dbID

tblDatabase:
dbID
dbName

tblAgency:
agID
agName

The initial view of the form works great. As I page through the
records, the comboBoxes show the correct data. When I click the New
button. This clears the Application textBox and it sets the two
comboBoxes' selectedIndex to 0.

Here's where the problem occurs. If I click the Add button or the
Cancel button, the form repopulates the fields. The Application textBox
displays the proper application name but the comboBoxes show the
selectedIndex of 0. This only occurs on the first record. The rest of
the records show the proper data. This is not being updated in the
database.

Below is the code I've been using to do this. It's lengthy but I cut
out what I thought was irrelevant. Can anyone point out to me what I'm
doing wrong and what I need to do to do it right?

Private Sub fillDropDowns()
'this is to populate the comboBoxes from their database tables.

cboAgency.DataSource = objAGDataView
cboAgency.DisplayMember = "agName"
cboAgency.ValueMember = "agID"

cboDatabase.DataSource = objDBDataView
cboDatabase.DisplayMember = "dbName"
cboDatabase.ValueMember = "dbID"
End Sub

I added cboAgency.SelectedValue =
BindingContext(objDataView).Current("agID") to the fillDropDowns() for
both cboAgency and cboDatabase and I get the effect I was looking for.
 
Top