Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a vb.net winform project that includes a combobox with a list of country codes. On one form a country code is selected from this combobox and can be saved to a sql server database. On another form, a textbox shows the country code that was saved to the database from the combobox in the first form. I want to change this country code in the second form if it is wrong (i.e. update/edit the info). What I am trying to do is this, on the second form, when 'edit' is selected, the textbox with the country code is hidden and a combobox with all the country codes is shown. I want to show the country code in the hidden textbox in the now visible combobox, but at the moment it is blank when the combobox is shown. I have tried putting the info into the combobox like the textboxes straight from the database, e.g:

VB
cbCountryCode.Text = CStr(dataTable.Rows(0).Item(2))


but this does not work. I also need to keep the country codes in the combobox as the user will need to change the country code if it's wrong. Is there a way of doing this? I have tried searching for a solution on google, but I cannot find a solution to this. I have a work around where I don't let the user update the info if the combobox is blank, but I want the country code to be already there so that the user does not have to select a country code again if it is not wrong. Any help with this problem would be greatly appreciated.

EDIT:

datatable.Rows(0).Item(2)

holds a country code, for example, Ireland (+353), United Kingdom (+44) or U.S.A. (1). This is the code I have for calling the information from the database:

VB
sqlVisitorDetails = "SELECT * FROM visitorDetails WHERE idNumber=@idNumber"
    sqlCon.Open()
    sqlCmd = New SqlCommand(sqlVisitorDetails, sqlCon)
    sqlCmd.Parameters.AddWithValue("@idNumber", txtIdNumber.Text)

    dtVisitorDetails = loadDtVisitorDetails()

    txtFirstName.Text = CStr(dtVisitorDetails.Rows(0).Item(1))
    txtLastName.Text = CStr(dtVisitorDetails.Rows(0).Item(2))
    txtContactNumber.Text = CStr(dtVisitorDetails.Rows(0).Item(3))
    txtCountryCode.Text = CStr(dtVisitorDetails.Rows(0).Item(4))
    txtAddress.Text = CStr(dtVisitorDetails.Rows(0).Item(5))


The country code (e.g. 'Ireland (+353)') is stored in dtVisitorDetails.Rows(0).Item(4) and this is put into the text box txtCountryCode. When edit is clicked on the form, the text box txtCountryCode is hidden and the combobox cbCountryCode is visible (before edit is clicked txtCountryCode is shown and cbCountryCode is hidden). I then want the country code (in this case 'Ireland (+353)') to be shown in the cbCountryCode combo box. At the moment when the combobox is shown it is blank and the user has to choose a country code again, even if it's right. I hope this makes things clearer.
Posted
Updated 12-Aug-14 3:43am
v2

Please use the below code to add item to your combo box
ComboBox1.Items.Add(TextBox1.Text)

Instead of setting the text property of the combo box kindly add the items to it.
 
Share this answer
 
Comments
Member 10804519 12-Aug-14 8:20am    
This doesn't help me as the item is already in the combobox, I don't want to add it again.
ChauhanAjay 12-Aug-14 8:28am    
You can add a condition to check if value already exists or not. If it does not exists then add the values. Please find the code below.

If (Not ComboBox1.Items.Contains(TextBox1.Text)) Then
ComboBox1.Items.Add(TextBox1.Text)
End If
Sharmanuj 12-Aug-14 8:42am    
To show the selected value in the comboBox control from the data base use this

cbCountryCode.SelectedValue = dataTable.Rows(0).Item(2).ToString()
Member 10804519 12-Aug-14 9:01am    
Thanks @Sharmanuj but that doesn't work. @ Member 4507912 and @ChauhanAjay did ye read the question? I know it's long but it explains what I'm looking for and it explains the whole problem I have.
ChauhanAjay 12-Aug-14 9:04am    
Can you please provide the code for your forms so that the problem can be understood better.
In properties I had set the combobox DropDownStyle to DropDownList, I changed this to DropDown and used
VB
cbCountryCode.Text = CStr(dataTable.Rows(0).Item(2))
and that worked.
 
Share this answer
 
Comments
Kschuler 12-Aug-14 15:46pm    
If you wanted it to be the DropDownList...look into the .SelectedValue or .SelectedItem of the combobox instead of the .Text. (Which one you use depends on how you loaded the data in the first place.)
Member 10804519 13-Aug-14 4:17am    
I tried that but it didn't work either. My solution works fine now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900