retrieving the value from a drop down list

i am populating a drop down list on my web page from a database like this:
mySelect.Datasource = Ds1.tables("company")
<asp:DropDownList id="mySelect" datavaluefield="companyid" datatextfield="companyname" runat="server"/>
I want to retrieve the value that is chosen in this dropdown list and write it, along with other values from the web form... to the database

I currently have this line of code todo this:
Row.Item("companyID") = mySelect.selecteditem.value

However when i hit the submit button, rather than writing the value i have selected it writes the first value in the dropdown list...

Please could you tell me what i should be doing to get the value i select on the page to write to the DB rather than the first in the list


Who is Participating?
Put the code where you put the data into the drop down list into your page load sub in a postback statement like:

Sub Page Load...

If Not IsPostBack Then
<populate the dropdownlist>
End if

End Sub

A post back is when the page gets refreshed.  So, the above code would only populate the list on the first load.
You need to set the text and value fields from the appropriate columns in your database.
ddlCompName.DataSource = dsTemp  '<--- (Dataset Name)
ddlCompName.DataMember = "Results" '<--- (Table Name)
ddlCompName.DataTextField = "Comp_Name" '<---    (Column Name)
ddlCompName.DataValueField = "Comp_Serial" '<--- (Column Name)
lz7cjcAuthor Commented:
i thought i had done that
the html code appears as i would hope:
<select name="mySelect" id="mySelect">
      <option value="1">Huxley Associates</option>
      <option value="2">HW Group</option>
      <option value="3">Computer Futures</option>
      <option value="4">Inside-Solutions</option>
      <option value="5">Abraxas</option>

      <option value="6">Full Circle</option>
      <option value="7">Progressive</option>
      <option value="8">Microcom Ltd </option>
      <option value="9">Orbis Resourcing Ltd </option>
      <option value="10">Siena Red </option>
      <option value="11">Ideal People </option>

      <option value="12">ICAD Technology</option>
      <option value="13">Informatiq Consulting </option>
      <option value="14">Timothy James Consulting</option>


i.e the text and value fields are appearing on the page as i would hope... it is when i hit submit that
Row.Item("companyID") = mySelect.selecteditem.value
takes the first value in the dropdown list rather than the one selected in the form
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

have you tried: Row.Item("companyID") = mySelect.selectedvalue

without the .value at the end?
lz7cjcAuthor Commented:
just tried that and got an error as it seems to be taking the textfield value

"Couldn't store <Progressive> in consultantID Column. Expected type is Int32."

bennet is the display value... i want the value associated with bennett ... in this case 7 since the DB is expecting an integer not a string

thanks for the idea though
lz7cjcAuthor Commented:
i think the issue has something to do with the point at which the code is asking for the value ... by the time it assigns the value from the DDL to the DB the page has reloaded and the  DDL has already been reset and the default (first) value in the list is entered into the db

this is my guess as to what is happening... but then again what do i know

Does the data rebind itself during postback?
lz7cjcAuthor Commented:
is that a rhetorical question?
not sure - what would i need to do to ensure it does/doesn't

lz7cjcAuthor Commented:
thank you ... that worked a treat
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.