0
我收到錯誤在這條線: using (SqlDataReader reader =cmd.ExecuteReader())
的WebMethod拋出「附近有語法錯誤‘=’」
我對Ajax的級聯在ASP.Net下拉例以下是我的代碼。由於錯誤,我無法運行代碼
'='附近的語法不正確。附近使用(SqlDataReader的讀卡器= cmd.ExecuteReader())
代碼
[WebMethod]
public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownCountry1(string knownCategoryValues)
{
// select CountryId, Country from Country where Status='Active'
// string query = "SELECT Country, CountryId FROM Country";
string query = "select [CountryName], [CountryId] from Countries";
List<AjaxControlToolkit.CascadingDropDownNameValue> countries = GetData(query);
return countries.ToArray();
}
private List<AjaxControlToolkit.CascadingDropDownNameValue> GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
List<AjaxControlToolkit.CascadingDropDownNameValue> values = new List<AjaxControlToolkit.CascadingDropDownNameValue>();
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
cmd.Connection = con;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
values.Add(new AjaxControlToolkit.CascadingDropDownNameValue
{
name = reader[0].ToString(),
value = reader[1].ToString()
});
}
reader.Close();
con.Close();
return values;
}
}
}
'CountryId'和'StateId'如何存儲在數據庫中?作爲'int'或'VARCHAR'? – ughai
我建議運行'SQL Profiler'並查看生成的查詢有什麼問題。 –
或打印你的'query'並運行它對sql服務器 – ughai