473,387 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

casting bit to boolean

Hey

..net 2.0

in my c# code I retrieve a value from a column in database (sql server 2000)
(using SqlReader) this database column has the datatype Bit!

Now I'm looking a way to cast this bit value into an .Net boolean variable

Boolean test = (Boolean) _SqlReader["modified"]; //this throws an
exception - specified cast is not valid!

So I was thinking maybe this could do it:
Boolean test = false;
int value = (int) _SqlReader["modified"];
if (value == 1)
test = true

I wonder if there is a much better of doing this cast?

any suggestions?
Dec 5 '07 #1
5 32988
Jeff wrote:
So I was thinking maybe this could do it:
Boolean test = false;
int value = (int) _SqlReader["modified"];
if (value == 1)
test = true

I wonder if there is a much better of doing this cast?

any suggestions?
Will Convert.ToBoolean((int) _SqlReader["modified"]) work?
Chris.
Dec 5 '07 #2
Jeff,

Something tells me that "modified" might not actually be a bit data
type. If it was, then the provider (the classes in the
System.Data.SqlClient namespace) should have converted it for you.

If you put _SqlReader["modified"] in the debugger, what is the type that
it says it returns?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jeff" <do***@spam.mewrote in message
news:eW**************@TK2MSFTNGP02.phx.gbl...
Hey

.net 2.0

in my c# code I retrieve a value from a column in database (sql server
2000) (using SqlReader) this database column has the datatype Bit!

Now I'm looking a way to cast this bit value into an .Net boolean variable

Boolean test = (Boolean) _SqlReader["modified"]; //this throws an
exception - specified cast is not valid!

So I was thinking maybe this could do it:
Boolean test = false;
int value = (int) _SqlReader["modified"];
if (value == 1)
test = true

I wonder if there is a much better of doing this cast?

any suggestions?


Dec 5 '07 #3
Jeff, if you mean SqlDataReader as in
System.Data.SqlClient.SqlDataReader
you could call

if (_SqlReader.GetBoolean(_SqlReader.GetOrdinal("modi fied")))
{
}

Michael

"Jeff" <do***@spam.mewrote in message
news:eW**************@TK2MSFTNGP02.phx.gbl...
Hey

.net 2.0

in my c# code I retrieve a value from a column in database (sql server
2000) (using SqlReader) this database column has the datatype Bit!

Now I'm looking a way to cast this bit value into an .Net boolean variable

Boolean test = (Boolean) _SqlReader["modified"]; //this throws an
exception - specified cast is not valid!

So I was thinking maybe this could do it:
Boolean test = false;
int value = (int) _SqlReader["modified"];
if (value == 1)
test = true

I wonder if there is a much better of doing this cast?

any suggestions?


Dec 5 '07 #4

"Jeff" <do***@spam.mewrote in message
news:eW**************@TK2MSFTNGP02.phx.gbl...
Hey

.net 2.0

in my c# code I retrieve a value from a column in database (sql server
2000) (using SqlReader) this database column has the datatype Bit!

Now I'm looking a way to cast this bit value into an .Net boolean variable

Boolean test = (Boolean) _SqlReader["modified"]; //this throws an
exception - specified cast is not valid!

So I was thinking maybe this could do it:
Boolean test = false;
int value = (int) _SqlReader["modified"];
if (value == 1)
test = true

I wonder if there is a much better of doing this cast?
How about:

bool test = (_SqlReader["modified"] == 1);
>
any suggestions?


Dec 5 '07 #5

-----Original Message-----
From: Ben Voigt [C++ MVP] [mailto:rb*@nospam.nospam]
Posted At: Thursday, 6 December 2007 7:01 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: casting bit to boolean
Subject: Re: casting bit to boolean
"Jeff" <do***@spam.mewrote in message
news:eW**************@TK2MSFTNGP02.phx.gbl...
Hey

.net 2.0

in my c# code I retrieve a value from a column in database (sql
server
2000) (using SqlReader) this database column has the datatype Bit!

Now I'm looking a way to cast this bit value into an .Net boolean
variable

Boolean test = (Boolean) _SqlReader["modified"]; //this throws an
exception - specified cast is not valid!

So I was thinking maybe this could do it:
Boolean test = false;
int value = (int) _SqlReader["modified"];
if (value == 1)
test = true

I wonder if there is a much better of doing this cast?

How about:

bool test = (_SqlReader["modified"] == 1);

any suggestions?
I was going to suggest the same, but it was going to be a
if..then..else. Good job finding the one liner that did it.

Dec 6 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: F. Da Costa | last post by:
Hi all, Following two sniperts of code I'm using and getting very interesting results from. ..html <tr id="1" class="segment" open="false"> This is the segment under 'investigation' ..js
3
by: Daves | last post by:
trying to get a boolean value from DataReader (that is, result from a SQL query); boolean MyBool = MyDataReader.GetBoolean(3); well it works using that number 3 but of course I would like to...
8
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked...
3
by: Matt Weaver | last post by:
I'm trying to setup a sub that would assign the properties of a webcontrol based on data in a table. I've setup a routine which looks like this: Private Sub SetObjectProperty(ByRef obj As...
2
by: Giulio Petrucci | last post by:
Hi everybody, here's my problem: I have to dymanically build (and compile, of course) some code, from some ECMAScript function. ECMAScript variables I get are not typezed, so I should have...
18
by: Greg Scharlemann | last post by:
I'm having an issue passing a boolean to the constructor of an object. For some reason, if I pass false into the constructor, it doesn't register. If I pass an integer 0, it does. PHP 5 Code...
10
by: active | last post by:
Function CastIt (byval z as boolean) As ?? if z then CastIt = DirectCast(ActiveMdiChild, MyForm1) else CastIt = DirectCast(ActiveMdiChild, MyForm2) end if End Function Can I make a function...
7
by: Michael Sharman | last post by:
Can anyone confirm if in PHP 0 = FALSE and anything else if TRUE? Or is it more 0 = FALSE and 1 = TRUE? One of the reasons I ask is that if I do; //$count is an incrementing value starting...
4
by: AndreH | last post by:
Good day, I have the a bit of an issue with retrieving an object from php's session. I set a session variable "user" from the class User as follows: $user = new User(); // ... do some stuff...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.