473,769 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert tinyint to boolean

I have a value in my sql table set to tinyint (can't set to bit).

I am trying to move it into a boolean field in my program and have tried:

isTrue = (int)dbReader["IsTrue"]

and

isTrue = Convert.ToBoole an((int)dbReade r["IsTrue"])

Both give me a "Specified cast is not valid".

Is there a simple way to do this other then

isTrue = false
if ((int)dbReader["IsTrue"] == 1)
isTrue = true;

Thanks,

Tom
Jul 29 '08 #1
19 14812

"tshad" <ts***@dslextre me.comwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
>I have a value in my sql table set to tinyint (can't set to bit).

I am trying to move it into a boolean field in my program and have tried:

isTrue = (int)dbReader["IsTrue"]

and

isTrue = Convert.ToBoole an((int)dbReade r["IsTrue"])

Both give me a "Specified cast is not valid".

Is there a simple way to do this other then

isTrue = false
if ((int)dbReader["IsTrue"] == 1)
isTrue = true;
Turns out I can't do

if ((int)dbReader["IsTrue"] == 1)

or

if (((int)dbReader["IsTrue"]) == 1)

I still get the same error.

Yet, the debugger shows the value to be 1.

Thanks,

Tom
>
Thanks,

Tom


Jul 29 '08 #2
How about:

isTrue = (dbReader("IsTr ue") <0

Tom Dacon
Dacon Software Consulting

"tshad" <ts***@dslextre me.comwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
>I have a value in my sql table set to tinyint (can't set to bit).

I am trying to move it into a boolean field in my program and have tried:

isTrue = (int)dbReader["IsTrue"]

and

isTrue = Convert.ToBoole an((int)dbReade r["IsTrue"])

Both give me a "Specified cast is not valid".

Is there a simple way to do this other then

isTrue = false
if ((int)dbReader["IsTrue"] == 1)
isTrue = true;

Thanks,

Tom


Jul 29 '08 #3

"Tom Dacon" <td****@communi ty.nospamwrote in message
news:Oz******** ******@TK2MSFTN GP05.phx.gbl...
How about:

isTrue = (dbReader("IsTr ue") <0
Actually, it would be something like:

isTrue = (int)dbReader["IsTrue"] != 0;

or

isTrue = ((int)dbReader["IsTrue"]) != 0;

but I get the same error.

Thanks,

tom
>
Tom Dacon
Dacon Software Consulting

"tshad" <ts***@dslextre me.comwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
>>I have a value in my sql table set to tinyint (can't set to bit).

I am trying to move it into a boolean field in my program and have tried:

isTrue = (int)dbReader["IsTrue"]

and

isTrue = Convert.ToBoole an((int)dbReade r["IsTrue"])

Both give me a "Specified cast is not valid".

Is there a simple way to do this other then

isTrue = false
if ((int)dbReader["IsTrue"] == 1)
isTrue = true;

Thanks,

Tom



Jul 29 '08 #4
On Jul 29, 1:36*pm, "tshad" <ts...@dslextre me.comwrote:
"Tom Dacon" <tda...@communi ty.nospamwrote in message

news:Oz******** ******@TK2MSFTN GP05.phx.gbl...
How about:
isTrue = (dbReader("IsTr ue") <0

Actually, it would be something like:

isTrue = (int)dbReader["IsTrue"] != 0;

or

isTrue = ((int)dbReader["IsTrue"]) != 0;

but I get the same error.

Thanks,

tom


Tom Dacon
Dacon Software Consulting
"tshad" <ts...@dslextre me.comwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
>I have a value in my sql table set to tinyint (can't set to bit).
I am trying to move it into a boolean field in my program and have tried:
isTrue = (int)dbReader["IsTrue"]
and
isTrue = Convert.ToBoole an((int)dbReade r["IsTrue"])
Both give me a "Specified cast is not valid".
Is there a simple way to do this other then
isTrue = false
if ((int)dbReader["IsTrue"] == 1)
* *isTrue = true;
Thanks,
Tom- Hide quoted text -

- Show quoted text -
what error r u getting?
Jul 29 '08 #5
On Jul 29, 1:06*pm, "tshad" <ts...@dslextre me.comwrote:
I have a value in my sql table set to tinyint (can't set to bit).

I am trying to move it into a boolean field in my program and have tried:

isTrue = (int)dbReader["IsTrue"]

and

isTrue = Convert.ToBoole an((int)dbReade r["IsTrue"])

Both give me a "Specified cast is not valid".

Is there a simple way to do this other then

isTrue = false
if ((int)dbReader["IsTrue"] == 1)
* * isTrue = true;

Thanks,

Tom
isTrue = Convert.ToBoole an( dbReader["IsTrue"])
should do the trick, there is no need to cast it

WARNING, you have to make sure that the value is not null because it
will fail then
Jul 29 '08 #6
tshad wrote:
I have a value in my sql table set to tinyint (can't set to bit).

I am trying to move it into a boolean field in my program and have tried:

isTrue = (int)dbReader["IsTrue"]

and

isTrue = Convert.ToBoole an((int)dbReade r["IsTrue"])

Both give me a "Specified cast is not valid".
The SQL TINYINT type is mapped to the .NET "Byte" type. While you can cast a
byte to an int, you can't cast a byte boxed as an object to int, as that
will not invoke any conversions.
Is there a simple way to do this other then

isTrue = false
if ((int)dbReader["IsTrue"] == 1)
isTrue = true;
This will not work either, for the same reason.

Ignacio already answered this one. Use Convert.ToBoole an directly and don't
involve any casts. If the value can be NULL, you have to check:

isTrue = !dbReader.IsDBN ull(dbReader.Ge tOrdinal("IsTru e")) &&
Convert.ToBoole an(dbReader["IsTrue"]));

Or:

object o = dbReader["IsTrue"];
isTrue = o != DBNull.Value && Convert.ToBoole an(o);

If you feel confident, you can cast the column to byte, but then everything
will break if it's ever changed to smallint, int or bit (none of which are
unlikely).

--
J.
Jul 29 '08 #7

"Ignacio Machin ( .NET/ C# MVP )" <ig************ @gmail.comwrote in
message
news:55******** *************** ***********@m45 g2000hsb.google groups.com...
On Jul 29, 1:36 pm, "tshad" <ts...@dslextre me.comwrote:
"Tom Dacon" <tda...@communi ty.nospamwrote in message

news:Oz******** ******@TK2MSFTN GP05.phx.gbl...
How about:
isTrue = (dbReader("IsTr ue") <0

Actually, it would be something like:

isTrue = (int)dbReader["IsTrue"] != 0;

or

isTrue = ((int)dbReader["IsTrue"]) != 0;

but I get the same error.

Thanks,

tom


Tom Dacon
Dacon Software Consulting
"tshad" <ts...@dslextre me.comwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
>I have a value in my sql table set to tinyint (can't set to bit).
I am trying to move it into a boolean field in my program and have
tried:
isTrue = (int)dbReader["IsTrue"]
and
isTrue = Convert.ToBoole an((int)dbReade r["IsTrue"])
Both give me a "Specified cast is not valid".
Is there a simple way to do this other then
isTrue = false
if ((int)dbReader["IsTrue"] == 1)
isTrue = true;
Thanks,
Tom- Hide quoted text -

- Show quoted text -
>what error r u getting?
I figured it out, but the error was:

"Specified cast is not valid".

The problem was that I was using a tinyint and needed to use byte (short
didn't seem to work, either).

Thanks,

Tom
Jul 29 '08 #8

"Jeroen Mostert" <jm******@xs4al l.nlwrote in message
news:48******** *************** @news.xs4all.nl ...
tshad wrote:
>I have a value in my sql table set to tinyint (can't set to bit).

I am trying to move it into a boolean field in my program and have tried:

isTrue = (int)dbReader["IsTrue"]

and

isTrue = Convert.ToBoole an((int)dbReade r["IsTrue"])

Both give me a "Specified cast is not valid".
The SQL TINYINT type is mapped to the .NET "Byte" type. While you can cast
a byte to an int, you can't cast a byte boxed as an object to int, as that
will not invoke any conversions.
That was what I found out, and did this to solve the problem:

IsTrue = (byte)dbReader["IsTrue"] == 1;

But I like Ignacios way better.

Thanks,

Tom
>
>Is there a simple way to do this other then

isTrue = false
if ((int)dbReader["IsTrue"] == 1)
isTrue = true;
This will not work either, for the same reason.

Ignacio already answered this one. Use Convert.ToBoole an directly and
don't involve any casts. If the value can be NULL, you have to check:

isTrue = !dbReader.IsDBN ull(dbReader.Ge tOrdinal("IsTru e")) &&
Convert.ToBoole an(dbReader["IsTrue"]));

Or:

object o = dbReader["IsTrue"];
isTrue = o != DBNull.Value && Convert.ToBoole an(o);

If you feel confident, you can cast the column to byte, but then
everything will break if it's ever changed to smallint, int or bit (none
of which are unlikely).

--
J.

Jul 30 '08 #9
Jeroen Mostert wrote:
tshad wrote:
>I have a value in my sql table set to tinyint (can't set to bit).

I am trying to move it into a boolean field in my program and have tried:

isTrue = (int)dbReader["IsTrue"]

and

isTrue = Convert.ToBoole an((int)dbReade r["IsTrue"])

Both give me a "Specified cast is not valid".
The SQL TINYINT type is mapped to the .NET "Byte" type. While you can
cast a byte to an int, you can't cast a byte boxed as an object to int,
as that will not invoke any conversions.
The funny looking:

(int)(byte)dbRe ader["IsTrue"]

should work.
Ignacio already answered this one. Use Convert.ToBoole an directly and
don't involve any casts. If the value can be NULL, you have to check:

isTrue = !dbReader.IsDBN ull(dbReader.Ge tOrdinal("IsTru e")) &&
Convert.ToBoole an(dbReader["IsTrue"]));

Or:

object o = dbReader["IsTrue"];
isTrue = o != DBNull.Value && Convert.ToBoole an(o);

If you feel confident, you can cast the column to byte, but then
everything will break if it's ever changed to smallint, int or bit (none
of which are unlikely).
I have the exact opposite opinion.

I like the cast.

Because if the field get changed from TINYINT to INT then I will like
to see a cast exception, because if the range 0-255 is too small,
then converting it to a boolean is most likely wrong !

Arne
Jul 30 '08 #10

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

Similar topics

19
7287
by: Lauren Quantrell | last post by:
I have a stored procedure using Convert where the exact same Convert string works in the SELECT portion of the procedure but fails in the WHERE portion. The entire SP is listed below. Specifically, I have a problem with this portion in the WHERE clause: DATEADD(Day,tblMyEventTableName.ReminderDays, @DateNow) Between CONVERT(smalldatetime,str(DATEPART(Month, @DateNow)+1) + '/' + str(DATEPART(Day, tblMyEventTableName.TaskDateTime)) + '/'...
0
1441
by: Kenneth P | last post by:
Hi, In MySql db doc ch 11 TINYINT A very small integer. The signed range is -128 to 127. The unsigned range is 0 to 255. BIT BOOL BOOLEAN
6
14567
by: Tim Cartwright | last post by:
I have a page that has the login control on it, nothing else. This page inherits from a master page, neither page has any code in it. This page works perfectly when running on the WebDev debug web server. I am able to log in. However after publishing the page to my local IIS, it results in the below error. This error is occurring on the Visual Studio.Net 2k5 release version, but did not occur on the beta 2, same code. A point of interest, is...
4
12527
by: John A Grandy | last post by:
Often in dbs , tinyint is used instead of int (because tinyint cols may be indexed , whereas int cols may not). What is the best way to convert integers to booleans ? The following fails: bool myBool; tinyint myTinyInt = 1;
0
1975
by: Elmo Watson | last post by:
I am having all kinds of problems with a Stored Proc I'm trying to run, with the error I'm getting above. First - the way I'm reading it - the database EXPECTS a TinyInt - and it thinks I'm giving it a varchar input - right? If that's so - there's only one field in the table that uses tinyInt - and I've double checked: cmd.Parameters.Add(New OleDbParameter("@idMarketingRepType",
5
3790
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public List<RoleData> GetRoles() { return GetRoles(null, false); }
4
25978
by: simonZ | last post by:
Why this don't work: Boolean test; String testValue; testValue="0"; test=System.Convert.ToBoolean(testValue); How can I convert string to boolean?
15
8068
by: angellian | last post by:
Sorry to raise a stupid question but I tried many methods which did work. how can I conserve the initial zero when I try to convert STR(06) into string in SQL statment? It always gives me 6 instead of 06. Thanks a lot.
21
3744
by: Geoff Cox | last post by:
Hello I have a TINYINT(1) field, group1, in a mysql data base and the value is 1 but php if ($row == "1") { does not work. What should I be writing here?
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.