Click here to Skip to main content
16,018,938 members
Please Sign up or sign in to vote.
1.60/5 (2 votes)
See more:
how to store company id by selecting company name in dropdownlist in asp.net


give clear explanation with example.







thanking u sir
Posted

Add dropdownlist with datatextfield and datavaluefield property in aspx page

<asp:DropDownList runat="server" ID="ddlCity" DataTextField="CityName" DataValueField="CityID"></asp:DropDownList>


Bind dropdownlist in page load inside !IsPostBack

C#
if (!IsPostBack)
            {
                //Bind Datatable to dropdownlist datasource property (probably in page_load inside !IsPostBack)
                //e.g datatable contain column CityID , CityName
                ddlCity.DataSource = GetCities();
                ddlCity.DataBind();

            }



access ID and name field using below code

C#
//get selected city ID
           string cityName = ddlCity.SelectedItem.Text;
           //get selected city ID
           int cityID = Convert.ToInt32( ddlCity.SelectedItem.Value);
 
Share this answer
 
Hi Shanavaz,
When you fill your dropdownlist set its DataTextField and
DataValueField. In DataTextField bind Company Name, in DataValueField bind Company Id and while saving save its DataValueField.
 
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