Getting next clicked value from combo box

gayamantra

New member
Joined
Sep 2, 2006
Messages
4
Programming Experience
Beginner
Hi,

I understand that I can use the following code to get the value of the chosen item from a combo box (windows application) i.e

Dim memName as String = cmbMembers.Text

But if the user chooses another value after his first choice the above code doesn't work.

I have 2 text boxes which retrieves some details from the database depending on the user's choice from the combo box. It works for the first time but then subsequent selections do not retrieve the details into the text boxes.

Will appreciate some advice.

Gaya
 
it depends on the how the code is set up.....
Got some you care to share? Might make it easier.

-tg
 
A bit long pls bear with it.

Dim memName AsString

'Code to bind data to the text boxes
Public
Sub BindData(ByVal mname AsString)
conn = New SqlConnection()
selectcmd = New SqlCommand()
selectda = New SqlDataAdapter()
conn.ConnectionString = strng
'Selecting member id

selectcmd.CommandText = "select member_id from Member where member_name=@name"
selectcmd.Connection = conn
selectcmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = mname
conn.Open()
Dim mem_id AsInteger = selectcmd.ExecuteScalar()

'Select member details using member id from previous query
selectcmd.CommandText = "select Address,TelNo from Member_contact where member_id=@member_id"
selectcmd.Parameters.Add("@member_id", SqlDbType.Int).Value = mem_id
selectda.SelectCommand = selectcmd
selectda.Fill(selectds, "Member_contact")
txtAdd.Text = selectds.Tables("Member_contact").Rows(0)(0).ToString()
txtTel.Text = selectds.Tables("Member_contact").Rows(0)(1).ToString()
EndSub

'Upon clicking SEARCH the code should get the selected value from the combo box and use it to populate the text boxes using BindData(memName)
PrivateSub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
memName = cmbMembers.Text
BindData(memName)
EndSub

'Not sure if this is reqd but it wasn't of any help.
PrivateSub cmbMembers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbMembers.SelectedIndexChanged
memName = cmbMembers.Text
EndSub
 
You should not be using the Text property in a ComboBox unless the DropDownStyle property is set to DropDown and the SelectedIndex is -1, which means that the user has not selected an item but rather typed a value free-hand, like a TextBox. You should be using the SelectedItem or the SelectedValue, and perhaps the getItemText method.

Having said that, you're not calling BindData from the SelectedIndexChanged event handler. Also, you shouldn't be creating new data access objects every time either. You should have a single connection and command created at the class level. Then all you need to do each time you need to change the data is set the value of the parameters. You can also get your data in a single query. There's no need to call that ExecuteScalar. Just execute a single query:
VB.NET:
SELECT mc.Address, mc.TelNo FROM Member m INNER JOIN MemberContact mc ON m.MemberID = mc.MemberID WHERE m.MemberName = @MemberName
It's not going to affect anthing directly but can I also suggest some consistency in your naming conventions? If you're going to use upper case characters to start names then do it for all names. If you're going to use upper case characters to start words then do it for all words and don't use underscores. If you're going to use underscores then don't use upper case letters. These names are horribly inconsistent: Address, TelNo, member_id, member_name, Member_contact. Pick a naming convention and stick to it.

address, telNo, memberID, memberName, memberContact
Address, TelNo, MemberID, MemberName, MemberContact
address, tel_no, member_id, member_name, member_contact
Address, Tel_no, Member_id, Member_name, Member_contact
 
Back
Top