Click here to Skip to main content
Rate this: bad
good
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 21:16pm
Edited 2-Oct-12 21:43pm
DaveAuld177.7K
v2
Comments
Mohd. Mukhtar at 3-Oct-12 3:16am
   
What error exactly you are getting??
AshishChaudha at 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 at 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 at 3-Oct-12 3:31am
   
have you set AutoPostBack="True" for DDConsultantName?
16041984 at 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 at 3-Oct-12 4:05am
   
put break point and check if anything is coming in ds.Tables[0];
16041984 at 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 at 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
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,327
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,920


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2014
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