我想从名为租户的选择查询中获取单个值。SQL Server 中的 Tenant_Facing 具有单个字符值,即“Y”或“N”。当我执行它时,在 Char 租户 = (Char)cmd.ExecuteScalar(); 上显示“指定的强制转换无效”的错误。我不明白为什么即使它们都是Char也不匹配?这是我的代码,
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;
}
}