?
Solved

Check value of a bit field in a dataset

Posted on 2007-12-06
7
Medium Priority
?
462 Views
Last Modified: 2013-12-16
Greetings

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";
        else
            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.......

0
Comment
Question by:allanmark
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 18

Accepted Solution

by:
Elvio Lujan earned 800 total points
ID: 20422791
try...

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

Assisted Solution

by:nisarkhan
nisarkhan earned 200 total points
ID: 20422800
first get the value from dsReligions.Tables["Religions_List"].Rows[lbxReligions.SelectedIndex]["RL_System"]  and then compare



0
 
LVL 7

Expert Comment

by:nisarkhan
ID: 20422805
1 means true
0 means false
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 

Author Comment

by:allanmark
ID: 20423056
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";
        else
            Label4.Text = "NON System";

ERROR:  Specified cast is not valid.
0
 
LVL 18

Assisted Solution

by:Elvio Lujan
Elvio Lujan earned 800 total points
ID: 20423111
make a break point and evaluate the value return in "dsReligions.Tables["Religions_List"].Rows[lbxReligions.SelectedIndex]["RL_System"]"
0
 

Author Comment

by:allanmark
ID: 20423364
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";
        else
            Label4.Text = "NON System";


THANK YOU!!!
0
 
LVL 12

Expert Comment

by:AGBrown
ID: 20428536
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.
e.g.

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)

Andy
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question