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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Priya LingePosted Nov 10, 2011, 4:38 AM
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.
Datta KharadPosted Nov 10, 2011, 4:10 AM
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.
Pravin MorePosted Nov 10, 2011, 4:08 AM
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.