Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have wrote this code to load when drop down list index change, but it doesn't working in index change event and other text change event, but working in button click event. Please advise what is the wrong?

SqlConnection con = new SqlConnection(connection);
SqlCommand cmd1 = new SqlCommand("Select ID from Test where Name='" + DropDownList1.Text + "'", con);

SqlDataAdapter da1 = new SqlDataAdapter(cmd1);

DataSet ds1 = new DataSet();
da1.Fill(ds1);

DropDownList2.DataTextField = ds1.Tables[0].Columns["ID"].ToString();
DropDownList2.DataValueField = ds1.Tables[0].Columns["ID"].ToString();

DropDownList2.DataSource = ds1.Tables[0];
DropDownList2.DataBind();
Posted
Comments
ZurdoDev 26-Aug-13 8:06am    
How could we know? What does it mean that it isn't working? You didn't even post the code that isn't working.

1 solution

Set the AutoPostBack="True" in your dropdown list control.
 
Share this answer
 
Comments
abdulsafran 26-Aug-13 8:33am    
Thanks for the answer, now DropDownList2 text values are changing, but I got another problem, the problem is I have created page load event to load one column to DropDownList1, therefore values are changing from only first value in DropDownList1. I can select the second value, because it's always get as first value in DropDownList1. Please advise.
CodeBlack 26-Aug-13 8:46am    
can you post your page load event ?
abdulsafran 26-Aug-13 8:57am    
SqlConnection con = new SqlConnection(connection");
SqlCommand cmd1 = new SqlCommand("Select Name from Test", con);

SqlDataAdapter da1 = new SqlDataAdapter(cmd1);

DataSet ds1 = new DataSet();
da1.Fill(ds1);

DropDownList1.DataTextField = ds1.Tables[0].Columns["Name"].ToString();
DropDownList1.DataValueField = ds1.Tables[0].Columns["Name"].ToString();

DropDownList1.DataSource = ds1.Tables[0];
DropDownList1.DataBind();
CodeBlack 26-Aug-13 9:00am    
Update your code as mentioned below :

SqlConnection con = new SqlConnection(connection");

SqlCommand cmd1 = new SqlCommand("Select Name from Test", con);


SqlDataAdapter da1 = new SqlDataAdapter(cmd1);



if(!IsPostBack)
{
DataSet ds1 = new DataSet();

da1.Fill(ds1);


DropDownList1.DataTextField = ds1.Tables[0].Columns["Name"].ToString();

DropDownList1.DataValueField = ds1.Tables[0].Columns["Name"].ToString();


DropDownList1.DataSource = ds1.Tables[0];

DropDownList1.DataBind();
}

Dont forget to mark it as an answer and rate this solution.
abdulsafran 26-Aug-13 9:06am    
Thank you very much for your answer. I have rated you. Can I ask my last question?

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