rai nalasa rai nalasa - 6 months ago 20
MySQL Question

bind data on dropdownlist

I'm trying run this code

private void LoadProdName()
{
con.Open();
cmd = new SqlCommand(@"SELECT productName
FROM Products3
WHERE productType = @Type",con);

cmd.Parameters.AddWithValue("@Type",ddlProducts.SelectedItem.Value.ToString());

da = new SqlDataAdapter(cmd);
dt = new DataTable();
da.Fill(dt);
ddlProdName.DataSource = dt;
ddlProdName.DataBind();
con.Close();
}


my selectedIndesChange codes:

protected void ddlProducts_SelectedIndexChanged(object sender, EventArgs e)
{
LoadProdName();
}


which uses the value of my first dropdownlist.My problem is whenever I select a ProductType on my dropdownlist it will fill my second DDL with the select query but I am not getting any data at all from my second dropdownlist.

Now I'm getting some progress.
This is what I get now:
enter image description here

Answer

For cascade drowdownlist you must do a postback in your dropdownlist event "OnSelectedIndexChanged" or use an AJAX call to your method

try this guide: http://www.aspsnippets.com/Articles/Creating-Cascading-DropDownLists-in-ASP.Net.aspx

try this code after your query

        ddlProdName.DataSource = cmd.ExecuteReader();

        ddlProdName.DataTextField = "productName";

        ddlProdName.DataValueField = "productName"; //or productId

        ddlProdName.DataBind();
Comments