我有 2 个下拉列表,一个用于州,一个用于城市。我正在从工作正常的数据库中填充状态下拉列表。我有一种方法,当各州的索引发生变化时,它会填充城市下拉列表。现在由于某种原因,当我更改状态并且该方法点击 populateCity 下拉菜单时,它给了我一个错误说
Sqlexception 错误:无效的列名 'ME'
我只是因为我选择了缅因州,它会发生在我选择的每个州,在 da.fill(ds, "City"); 不知道为什么它一直这样做。
这是州下拉菜单和城市下拉菜单的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
SqlCommand cmdState = new SqlCommand("SELECT DISTINCT state,abr FROM tbl_zip ORDER BY state ASC", con);
DataSet dsState = new DataSet();
SqlDataAdapter daState = new SqlDataAdapter(cmdState);
con.Open();
daState.Fill(dsState, "State");
States.DataSource = dsState.Tables[0];
States.DataTextField = "state";
States.DataValueField = "abr";
States.DataBind();
con.Close();
}
}
protected void States_SelectedIndexChanged(object sender, EventArgs e)
{
if (States.SelectedItem.Value != "Select")
{
string st = States.SelectedValue;
PopulateCity(st);
}
}
protected void PopulateCity(string st)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT DISTINCT city FROM tbl_zip WHERE abr = " + st, con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
da.Fill(ds, "City");
Cities.DataSource = ds.Tables[0];
Cities.DataTextField = "city";
Cities.DataValueField = "city";
Cities.DataBind();
}