Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello i am having a dropdown list and i am trying to get the address on selection of empname ..help meplease i am new to programming
Posted
Comments
george4986 28-Feb-14 23:45pm    
are u getting value on dropdown selected index change?
if yes, use the value to filter ur dt using rowfilter or linq. then show the address to the required field

Poste the code?
Set the dropdown to autopostback and add event to onselectedindexchanged and use the selected index to dig out the value, that's the most reliable approach

for example:
Web page

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Dropdown dynamics sample</title>
</head>
<body>
    <form id="DemoForm" runat="server">
    <div>
        <asp:DropDownList id="CustomerSelect" runat="server" AutoPostBack="true"
            onselectedindexchanged="CustomerSelect_SelectedIndexChanged">
            <asp:ListItem Value="0">Please select</asp:ListItem>
        </asp:DropDownList>
         <label id="CustomerActive" runat="server"></label>
    </div>
    </form>
</body>
</html>


and code behind

C#
public partial class DropDownSample : System.Web.UI.Page
    {
        protected struct CustomerData
        {
            public int CustomerID;
            public string Name;
            public string StreetAndNumber;
        }

        private Dictionary<int, CustomerData> _dictionary = new Dictionary<int, CustomerData>();

        protected void Page_Init(object sender, EventArgs e){
            _dictionary.Add(1, new CustomerData { CustomerID = 1, Name = "Bill Spectre", StreetAndNumber = "Looneystreet 1" });
            _dictionary.Add(2, new CustomerData { CustomerID = 2, Name = "Charlie P", StreetAndNumber = "Yazzlane 2" });
            _dictionary.Add(3, new CustomerData { CustomerID = 3, Name = "Dick Di Spenser", StreetAndNumber = "Whatyerroad 3" });
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //CustomerSelect.Items.Add(new ListItem("Please select", "0"));
                foreach (var customer in _dictionary)
                {
                    CustomerSelect.Items.Add(new ListItem(customer.Value.Name, customer.Value.CustomerID.ToString()));
                }
            }
        }

        protected void CustomerSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            int idx = CustomerSelect.SelectedIndex;
            if (idx == 0)
            {
                CustomerActive.InnerText = "Please select from dropdown, one of the names";
                return;
            }
            var cust = _dictionary[idx];
            CustomerActive.InnerText = cust.StreetAndNumber;
        }
    }
 
Share this answer
 
v2
Comments
vivek0041 28-Feb-14 13:07pm    
i have done that i am getting the employee name but ...i want to show emp address on select of empane name ...i have label on which i have display that address on selection of empname
vivek0041 28-Feb-14 13:08pm    
employee name and address are in the same table
This would load Employee Names in the dropdown list.
C#
empDropDownList.DataSource = empDt;      //empDt is a DataTable that contains all the data of Employee, 
empDropDownList.DataValueField = "EmpID";      // your unique key
empDropDownList.DataTextField = "EmpName";
empDropDownList.DataBInd();

now write the query following way to get the data of the employee based on the name,
C#
string query  = "SELECT * FROM Employee WHERE EmpID=" + int.parse(empDropDownList.SelectedValue);
SqlDataAdapter da = new SqlAdapter(query, connnectionObject);
DataTable dt = new DataTable();
da.Fill(dt);
addressLbl.Text = dt.Rows[0]["Address"].ToString();


-KR
 
Share this answer
 
Comments
vivek0041 5-Mar-14 1:44am    
hello dear i am getting error ...no row at position 0
Krunal Rohit 5-Mar-14 8:54am    
can you post your code here ?

-KR

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