if (!IsPostBack)
{
SqlDataReader r;
cnFriends.Open();
// Initialize comboboxes
try
{
r = cmPlace.ExecuteReader();
cbPlace.DataSource = r;
cbPlace.DataBind();
r.Close();
cbPlace.Items.Add(new ListItem("-- Not selected --", "0"));
cbPlace.SelectedIndex = cbPlace.Items.Count - 1;
r = cmType.ExecuteReader();
cbType.DataSource = r;
cbType.DataBind();
r.Close();
cbType.Items.Add(new ListItem("-- Not selected --", "0"));
cbType.SelectedIndex = cbType.Items.Count - 1;
}
finally
{
cnFriends.Close(); // Ensure connection is closed
}
}
在取cbType的值时一定要先判断它的值是否为0
如: if (cbPlace.SelectedItem.Value != "0")
{
qry.Append("PlaceID = '");
qry.Append(cbPlace.SelectedItem.Value).Append("' AND ");
}