Reputation: 5986
The best way to explain this is with code:
byte roominspiration = 0;
query = "SELECT room_inspiration FROM Room WHERE room_id = @room_id); SELECT SCOPE_IDENTITY();";
using (sqlConnection1)
{
using (SqlCommand cmd = new SqlCommand(query, sqlConnection1))
{
cmd.Parameters.Add("@room_id", System.Data.SqlDbType.Int);
cmd.Parameters["@room_id"].Value = roomid;
sqlConnection1.Open();
if (cmd.ExecuteScalar() != null)
{
roominspiration = (byte)cmd.ExecuteScalar();
}
}
}
All I'm trying to do is update the value of 'roominspiration' to whatever the value of that record in the database is. The field in the database is a bit datatype with a default value of 0. It is always set correctly, so this shouldn't an issue in it returning null.
Can anyone spot what I'm doing wrong, or if there's a better way to do what I'm doing. My C# skills aren't too good, but I'm trying to learn!
Thanks folks!
Upvotes: 2
Views: 10199
Reputation: 18843
What you could also do to convert the bit to a bool is keep the roominspiration defined as a byte and at the end convert it like
roominspiration = ConvertToBoolean(the value you are wanting to convert);
Upvotes: 1
Reputation: 171451
I think you want to use the bool
.NET data type here.
So,
bool roominspiration = false;
query = "SELECT room_inspiration FROM Room WHERE room_id = @room_id); SELECT SCOPE_IDENTITY();";
using (sqlConnection1)
{
using (SqlCommand cmd = new SqlCommand(query, sqlConnection1))
{
cmd.Parameters.Add("@room_id", System.Data.SqlDbType.Int);
cmd.Parameters["@room_id"].Value = roomid;
sqlConnection1.Open();
roominspiration = ((bool?)cmd.ExecuteScalar()).GetValueOrDefault();
}
}
Upvotes: 8