Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am beginner in this field. i have problem with databinding of drop down list.Problem is that when i select an item in drop down list1 then drop down list2 doesn't show data related to selected value in 1st drop down list.problem is in databinding of drop down list2. What to do, plz help me.

C#
  public SqlConnection conn = new SqlConnection("Data Source=.; uid=sa; password=123456; database=GD_Test");


    public void BindCountry()
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("Select * from Country", conn);
        SqlDataReader dr = cmd.ExecuteReader();
        DDList1.DataSource = dr;
        DDList1.Items.Clear();
        DDList1.Items.Add("--Please Select country--");
        DDList1.DataTextField = "Country";
        DDList1.DataValueField = "CountryId";
        DDList1.DataBind();
        conn.Close();
    }
    public void BindState()
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("Select State,StateID from State where CountryID='" + DDList1.SelectedValue + "", conn);
        SqlDataReader dr = cmd.ExecuteReader();
        DDList2.DataSource = dr;
        DDList2.Items.Clear();
        DDList2.Items.Add("--Please Select State--");
        DDList2.DataTextField = "State";
        DDList2.DataValueField = "StateID";
        DDList2.DataBind();
        conn.Close();
    }
    public void BindCity()
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("Select* from City where StateID='" + DDList2.SelectedValue + "", conn);
        SqlDataReader dr = cmd.ExecuteReader();
        DDList3.DataSource = dr;
        DDList3.Items.Clear();
        DDList3.Items.Add("--Please Select City--");
        DDList3.DataTextField = "City";
        DDList3.DataValueField = "CityID";
        DDList3.DataBind();
        conn.Close();
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
            BindCountry();
    }
    

    protected void DDList2_SelectedIndexChanged(object sender, EventArgs e)
{
    
        BindCity();
    }



    protected void DDList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindState();

    }


Following is the error its showing in case of BindState() method near 'SqlDataReader dr = cmd.ExecuteReader();'...

Unclosed quotation mark after the character string '101'.
Incorrect syntax near '101'.
Posted

1 solution

Hi,
You missed adding single quotes at the end of SQL Statements.

Use this
C#
SqlCommand cmd = new SqlCommand("Select State,StateID from State where CountryID='" + DDList1.SelectedValue + "", conn);


Instead of this
C#
SqlCommand cmd = new SqlCommand("Select State,StateID from State where CountryID='" + DDList1.SelectedValue + "'", conn);



Its a work around of your error. But you always use parameters in you statements.
 
Share this answer
 
Comments
pck.ns 27-Apr-12 1:47am    
thanx
it worked around error.
but how to solve main problem. and also there is another problem that after selecting item in DDList1 DDList2 become invisible..
pck.ns 27-Apr-12 2:00am    
problem was in designing part now it is solved..thanx to Deepak_Sharma_
for ur help in first case

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