我想從稱爲租戶的選擇查詢中獲得單個值。來自SQL Server的Tenant_Facing具有單個字符值,即'Y'或'N'。當我執行它時,在Char tenant =(Char)cmd.ExecuteScalar();上出現錯誤,指出「指定的轉換無效」。 我不明白爲什麼它不匹配,即使他們都是Char?這裏我的代碼,ASP.NET c#指定的轉換無效
protected void DropDownArchitecture_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Shared_NotebookConnectionString"].ConnectionString);
string architecture = ((DropDownList)GridViewServer.FooterRow.FindControl("DropDownArchitecture")).Text;
SqlCommand cmd = new SqlCommand("SELECT Tenant_Facing FROM tblArchitecture WHERE Architecture = '" + architecture + "'");
cmd.Connection = conn;
conn.Open();
Char tenant = (Char)cmd.ExecuteScalar();
conn.Close();
if (tenant == 'Y')
{
((DropDownList)GridViewServer.FooterRow.FindControl("DropDownTenant")).Visible = true;
}
else
{
((DropDownList)GridViewServer.FooterRow.FindControl("DropDownTenant")).Visible = false;
}
}
你有沒有試過把它轉換成字符串?字符串tenant =(字符串)cmd.ExecuteScalar(); – SimonGates
小建議:用布爾代替Y或N. –