How to Hide One Of The Arrays In A Split Function

PRAISE PHS

Well-known member
Joined
Jun 2, 2011
Messages
58
Programming Experience
Beginner
Hi All,
I have two columns named ItemName and ItemID on my DB(sqlserver). The ItemID is a primary key and also on auto increment. Then I loaded the ItemName and ItemID on a combo box from my DB in my front end by using a split function.
An example of how my combo box is loaded is illustrated below :

Camera-1
Phone-2
Laptop-3
DVD-4

I'm actually using the ItemIDs' (1,2,3 and 4) to track some information
My question is : How do I load the combo box like what is above but making sure that the ItemIDs' are hidden and still being able to track info with the IDs' as they are hidden.
I want something like :

Camera
Phone
Laptop
DVD

While making sure they all have their ItemIDs' attached to them.

Thanks in anticipation of your responses.
 
I'm not quite how using Split did that because you are joining two values, not splitting anything.

Anyway, a ComboBox is designed specifically to do what you want, i.e. display one set of values to the user while using a corresponding set of values in code. Assuming that you're using a query like this:
VB.NET:
SELECT ItemID, ItemName FROM SomeTable
to populate a DataTable, you can simply bind that DataTable to your ComboBox like so:
With myComboBox
    .DisplayMember = "ItemName"
    .ValueMember = "ItemID"
    .DataSource = myDataTable
End With
When the user makes a selection from the list of ItemName values, you can get the corresponding ItemID value from the SelectedValue property of the ComboBox. Note that that property is type Object because it has to be able to return any type of object. That means that you will need to cast it as its actual type to use it as such, for which you would use CInt if your ItemID is an Integer.

If you don't know how to populate a DataTable then check this out:

Retrieving and Saving Data in Databases
 
Back
Top