代码背后:
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
string countryid = Convert.ToString(DropDownList3.SelectedValue);
con.Open();
SqlCommand cmd = new SqlCommand("select * from states where countryID=" + countryid, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
DropDownList4.DataSource = ds;
DropDownList4.DataTextField = "stateName";
DropDownList4.DataValueField = "stateID";
DropDownList4.DataBind();
DropDownList4.Items.Insert(0,new ListItem("--select--","0"));
if (DropDownList4.SelectedValue == "0")
{
DropDownList6.Items.Clear();
DropDownList6.Items.Insert(0, new ListItem("--select--", "0"));
}
表格结构:
CREATE TABLE countries
(
countryID varchar(3) NOT NULL,
countryName nvarchar(100) NOT NULL,
localName nvarchar(100),
webCode varchar(2),
region varchar(50),
continent varchar(25),
latitude float NOT NULL,
longitude float NOT NULL,
surfaceArea float NOT NULL,
population int NOT NULL,
PRIMARY KEY (countryID),
UNIQUE (webCode),
UNIQUE (countryName)
);
错误:
System.Data.SqlClient.SqlException:转换varchar值时转换失败' USA'数据类型int。
答案 0 :(得分:0)
使用这个:
SqlCommand cmd = new SqlCommand("select * from states where countryID='" + countryid + "'", con);
实际上你错过了单引号而你的表CountryID
是varchar
所以你必须使用单引号