Click here to Skip to main content
16,020,347 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, in my table i have two columns name and id, i want to display name field values in combobox that i already did, my point here is how to assign values to the combobox so that if the user selects "ABC" from combobox and click on submit button i want to send the ID value of the "ABC" not the name. for example i have

Name id
ABC 23
ASD 28
ZXC 30

So when the user click on submit button the program should send 23, not the name itself. what i did for this
I am using VB.NET

Thanks in advance
Posted

It does depend on how you've bound the data to the combobox, but you're probably looking for the Selected Value[^] property,

There are a number of properties that will help you when trying to determine what the user has selected - SelectedIndex, SelectedItem, SelectedText, SelectedValue

Add an event handler to the combobox to trap when the user selects an item & experiement with these properties to view the data associated with them
 
Share this answer
 
i am using ' cmbParent.SelectedItem.ToString = "ABC" ' but id not save and generate a SqlException
" Conversion failed when converting the varchar value 'ID ' to data type int. " so i want to save id value into database table, not text value.
 
Share this answer
 
use this, but code in c#

<asp:dropdownlist id="DropDownList1" runat="server" xmlns:asp="#unknown">
DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="ID">

<asp:sqldatasource id="SqlDataSource1" runat="server" xmlns:asp="#unknown">
ConnectionString="<%$ ConnectionStrings:Personal %>"
SelectCommand="SELECT [Name], [ID] FROM [Test_Tab]">

<asp:button id="Button1" runat="server" text="Button" onclick="Button1_Click" xmlns:asp="#unknown">

in code file

protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(DropDownList1.SelectedValue);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900