hi frnds ,
i have a combobox containing countries name and their codes like INDIA:CNT001
then i have a textbox that store the code of country i.e CNT001(fetching from database)
Now i want to match that code with the combobox code so that the combobox is set to that item when i click a button.
i m using vb.net and sql server as database.

Dani AI

Generated

Short summary and the simplest, most robust fix for : avoid fragile string parsing when possible and use data-binding. If the ComboBox is bound to a table that contains separate columns for country name and country code, set the ComboBox DisplayMember/ValueMember and then assign SelectedValue to the code stored in the TextBox. That picks the correct item directly and is less error-prone than looping and substring operations.

' Data-bound (WinForms)
CbCategory.DisplayMember = "CountryName"
CbCategory.ValueMember = "CountryCode"
CbCategory.DataSource = countriesTable   ' DataTable or BindingSource
CbCategory.SelectedValue = TxtCategory.Text.Trim()

If the ComboBox contains plain strings like "INDIA:CNT001" (unbound), extract the code reliably and use a zero-based loop. The original snippet in this thread used For k = 1 To Items.Count (off-by-one) and compared the wrong value; that will either skip the first item or raise an index error. Use LastIndexOf or Split, Trim, and a case-insensitive comparison, then Exit For once matched:

' Unbound items example
Dim wanted As String = TxtCategory.Text.Trim()
For i As Integer = 0 To CbCategory.Items.Count - 1
    Dim s As String = CStr(CbCategory.Items(i))
    Dim colon As Integer = s.LastIndexOf(":"c)
    Dim codePart As String = If(colon >= 0, s.Substring(colon + 1).Trim(), s.Trim())
    If String.Equals(codePart, wanted, StringComparison.OrdinalIgnoreCase) Then
        CbCategory.SelectedIndex = i
        Exit For
    End If
Next

Quick ASP.NET note (DropDownList): set DataTextField/DataValueField, bind, then use If ddl.Items.FindByValue(code) IsNot Nothing Then ddl.SelectedValue = code. Troubleshooting checklist: trim whitespace, match types (string vs integer), set DisplayMember/ValueMember before or at least ensure DataSource is assigned before setting SelectedValue, and always use zero-based indexing when iterating ComboBox.Items. This complements 's binding suggestion while fixing the indexing and comparison issues observed in 's sample.

Recommended Answers

All 5 Replies

Goodmorning, this is very simple, you have only to add a Data BindingSource, and a Data BindingNavigator, connect them, and try it... if it doesn't worked then post and i will find one other solution. Remember to connect the combobox and textbox with Data BindingSource

plz tell me the procedure to do that...

oke..
so go to the Toolbox Panel and add Binding Source in DATA Collapsible Panel, then go to Proprty Panel and modify in Data the property:
DataSource: None
click on that and click on "Add Project Data Source..."
then you have many option, click next, next, New connection, Browse, *Load the Database File*, Ok, Next, Yes, Finish, *select all tables*, Finish, and you have integrated the database in your project.
See the images that I attached to help you....
Now select the combobox and modify his property
Data Source: None
select the table and connect the combobox with the database
Re do same thing with the textbox,
Now your Database is connected with Combobox and Textbox.
You can do this with everything, like label text, listbox items, progressbar value, everything....
Now add Data BindingNavigator, and modify his property:
Binding Source: None
to
Binding Source: and here the name of the BindingSource
it will gives you suggestion.
select the bindingsource
and try it!
If you need more help wait some time, and i will make a video tutorial for you... but i hope to be helpful here... but if you need for help post.. no problem

thnxs sir for ur concern ...but what i actually want to do will be clear wen u c the image tht i hav attached.i hav done som coding of my own but it seems not working.

Dim j As String = TxtCategory.Text.Substring(3, 3).ToString
        For k As Integer = 1 To CbCategory.Items.Count
           Dim q As String = (CbCategory.Items(k).ToString)
            If q = Microsoft.VisualBasic.Right(CbCategory.Text, 3) Then
               CbCategory.SelectedIndex = k
           End If
        Next

ah ok, so i suggest you to make the database with 2 column.
first: the Title of the product
second: the ID number of the product
now you have to delete the connection between combobox and the dtatasource (but don't delete any other connection) and write this code in Form Load:

For each Item In WriteHereDatabaseName.WriteHereDatabaseTableName
      combobox1.items.add(Item.NoCProduct + " (" + Item.NocProductID + ")")
Next

modify the connection between the textbox and datasource to the NocProductID
WriteHereDatabaseName = title of the database
WriteHereDatabaseTableName = title of the table in the database
NoCProduct = Name Of Column of Product
NoCProductID = Name Of column of Product ID

try now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.