• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

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

0
lz7cjc
Asked:
lz7cjc
  • 5
  • 3
1 Solution
 
TorrwinCommented:
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)
0
 
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>

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

without the .value at the end?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
0
 
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

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

thanks
0
 
TorrwinCommented:
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.
0
 
lz7cjcAuthor Commented:
thank you ... that worked a treat
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now