Check value of a bit field in a dataset


I am trying to check the value of a bit field and based on that, to update a label. I am getting an erro:
Error      1      Operator '==' cannot be applied to operands of type 'object' and 'bool'      D:\MyStuff - Allan\Training\School_Keeper_ASP\Pages_Admin\Lookup_Religion.aspx.cs      74      13      D:\...\School_Keeper_ASP\

My code:
if (dsReligions.Tables["Religions_List"].Rows[lbxReligions.SelectedIndex]["RL_System"] == true)        
            Label4.Text = "System";
            Label4.Text = "NON System";

My sql, that ceated the table:
CREATE TABLE [dbo].[Religions_List](
  RL_No                  smallint IDENTITY(1,1) not null,
  RL_Name            varchar(50) not null,
  RL_System            bit not null default(0),
CONSTRAINT [PK_Religions_List] PRIMARY KEY.......

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Elvio Lujan.Net Senior DeveloperCommented:

if ((int)dsReligions.Tables["Religions_List"].Rows[lbxReligions.SelectedIndex]["RL_System"] == 1)        
            Label4.Text = "System";
            Label4.Text = "NON System";

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
first get the value from dsReligions.Tables["Religions_List"].Rows[lbxReligions.SelectedIndex]["RL_System"]  and then compare

1 means true
0 means false
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

allanmarkAuthor Commented:
int xyz = (int)dsReligions.Tables["Religions_List"].Rows[lbxReligions.SelectedIndex]["RL_System"];
        if ((int)dsReligions.Tables["Religions_List"].Rows[lbxReligions.SelectedIndex]["RL_System"] == 1)        
            Label4.Text = "System";
            Label4.Text = "NON System";

ERROR:  Specified cast is not valid.
Elvio Lujan.Net Senior DeveloperCommented:
make a break point and evaluate the value return in "dsReligions.Tables["Religions_List"].Rows[lbxReligions.SelectedIndex]["RL_System"]"
allanmarkAuthor Commented:
Got it!!

Did as you suggested - the break point thingy ... value was TRUE. SO I played around and:

if ((BOOL)dsReligions.Tables["Religions_List"].Rows[lbxReligions.SelectedIndex]["RL_System"] == true)        
            Label4.Text = "System";
            Label4.Text = "NON System";

As a follow up note:

SQL Server data types do correspond to CLR data types:
bigint -> long
int -> int
smallint -> short
tinyint -> byte
bit -> bool

Be careful to always explicitly cast the result of a function (such as the column indexer of a datatable row, or the GetValue method of a data reader) to the CLR type you want before comparing to other values or assigning to variables.

bool blnSystem = (bool)dsReligions.Tables["Religions_List"].Rows[lbxReligions.SelectedIndex]["RL_System"];

short shNo = (short)myReader.GetValue(0); // same as myReader.GetInt16(0)

All you have to be careful of are null database values - if you could have a NULL in a column, you can use a nullabe data type:

short? shNo = (short)myReader.GetValue(0); // same as myReader.GetInt16(0)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.