0

I have a method that i would like to use multiple times, that basically populates a Dropdown List.

public void PopulateDropdown(string selectedValue, object listname)
{
    String connString = ConfigurationManager.ConnectionStrings["MySql"].ToString(); //Conn string
    MySqlConnection mySqlConnection = new MySqlConnection(connString); //Objekt
    MySqlCommand cmd = new MySqlCommand(); //cmd objekt

    cmd.CommandText = "SELECT NAME FROM CustomerDb WHERE CITY = \"" + selectedValue + "\"";
    cmd.CommandType = CommandType.Text;
    cmd.Connection = mySqlConnection;

    DropDownList dropDownList = listname as DropDownList;
    mySqlConnection.Open();

    dropDownList.DataSource = cmd.ExecuteReader();
    dropDownList.DataTextField = "NAME";
    dropDownList.DataBind();
    mySqlConnection.Close();
}

My call looks like this:

protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
    string value = DropDownList3.SelectedValue;
    PopulateDropdown(value, DropDownList4);
}

I know that my call and my method is correct, but for some reason, im not able to call it in DropDownList3_SelectedIndexChanged. When i select a value in DropDownList3 it just reloads and picks the default value "Select city".

<asp:DropDownList ID="DropDownList3" CssClass="btn btn-default btn-md pull-right" runat="server" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged" AutoPostBack="true">
   <asp:ListItem>Select city</asp:ListItem>
   <asp:ListItem>City1</asp:ListItem>
   <asp:ListItem>City2</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList4" runat="server" CssClass="btn btn-default btn-md pull-right" OnSelectedIndexChanged="DropDownList4_SelectedIndexChanged" AutoPostBack="true" Style="">
</asp:DropDownList>

my DropDownList3_SelectedIndexChanged looks like this:

protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
        string value = DropDownList3.SelectedValue;
        PopulateDropdown(value, DropDownList4);
}

The postback doesn't reach breakpoint in method.

Jeppe Christensen
  • 1,680
  • 2
  • 21
  • 50

2 Answers2

0

Well I don't really know if this will answer your question, but when getting a value from the database and using those data to fill a combobox or dropdownlist, I use this code/query:

    String path = "Data Source = LOCALHOST; Initial Catalog= sample_database; username='root'; password=''";
        MySqlConnection sqlcon = new MySqlConnection(path);
        MySqlCommand sqlcom = new MySqlCommand();
        MySqlDataReader sqlread;

        sqlcon.Open();
        sqlcom.CommandType = CommandType.Text;
        sqlcom.CommandText = "SELECT name from database_table where city = '"+TextBox1.text+"'";
        sqlcom.Connection = sqlcon;
        sqlread = sqlcom.ExecuteReader();
        while (sqlread.Read()) //use loop to get all data in the specified column
        comboBox1.Items.Add(sqlread[0].ToString()); //place the data gathered in the combobox or dropdownlist
        sqlcon.Close();
0

It could be that you're populating the City list on page load, and the AutoPostBack reloads the list. See DropDownList's SelectedIndexChanged event not firing

Community
  • 1
  • 1
Craig Eddy
  • 889
  • 1
  • 6
  • 17