473,396 Members | 1,965 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,396 software developers and data experts.

0 or 1 vs True or False

I am using VB in a VSNET 2003 Windows application. I've run into a situation
where, when trying to set a bit value in a SQL Server 2000 database I get
errors because the values extracted from a datarow return True or False. In
the snippet below, the SQL becomes "Update myTable SET EQ = True" which
fails with an error that "True" is not a valid column name? I gather that
the datarow object returns True or False for bit fields? I can always recode
this to get around the problem but I am trying to understand what the rules
are here?

========== Sample Code =============
Dim dr As DataRow
If chkEqp.Checked Then dr("EQ") = 1 Else dr("EQ") = 0

' If I need to manually do the update I use this code

"Update myTable SET EQ = " & dr("EQ")
Nov 20 '05 #1
4 7185
If CInt doesn't work (nor CDbl) try doing:

"Update myTable SET EQ = " & IIf(dr("EQ"), 1, 0)

Can't remember if SQL Server's True is -1 or 1, try -1 if 1 doesn't seem to work,
although I think both will work.

Hope it helps :)

Mythran
"Wayne Wengert" <wa***************@wengert.com> wrote in message
news:OL*************@TK2MSFTNGP10.phx.gbl...
I am using VB in a VSNET 2003 Windows application. I've run into a situation
where, when trying to set a bit value in a SQL Server 2000 database I get
errors because the values extracted from a datarow return True or False. In
the snippet below, the SQL becomes "Update myTable SET EQ = True" which
fails with an error that "True" is not a valid column name? I gather that
the datarow object returns True or False for bit fields? I can always recode
this to get around the problem but I am trying to understand what the rules
are here?

========== Sample Code =============
Dim dr As DataRow
If chkEqp.Checked Then dr("EQ") = 1 Else dr("EQ") = 0

' If I need to manually do the update I use this code

"Update myTable SET EQ = " & dr("EQ")

Nov 20 '05 #2
Thanks for that suggestion.

Wayne

"Mythran" <ki********@hotmail.com> wrote in message
news:uU**************@tk2msftngp13.phx.gbl...
If CInt doesn't work (nor CDbl) try doing:

"Update myTable SET EQ = " & IIf(dr("EQ"), 1, 0)

Can't remember if SQL Server's True is -1 or 1, try -1 if 1 doesn't seem to work, although I think both will work.

Hope it helps :)

Mythran
"Wayne Wengert" <wa***************@wengert.com> wrote in message
news:OL*************@TK2MSFTNGP10.phx.gbl...
I am using VB in a VSNET 2003 Windows application. I've run into a situation where, when trying to set a bit value in a SQL Server 2000 database I get errors because the values extracted from a datarow return True or False. In the snippet below, the SQL becomes "Update myTable SET EQ = True" which
fails with an error that "True" is not a valid column name? I gather that the datarow object returns True or False for bit fields? I can always recode this to get around the problem but I am trying to understand what the rules are here?

========== Sample Code =============
Dim dr As DataRow
If chkEqp.Checked Then dr("EQ") = 1 Else dr("EQ") = 0

' If I need to manually do the update I use this code

"Update myTable SET EQ = " & dr("EQ")


Nov 20 '05 #3
This raises a number of matters that need to be understood to arrive at the
best solution for you.

In SQL Server, the bit data type should not be considered to be boolean.
Whereas a boolean can be True or False (and nothing else), the bit data type
can be 0, 1 or NULL. To digress, the fact that it can be NULL, in my
experience, causes headaches, and I always declare a bit as NOT NULL with
default of 0.

In VB, the boolean datatype can be True oe False. Although a boolean can be
represented numerically as -1 for True and 0 for False, on should always
think of a boolean in terms of True or False and forget about the underlying
numerical values. The difference between VB and other languages that have 1
as their 'True' value is due to the historical fact that in BASIC, True is
actually calculated as Not False. If you do a bitwise NOT on 0 the result
is -1. Over the years the language has evolved so that any non 0 value will
resolve to True.

In a DotNet datatable, a SqlDBType.Bit will be interpreted as a
System.Boolean and the translation of 1 and 0 to True and False respectively
(and the reverse) is handled by the Framework.

If you do things the DotNet way, thus:

Dim _com As New SqlCommand("Update myTable SET EQ=@EQ", _sqlcon)
_com.Parameters.Add(New SqlParameter("@EQ", SqlDBType.Bit)).Value =
dr("EQ")
_com.ExecuteNonQuery

then you find that you have little or no difficulity.

If you wish to persist with a dynamic SQL string then using Math.Abs() will
help you out. There is a gotch in that Math.Abs() cannot be performed on a
boolean, so it needs to be converted to something else first, thus:

"Update myTable SET EQ=" & Math.Abs(CInt(dr("EQ"))

The result is either the absoulute value of -1 (1) or the absoulute value of
0 (0).

When updating your datarow, simply using dr("EQ") = chkEqp.Checked will
suffice, because the datatype for EQ in the datatable is System.Boolean.
"Wayne Wengert" <wa***************@wengert.com> wrote in message
news:OL*************@TK2MSFTNGP10.phx.gbl...
I am using VB in a VSNET 2003 Windows application. I've run into a situation where, when trying to set a bit value in a SQL Server 2000 database I get
errors because the values extracted from a datarow return True or False. In the snippet below, the SQL becomes "Update myTable SET EQ = True" which
fails with an error that "True" is not a valid column name? I gather that
the datarow object returns True or False for bit fields? I can always recode this to get around the problem but I am trying to understand what the rules are here?

========== Sample Code =============
Dim dr As DataRow
If chkEqp.Checked Then dr("EQ") = 1 Else dr("EQ") = 0

' If I need to manually do the update I use this code

"Update myTable SET EQ = " & dr("EQ")

Nov 20 '05 #4
Thanks for the very informative response.

Wayne

"Stephany Young" <noone@localhost> wrote in message
news:Op**************@tk2msftngp13.phx.gbl...
This raises a number of matters that need to be understood to arrive at the best solution for you.

In SQL Server, the bit data type should not be considered to be boolean.
Whereas a boolean can be True or False (and nothing else), the bit data type can be 0, 1 or NULL. To digress, the fact that it can be NULL, in my
experience, causes headaches, and I always declare a bit as NOT NULL with
default of 0.

In VB, the boolean datatype can be True oe False. Although a boolean can be represented numerically as -1 for True and 0 for False, on should always
think of a boolean in terms of True or False and forget about the underlying numerical values. The difference between VB and other languages that have 1 as their 'True' value is due to the historical fact that in BASIC, True is
actually calculated as Not False. If you do a bitwise NOT on 0 the result
is -1. Over the years the language has evolved so that any non 0 value will resolve to True.

In a DotNet datatable, a SqlDBType.Bit will be interpreted as a
System.Boolean and the translation of 1 and 0 to True and False respectively (and the reverse) is handled by the Framework.

If you do things the DotNet way, thus:

Dim _com As New SqlCommand("Update myTable SET EQ=@EQ", _sqlcon)
_com.Parameters.Add(New SqlParameter("@EQ", SqlDBType.Bit)).Value =
dr("EQ")
_com.ExecuteNonQuery

then you find that you have little or no difficulity.

If you wish to persist with a dynamic SQL string then using Math.Abs() will help you out. There is a gotch in that Math.Abs() cannot be performed on a
boolean, so it needs to be converted to something else first, thus:

"Update myTable SET EQ=" & Math.Abs(CInt(dr("EQ"))

The result is either the absoulute value of -1 (1) or the absoulute value of 0 (0).

When updating your datarow, simply using dr("EQ") = chkEqp.Checked will
suffice, because the datatype for EQ in the datatable is System.Boolean.
"Wayne Wengert" <wa***************@wengert.com> wrote in message
news:OL*************@TK2MSFTNGP10.phx.gbl...
I am using VB in a VSNET 2003 Windows application. I've run into a

situation
where, when trying to set a bit value in a SQL Server 2000 database I get errors because the values extracted from a datarow return True or False.

In
the snippet below, the SQL becomes "Update myTable SET EQ = True" which
fails with an error that "True" is not a valid column name? I gather that the datarow object returns True or False for bit fields? I can always

recode
this to get around the problem but I am trying to understand what the

rules
are here?

========== Sample Code =============
Dim dr As DataRow
If chkEqp.Checked Then dr("EQ") = 1 Else dr("EQ") = 0

' If I need to manually do the update I use this code

"Update myTable SET EQ = " & dr("EQ")


Nov 20 '05 #5

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

Similar topics

46
by: Scott Chapman | last post by:
There seems to be an inconsistency here: Python 2.3.2 (#1, Oct 3 2003, 19:04:58) on linux2 >>> 1 == True True >>> 3 == True False >>> if 1: print "true" ....
3
by: drs | last post by:
I just upgraded my Python install, and for the first time have True and False rather than 1 and 0. I was playing around at the command line to test how they work (for instance, "if 9:" and "if...
35
by: Steven Bethard | last post by:
I have lists containing values that are all either True, False or None, e.g.: etc. For a given list: * If all values are None, the function should return None.
14
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I took a C course some time ago, but I'm only now beginning to use it, for a personal pet project. My current stumbling-block is finding an efficient way to find a match between the beginning of a...
48
by: Skybuck Flying | last post by:
Hi, I came across this C code which I wanted to understand etc it looked like this: if (-1) etc It made me wonder what the result would be... true or false ? In C and Delphi
1
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What...
59
by: Pierre Quentel | last post by:
Hi all, In some program I was testing if a variable was a boolean, with this test : if v in My script didn't work in some cases and I eventually found that for v = 0 the test returned True ...
30
by: Jason | last post by:
I am fairly new to ASP--I have been using it about 2 months. I did these tests (below), and it doesn't make sense to me. False is equal to 0, and that's fine. True should be equal to 1, but it's...
71
by: David T. Ashley | last post by:
Where is the best place to define TRUE and FALSE? Are they in any of the standard include files, ever? Do any standards apply? What I've traditionally done is something like: #ifndef...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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,...
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...
0
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...
0
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,...

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.