Hi,
I want to fetch the address from the sql table for the selected customer in dropdownlist and display it in a label. How to do it?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pravin MoreRank #1,419 · 1.2K reputationPosted Nov 9, 2011, 11:08 PM EST
while binding cutomers to dropdownlist bind list of 2 columns custID,CustName and hide customerID means just show name and when user sselect Customer name you will get ID for that name in list so pass that iD to query like this
select Address from Customer where custID=ID of selected name
n then label1.Text=result of query.....
Thanks,
Pravin.
Datta KharadRank #2,135 · 668 reputationPosted Nov 9, 2011, 11:10 PM EST
It will get selected value of dropdownlist.
dropDownlist1.DataSource=ds.Tables[CustomerTab];
dropDownlist1.DataValueField="CustID";
dropDownlist1.DataTextField="CustName";
dropDownlist1.Databind();
//It will display customer name and whenever you selected custname then you will get CustId.
//For getting value
string sSQL="Select address from CustomerTab where CustID='"+DropDownList1.SelectedValue+"'";
----
------
-------
Please mark as Accepted answer if your query resolved.
Priya LingeRank #785 · 2.9K reputationPosted Nov 9, 2011, 11:38 PM EST
SqlConnection sqlConnection = new SqlConnection("Data Source=XYSERVER;Initial Catalog=pub4;Persist Security Info=True;User ID=trustfort;Password=Admin@123");
SqlCommand sqlCommand;
SqlDataReader sqlReader;
On DropDown SelectionIndexChanged Event retrive Address of customer from sql (Customer Table) related tht id:
DropDownList1.SelectedIndexChanged+=new EventHandler(DropDownList1_SelectedIndexChanged);
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
sqlConnection.Open();
string query = "select Address from Customer where ID =" + DropDownList1.SelectedItem.Value;
sqlCommand = new SqlCommand(query, sqlConnection);
sqlReader = sqlCommand.ExecuteReader();
while (sqlReader.Read())
{
Label1.Text = sqlReader[0].ToString();
}
sqlConnection.Close();
}
Hope this will help you.
Thanks.