I am facing a problem getting the boolean value from one of the columns of my database. I am using SQL Server 2008 where in I have created a databaseas follows:
Table name: SysUser3 and columns as:
ProductName || ProductId || SelectedProducts
The column SelectedProducts is a BIT type column and contains False values for each of the row entries at present.
Now, I am writing a SQL Query to get the boolean value from my 'SelectedProducts' column
Here is my code:
using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Database.mdf;User Instance=true"))
{
con.Open();
string cmdString = "SELECT ProductName,SelectedProducts FROM SysUser3";
using (SqlCommand cmd = new SqlCommand(cmdString, con))
{
using (SqlDataReader dataRead = cmd.ExecuteReader())
{
while (dataRead.Read())
{
items.Add(new ProductModel
{
Selected=(bool)dataRead["SelectedProducts"];
ProductName= dataRead["ProductName"].ToString()
});
}
}
}
}
I am getting an error at this line and hence not able to run the code:
Selected=(bool)dataRead["SelectedProducts"];
Am I doing it correctly ? can someone tell me what's wrong in the code?