I want to get single value from select query called tenant. Tenant_Facing from SQL Server have single char value that either ‘Y’ or ‘N’. When I execute it, there a error that say “Specified cast is not valid” on Char tenant = (Char)cmd.ExecuteScalar();.
I don’t understand why it not match even they are both Char? Here my codes,
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;
}
}
Even though your data type in SQL Server is a single
char, that type still maps tostringin ADO.NET. Try it like this instead: