Link to home
Start Free TrialLog in
Avatar of lz7cjc
lz7cjc

asked on

retrieving the value from a drop down list

Hi
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

thanks

Avatar of Torrwin
Torrwin
Flag of United States of America image

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)
Avatar of lz7cjc
lz7cjc

ASKER

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>

</select>

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
have you tried: Row.Item("companyID") = mySelect.selectedvalue

without the .value at the end?
Avatar of lz7cjc

ASKER

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
Avatar of lz7cjc

ASKER

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

thanks
Does the data rebind itself during postback?
Avatar of lz7cjc

ASKER

is that a rhetorical question?
not sure - what would i need to do to ensure it does/doesn't

thanks
ASKER CERTIFIED SOLUTION
Avatar of Torrwin
Torrwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lz7cjc

ASKER

thank you ... that worked a treat