problem in accessing data from sql in two drop down list at same time
i have two drop down list in web form..for both of them i've used following code to bind with sql..but whenever i'm trying to bind second drop down list with same method..it's giving error.. the code i've used:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlPropertyType.AppendDataBoundItems = true;
String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
String strQuery = "select ID, PropertyName from PropertyType";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new S开发者_Python百科qlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
ddlPropertyType.DataSource = cmd.ExecuteReader();
ddlPropertyType.DataTextField = "PropertyName";
ddlPropertyType.DataValueField = "ID";
ddlPropertyType.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
}
You want to create two dropdownlists with a dependency between them. You need to enable AutoPostBack in the "parent" dropdownlist, add the changed event to it, and in the event load your "child" dropdownlist.
You have a good example here: http://www.aspsnippets.com/Articles/Creating-Cascading-DropDownLists-in-ASP.Net.aspx
u can directly bind dropdown using sql data base.......no need to do cding
精彩评论