public static bool CheckIfUserISbanned(Guid guid)
{
StringBuilder sb = new StringBuilder();
sb.Append("SELECT Banned");
sb.Append(" FROM dbo.Users");
sb.Append(" WHERE UsersID=@UserID");
object o;
bool isBanned = false;
string myConnectionString = AllQuestionsPresented.connectionString;
using (SqlConnection conn = new SqlConnection(AllQuestionsPresented.connectionString))
{
SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
conn.Open();
cmd.Parameters.Add("@UserID", SqlDbType.UniqueIdentifier).Value = guid;
o = cmd.ExecuteScalar();
}
isBanned = o == null ? false : true;//Problem here
return isBanned;
}
The problem is that the object receives always a value which is not null. But in the Users table at the Banned field, I set its type to “Allow Nulls”… I can see that there are nulls, but no null is retreived..Something else does.. which makes the “isBanned” parameter be true..the whole time.. Why is it happening, and how can I know when the the object is bool True.
If your database query returns
NULLin SQL, this gets converted toDBNullin .NET. So rather than testing fornull, test forDBNull.Value.