locked
add item to dropdownlist from database using first dropdownlist RRS feed

  • Question

  • User383567333 posted

     Hi i have two dropdownlist , i want to add item in dropdownlist  2 from database if the city is changed in first dropdownlist..I am using access database

    here is my code:


    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            con.Open();
            OleDbCommand cmd1 = new OleDbCommand("select ncity from add_property where ncity=" + DropDownList1.SelectedItem.Text, con);
            OleDbDataReader red = cmd1.ExecuteReader();
            DropDownList2.DataSource = red;
            DropDownList2.DataValueField = "ncity";
            DropDownList2.DataTextField = "ncity";
            DropDownList2.DataBind();
            con.Close();
        }

    --------------------------------------------------------------------------------------------------------------------------------

    <asp:DropDownList ID="DropDownList1" runat="server" CssClass="formlist" AutoPostBack="true" OnTextChanged="DropDownList1_TextChanged" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
          
                    <asp:ListItem  Selected="True" value="Delhi">Delhi</asp:ListItem>
                    <asp:ListItem value="Solan">Solan</asp:ListItem>
                    <asp:ListItem value="Agra">Agra</asp:ListItem>
                    <asp:ListItem value="Jaipur">Jaipur</asp:ListItem>
                    <asp:ListItem value="Gangtok">Gangtok</asp:ListItem>

    </asp:DropDownList>



    but when i select any text , it will not show anything in dropdownlist2 ..


    plz help me..

    Thanks

    Friday, May 28, 2010 2:54 AM

Answers

  • User-1199946673 posted

    but when i select any text , it will not show anything in dropdownlist2 ..

    Are you sure that your SQL statement is correct? If ncity is a text field, your syntax is wrong. You should use quotes, something like:

    ("select ncity from add_property where ncity='" + DropDownList1.SelectedItem.Text + "'"

    But you should do some reading on Parameterized queries. They're not only saver (the most important reason), but you don't have to think if you need to use quotes or not

    Also, take a look at the AJAX Toolkit CascadingDropDown Extender

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 28, 2010 3:22 AM

All replies

  • User-1199946673 posted

    but when i select any text , it will not show anything in dropdownlist2 ..

    Are you sure that your SQL statement is correct? If ncity is a text field, your syntax is wrong. You should use quotes, something like:

    ("select ncity from add_property where ncity='" + DropDownList1.SelectedItem.Text + "'"

    But you should do some reading on Parameterized queries. They're not only saver (the most important reason), but you don't have to think if you need to use quotes or not

    Also, take a look at the AJAX Toolkit CascadingDropDown Extender

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 28, 2010 3:22 AM
  • User383567333 posted

    but when i select any text , it will not show anything in dropdownlist2 ..

    Are you sure that your SQL statement is correct? If ncity is a text field, your syntax is wrong. You should use quotes, something like:

    ("select ncity from add_property where ncity='" + DropDownList1.SelectedItem.Text + "'"

    But you should do some reading on Parameterized queries. They're not only saver (the most important reason), but you don't have to think if you need to use quotes or not

    Also, take a look at the AJAX Toolkit CascadingDropDown Extender



    hi thanks for reply , i really forgot that this is varchar i was trying as a int...


    Thanks........


    Friday, May 28, 2010 3:28 AM