Solved

Check value of a bit field in a dataset

Posted on 2007-12-06
7
452 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 17

Accepted Solution

by:
lem2802 earned 200 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 50 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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 17

Assisted Solution

by:lem2802
lem2802 earned 200 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
crm development 2 39
Converting C# Objects into JSON file 7 41
Align Buttons next to each other 2 14
Saveas need to save a copy 16 14
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now