Click here to Skip to main content
15,905,781 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one registration form. I have made cascading dropdownlist in which data of state dropdownlist is loaded on selected index change of Country. sql database is having table Country and State.

I am loading all data(other page, like here it is edit profile) which was submitted during registration on page load event at respective textboxes and dropdownlist. The problem is that Country is loading perfectly on country dropdown but the value of respective state is not loading on state drop down.


code that i am using is as below

C#
protected void loadinternshipdata()
       {
           SqlConnection con = new SqlConnection("Data Source=ja-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");

           con.Open();
           SqlCommand cmd3 = new SqlCommand("select * from Internship where CompanyID=" + Convert.ToInt16(Session["CompanyID"].ToString()), con);

           SqlDataAdapter daa = new SqlDataAdapter(cmd3);
           DataTable dtt = new DataTable();
           daa.Fill(dtt);
           if (dtt.Rows.Count > 0)
           {

               DropDownList3.SelectedItem.Text = dtt.Rows[0]["Country"].ToString();
              DropDownList4.SelectedItem.Text = dtt.Rows[0]["State"].ToString();







           }
       }
Posted
Comments
Ankit Maini 1-Apr-14 7:24am    
I guess the code is changing the selection of dropdown control and not loading any data into them. So, can you please post the code related to data loading in dropdown list?
jayraj86 1-Apr-14 7:32am    
actually it is degree and branch in place of country and city

protected void BindDegreedropdown()
{
//conenction path for database
SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");
con.Open();
SqlCommand cmd = new SqlCommand("select * from Degree", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
DropDownList3.DataSource = ds;
DropDownList3.DataTextField = "DegreeName";
DropDownList3.DataValueField = "DegreeID";
DropDownList3.DataBind();
DropDownList3.Items.Insert(0, new ListItem("--Select--", "0"));
//branch1.Items.Insert(0, new ListItem("--Select--", "0"));

}


protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
int DegreeID = Convert.ToInt32(DropDownList3.SelectedValue);
SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");
con.Open();
SqlCommand cmd = new SqlCommand("select * from Branch where DegreeID=" + DegreeID, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
DropDownList4.DataSource = ds;
DropDownList4.DataTextField = "BranchName";
DropDownList4.DataValueField = "BranchID";
DropDownList4.DataBind();
DropDownList4.Items.Insert(0, new ListItem("--Select--", "0"));
//if (DropDownList2.SelectedValue == "0")
//{
// DropDownList3.Items.Clear();
// DropDownList3.Items.Insert(0, new ListItem("--Select--", "0"));
//}
}
Member 10476757 1-Apr-14 7:40am    
CHECK YOUR CONNECTION STRING
Ankit Maini 1-Apr-14 7:42am    
The code is looking fine. Please make sure that AutoPostback properties of dropdowns are set to true.
jayraj86 1-Apr-14 7:52am    
string i checked, its okay. I am not having problem in cascading dropdownlist. the problem is in loadinternshipdata() function. where i am just loading data from table (editing profile, which shows all registered data in respective textboxes).

1 solution

Mmmm try this:
C#
protected void loadinternshipdata()
{
    if (!IsPostBack)
    {
        //YOUR CODE HERE.
    }
}


Hope it helps.
 
Share this answer
 
Comments
jayraj86 2-Apr-14 6:27am    
I am already loading it on !IsPostBack

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