Reputation: 15
So here's what's going on. I'm currently running VB via Visual Studio 2010. I'm creating a program for my team that is simple, but it's giving me the fits. The part I am stuck on right now is I have created an access Database with 2 columns. First column has usernames and the second column has their respective UserID. The premise is when they select their username from the ComboBox it populates the text box with their userID.
I got it to work when you run the program, but for whatever reason, if I select the first user being Peter Griffin, the field next to it populates PGriffin, if I select Bob Belcher, Archer pops up in the text box, yet, when I go back to Peter Griffin, false shows up in the text box and if I go back to Bob, false shows up for him. So it works the first time they select their name, but not the second time. Here's the code I'm working with:
Private Sub FillCombo()
Dim fillcon As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Jae\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplication1\SD Members1.accdb")
Dim query As String = ("SELECT User_Name, User_ID FROM Analysts")
Dim da As New OleDb.OleDbDataAdapter(query, fillcon)
Dim ds As New DataSet
da.Fill(ds)
ComboBox1.ValueMember = "User_Name"
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.SelectedIndex = 0
TextBox10.DataBindings.Clear()
TextBox10.DataBindings.Add("Text", ds.Tables(0), "User_ID")
End Sub
This is the query that runs on the combo box
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim dt As DataTable = ComboBox1.DataSource
Dim dr As DataRow = dt.Rows(ComboBox1.SelectedIndex)
TextBox10.Text = IIf(dr.IsNull("User_ID"), "", dr.IsNull("User_ID").ToString)
End Sub
Upvotes: 1
Views: 494
Reputation: 2451
The first time, the SelectedIndexChanged event may not be firing. I noticed in your iif, it's returning the result of isnull, which is False.
Try the following:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim dt As DataTable = ComboBox1.DataSource
Dim dr As DataRow = dt.Rows(ComboBox1.SelectedIndex)
TextBox10.Text = IIf(dr.IsNull("User_ID"), "", dr("User_ID").ToString)
End Sub
Edit: LarsTech did pose an interesting question. Do you need this event if you are binding that to the User_Id? Unless you need additional functionality, the binding should do the trick for you.
Upvotes: 0
Reputation: 81620
Get rid of your SelectedIndexChanged
event.
'Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
' Dim dt As DataTable = ComboBox1.DataSource
' Dim dr As DataRow = dt.Rows(ComboBox1.SelectedIndex)
' TextBox10.Text = IIf(dr.IsNull("User_ID"), "", dr.IsNull("User_ID").ToString)
'End Sub
You already have the DataBinding setup, so it should work without it.
Upvotes: 1