boolean and null

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can a boolean data type be null?

I'm passing a parameter to a stored procedure. It's type is defined by the
line:
....
SqlParameter paramOK = cmd.Parameters.Add("@IsOK",
SqlDbType.Bit);
....

and it's value is set using:

....
paramIsOK.Value = null;
....


but it doesn't like this (I'd like to have the null possibility if a user
has neither replied Yes nor No to a question on a form.)

many thanks

Pete
 
Italian said:
Can a boolean data type be null?

Not quite.
I'm passing a parameter to a stored procedure. It's type is defined by the
line:
...
SqlParameter paramOK = cmd.Parameters.Add("@IsOK",
SqlDbType.Bit);
...

and it's value is set using:

...
paramIsOK.Value = null;
...


but it doesn't like this (I'd like to have the null possibility if a user
has neither replied Yes nor No to a question on a form.)

C#'s 'null' is not the same thing as SQL's 'NULL'. The former is an
object reference to no object at all; the latter is a value of a
database field. You want

paramIsOK.Value = DBNull.Value;
 
Hi,

Italian Pete said:
Can a boolean data type be null?

In a DB yes , there is no bool in SQL server, there is BIT and yes, it can
accept nulls
I'm passing a parameter to a stored procedure. It's type is defined by the
line:
...
SqlParameter paramOK = cmd.Parameters.Add("@IsOK",
SqlDbType.Bit);
...

and it's value is set using:

...
paramIsOK.Value = null;
...


but it doesn't like this (I'd like to have the null possibility if a user
has neither replied Yes nor No to a question on a form.)

Of course he does not like it, If you use DBNull.Value it will be pleased :)

For your case you could have a short int , with values 1, 2, 3 where each
one mean , yes, no , no answer

I alwasy try to avoid dealing with nulls :)


cheers,
 
By definition a bool must either be true or false. However, in version 2.0
of the .NET framework, MS will implement "nullable types." With this you
would, essentially, be able to declare a tri-state bool that can be either
true, false, or (I think) null. (If not null, then undefined). If you're
interested in more info on this check MS's web site. (Sorry I don't have the
correct link, but I'd start at "http://msdn.microsoft.com/vs2005".

..ARN.
 
Back
Top