Get value from dropdownlist

  • Thread starter Thread starter Przemek
  • Start date Start date
P

Przemek

Hi,

I've populated my dropdown list from database (category table) via
datatable. It's showing categories in strings. The problem appears,
when I want to add new record to database (entry_table). I'm using
stored procedures and everything works fine until I want to insert
Category ID into entry_table.

I'm getting the selected item from drop down list:

category = myDropDownList.SelectedItem.Value.ToString

and using Select Case statement I'd like to set up valid ID:

Select Case categoryValue
Case categoryValue = "Economy"
category = 1
Case categoryValue = "Politics"
category = 2
End Select

but, string from dropdownlist contains whitespace. I've tried .Trim
function but it didn't work too. Is there any solution for my problem?
Or is there any other way to get back correct IDs?

Przemek
 
The value from the dropdownlist contains exactly what you put in it when
you populate it.

What is the data type in the database? If you are using the archaic data
type char instead of varchar, it would be padded with spaces.

The value is not what is displayed in the dropdownlist. Why don't you
put the id in the value instead of the text?
 
populate the dropdown with TEXT and VALUE

TEXT is what the user will read from the dropdownlist
VALUE is the value that you'll retrieve from dropdownlist.selectedvalue

if you do not populate like this, the dropdown value = dropdown text
 
Back
Top