Nulls

  • Thread starter Thread starter Jim Heavey
  • Start date Start date
J

Jim Heavey

Hello, I have a Vb.Net client and I have some fields that I am attempting
to load with a value of "null" in a parm which is passed to a stored
procedure.

In the Vb.Net client I am looking at a text box and if that text box has
nothing in it (lenght is 0), I do the following....

dim null as object = dbnull.value
cmd.parameter(@MyField).value = null

When I run, I get an expception indicating that I have not provided a value
for "@MyField". It works fine when there is a non-null value. I also
tried "", and it works, but does not render a null in the field. How to I
place nulls into a parameter in the VB.Net client?

Thanks in advance for your assistance!!!!!!
 
Hi,

If you're calling an SP through the command object, have
you ensured that the SP aceepts null as a default value
for the parameter in question?

Regards
Harsh Thakur
 
My Database column is defined to accept nulls is this what you mean about
the SP accepts null?
 
Jim,

You want:

cmd.parameter("@MyField").value = DBNull.Value;

"null" represents an unitialized reference to a class instance; DBNull is a
singleton class that represents a database NULL, which is an entirely
different thing.

--Bob
 
Hi Jim,

1. As Bob said, your assignment should be done thus:

cmd.parameter("@MyField").value = DBNull.Value;

2. What I meant was: What are you executing through the
command object? Are you executing a stored procedure? If
you are, then have you made sure that your stored
procedure accepts a NULL value for the parameter in
question. Otherwise, the stored procedure will throw an
exception even though your assignment is correct.

Your stored procedure should start with something like the
following:

CREATE PROC <Procedure Name>(
@ParameterOne <data type>,
@ParameterTwo <data type> = NULL,
)

Doing so will ensure that you can pass a NULL value to
@ParameterTwo.

HTH
Regards
Harsh Thakur
 
Hi Bob,

The code is VB.net (dim)

The "null" in VB.net does not exist anymore, I think the answers from you
and Hars toghether will solve Jims problem, but for you in vb.net is an
unitialized reference to a class instance Is Nothing

Cor
 
Back
Top