Solved

Check value of a bit field in a dataset

Posted on 2007-12-06
7
458 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 18

Accepted Solution

by:
Elvio Lujan 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

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 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

705 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