简体   繁体   中英

retrieving database value for a manual drop down list

So I have a manual drop down list that I added 3 items to (no data connection, no binding).

 **Text**                   **Value**
 blank space            blank space
 Dog                    6
 Cat                    7

I store the drop down list value as so

  mycommand.Parameters.AddWithValue("@ANIMAL", SqlDbType.Char).Value = ddlAnimals.SelectedItem.Text;

I retrieve the drop down list value as so

 ddlAnimals.SelectedItem.Value = mySqlDataReader[1].ToString();

The problem I am having is that although my drop down list populates with the correct Text (after I retrieve it), it duplicates the text in the list, and the text from the database does not have a value associated with it. If I return Dog, there is no value of 6. My drop down list, upon clicking looks like this:

   Dog (Currently Selected upon Click)
   Dog
   Cat

If I select the other Dog or the Cat Value, I retain my 6 and 7. But the original "Dog" has no value associated with the Text. What in the world am I doing wrong?

Before you are loading your dropdown list clear the items and then reload it.Like this :

ddlAnimals.Items.Clear();

Use ddlAnimals.SelectedValue to pass the parameters like this :

mycommand.Parameters.AddWithValue("@ANIMAL", SqlDbType.Char).Value = ddlAnimals.SelectedValue;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM