473,796 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7220
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******** *****@TK2MSFTNG P10.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********@hot mail.com> wrote in message
news:uU******** ******@tk2msftn gp13.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******** *****@TK2MSFTNG P10.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("Upd ate myTable SET EQ=@EQ", _sqlcon)
_com.Parameters .Add(New SqlParameter("@ EQ", SqlDBType.Bit)) .Value =
dr("EQ")
_com.ExecuteNon Query

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(d r("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******** *****@TK2MSFTNG P10.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@localhos t> wrote in message
news:Op******** ******@tk2msftn gp13.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("Upd ate myTable SET EQ=@EQ", _sqlcon)
_com.Parameters .Add(New SqlParameter("@ EQ", SqlDBType.Bit)) .Value =
dr("EQ")
_com.ExecuteNon Query

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(d r("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******** *****@TK2MSFTNG P10.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
4267
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
2574
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 True:" both lead to the conditional being executed, but True == 9 -> False, that this would be true was not obvious to me -- "True is True" is True, while "9 is True" is false even though 9 evaluates to True.) Anyhow, in doing my tests, I...
35
3401
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
2485
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 "counted" string and data in a binary file. Given... #include <stdio.h> int main(int argc, char *argv) { char bstring;
48
30178
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
8424
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 I want to do is to allow the user to click on the row that corresponds to the correct address, and have the code behind populate the form's Address1, Address2 etc. controls with the relevant data items. I put the code for this into the...
59
4598
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 So I changed my test for the obvious "if type(v) is bool", but I still find it confusing that "0 in " returns True
30
3160
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 not. Actually, True should be equal to anything but False, null, and 0. Is there a workaround for this? Or do I need to change all my comparisons to = 1 instead of = true? response.write True = 1 'prints False response.write True = 0 ...
71
33238
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 (TRUE) #define TRUE (1)
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9525
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
10452
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
10221
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9050
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...
0
6785
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
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.