Click here to Skip to main content
12,353,607 members (66,414 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET
i have dropdown list i select value from dropdown list when i select consultant name from dropdownlist i want to show email id of that consultant in listbox.how is this possible?
i write this code but it gives error
protected void  DDConsultantName_SelectedIndexChanged(object sender, EventArgs e)
   {
       SqlConnection cn = new SqlConnection();
       cn.ConnectionString = ConfigurationManager.ConnectionStrings["Homeconstr"].ConnectionString;
       cn.Open();
       SqlCommand command = new SqlCommand("select ContactPerson_EmailID from Consultant_Detail where Consultant_Name=" + this.DDConsultantName.SelectedValue, cn);
       SqlDataAdapter dataAadpter = new SqlDataAdapter(command);
       DataSet ds = new DataSet();
       dataAadpter.Fill(ds);
       LBEmailId.DataSource = ds;
       LBEmailId.DataTextField = "ContactPerson_EmailID";
       //LBEmailId.DataValueField = "Id";
       LBEmailId.DataBind();
       }

when i select e.g morphees consultant from dropdown i want to show his email id in LBemail id listbox ?????????
Posted 2-Oct-12 20:16pm
Edited 2-Oct-12 20:43pm
DaveAuld187.1K
v2
Comments
Mohd. Mukhtar 3-Oct-12 3:16am
   
What error exactly you are getting??
AshishChaudha 3-Oct-12 4:45am
   
have you checked dataset..are you getting the email ID of the selected value??

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Try this:
protected void  DDConsultantName_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlConnection cn = new SqlConnection();
    cn.ConnectionString = ConfigurationManager.ConnectionStrings["Homeconstr"].ConnectionString;
    cn.Open();
    SqlCommand command = new SqlCommand("select ContactPerson_EmailID from Consultant_Detail where Consultant_Name='" + this.DDConsultantName.SelectedValue + "'", cn);
    SqlDataAdapter dataAadpter = new SqlDataAdapter(command);
    DataSet ds = new DataSet();
    dataAadpter.Fill(ds);
    if(ds != null){
        LBEmailId.DataSource = ds.Tables[0];
        LBEmailId.DataTextField = "ContactPerson_EmailID";
        //LBEmailId.DataValueField = "Id";
        LBEmailId.DataBind();
    }
}


Hope it helps!
--Amit
  Permalink  
Comments
16041984 3-Oct-12 3:20am
   
when i use this it is running but not showing any result in listboxemailid i.e LBEmailID.pls help
ujju.1 3-Oct-12 3:31am
   
have you set AutoPostBack="True" for DDConsultantName?
16041984 3-Oct-12 3:45am
   
yes i set autopostback = true for dropdown
<asp:DropDownList ID="DDConsultantName" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDConsultantName_SelectedIndexChanged">

it shows
Mohd. Mukhtar 3-Oct-12 4:05am
   
put break point and check if anything is coming in ds.Tables[0];
16041984 3-Oct-12 4:59am
   
i will do one thing i take one textbox then add email id's in listbox ....problem solved....thank u for ur reply all of u
16041984 3-Oct-12 4:59am
   
if (DDConsultantName.SelectedIndex != 0)
{
string strValue = string.Empty;
cn.Open();
//SqlConnection con = new SqlConnection("server=servername;uid=sa;password=p@ssw0rd;database=test;Connect Timeout=200; pooling='true'");
SqlCommand cmd = new SqlCommand(" select ContactPerson_EmailID from Consultant_Detail where Consultant_ID='" + DDConsultantName.SelectedValue.ToString() + "'", cn);
//cn.Open();
strValue = (string)cmd.ExecuteScalar();
cn.Close();
TextBox1.Text = strValue;
// LBEmailId.DataTextField = "ContactPerson_EmailID";
// LBEmailId.DataValueField = "Consultant_ID";

}

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160621.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100