Hi

Im devloping a form and i have used two combo boxes in it.. basically these combo boxes store numric code for them in databses[MS. ACCESS]... but i want to display relative string representation to user....i have populate items manually using collections in the propert window. but when i want to retrive these values from the database im unable to retrive value in the string format for a specific record . i dont know what property should i use to get the selected value for a specific record in the database. same problem occur for insert.. e.g for female employee i want to store 1 in database but want to female to user.... how can i do this... please help me...

thanks

Dani AI

Generated

For : the clean, maintainable solution is to separate what the user sees (text) from what you store (numeric code). Both WinForms and ASP.NET support binding a visible label and a hidden value so you never have to hard-code "if Female then 1" in events. correctly noted storing text works, but since you must keep numeric codes, and suggested mapping on SelectionChange, a binding approach is more robust and easier to maintain.

WinForms (VB.NET) pattern: populate the combo from a lookup table (ID, Label), set DisplayMember and ValueMember, then use SelectedValue to read/write the numeric code. Example:

' load once
ComboBox1.DataSource = GetLookupTable()  ' DataTable with ID and Name
ComboBox1.DisplayMember = "Name"
ComboBox1.ValueMember = "ID"

' save: numeric code to DB
Dim code As Integer = CInt(ComboBox1.SelectedValue)

' load a record: select by code to show the label
ComboBox1.SelectedValue = recordCode

ASP.NET WebForms (VB.NET) uses the same idea with DataTextField/DataValueField and SelectedValue:

ddlGender.DataSource = GetLookupTable()
ddlGender.DataTextField = "Name"
ddlGender.DataValueField = "ID"
ddlGender.DataBind()

Dim code As Integer = CInt(ddlGender.SelectedValue)

Troubleshooting tips: make sure SelectedValue is not Nothing before casting; if the lookup is bound and SelectedValue is not found SelectedIndex becomes -1; always use parameterized queries and pass the code as an integer (OleDbParameter/DbType.Int32 for Access) to avoid type issues. If you cannot add a lookup table, add ListItems with a value (ListItem("Female","1")) instead of string-only items. Microsoft docs for the ComboBox/DropDownList value/display fields are useful references: and .

Is it important that you represent female with 1? the database will accept the strings in the combo box so if the user chooses female it will appear as female in the db eliminating your problem i suppose

Yes its requirement... because i want to store numeric code for data and dont want to show it user and also want to display string representation to user and dont want to store it into database... please help me

Thanks

Hi maheen123,

if i have understood this properly, you have combo boxes with strings in them i.e. Female, Male and you want to insert a 1 if Female and a 2 if Male (for example) into the database.

if this is correct, then on the selectionchange of the combobox, read the text proprty and if Female write a 1 to the database table and a 2 if it is male (or whatever values you want to use).

i have some code for this if you require it?

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.