1

I have a probleme with a sql query :

I would like do something like :

SELECT * FROM myTable WHERE [myBoolField] IS NULL

if i run this query, i have my results,

but i would like use parameters, so i add :

SELECT * FROM myTable WHERE [myBitField] IS @myBitField

with

New SqlParameter("@myBitField", dbnull.value)

i have an error at the @myBoolField

if i pass a value like true or false, that's work ( with " = @myBitField")

Can someone help me ?

Thanks

Testman
  • 57
  • 7

1 Answers1

1

You can only test for NULL using IS NULL. You cannot use parameters and there wouldn't really be any point.

DBNull.Value can be used when inserting or comparing values in code, but not on WHERE conditions since NULL is not equal to even another NULL.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
  • oh... okay, but dbnull.value is NULL, so it's strange, no ? And as you said you can use it for insert for example – Testman Jul 30 '15 at 10:20
  • @Testman when using parameters the query is usually parsed beforehand and the parameters are not just substituted into the string. The parser will see `IS @parameter` and say "nope, that's not acceptable" and that's it. – Sami Kuhmonen Jul 30 '15 at 10:22