all groups > vb.net data > july 2007 >
You're in the

vb.net data

group:

how to pass a <null>to a stored procedure parameter?


how to pass a <null>to a stored procedure parameter? Brad Pears
7/6/2007 4:29:09 PM
vb.net data:
I have a vb.net 2005 project using an sql server 2000 backend db. I want to
be able to insert null values into two of the date fields on new inserts
(there may be a valid date - but possibly it will be empty). These two
fields are defined as "smalldatetime" in sql server.

In my vb.net code, I am trying to set up the date variable I wan tto pass as
follows but I keep getting an error indicating that "value of type
'System.DBNull' can not be converted to 'Date'.

dim dtDate as date =system.DbNull.value

How do I get a date variable to be initialized to null so I can pass it to
my stored procedure?

Thanks, Brad

Re: how to pass a <null>to a stored procedure parameter? William (Bill) Vaughn
7/7/2007 7:00:49 PM
To pass a NULL value to a Parameter in VB.NET pass Nothing, in C# pass Null.
Another approach that I discuss in the book is to set the default value of
the Parameter to NULL. In this case you simply don't add the Parameter to
the Parameters collection and the default value is assumed.

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------

[quoted text, click to view]

Re: how to pass a <null>to a stored procedure parameter? Scott M.
7/7/2007 7:04:59 PM
If I'm not mistaken (and I could be), if you insert a record but omit
certain fields, then those fields will become null in the database. For
example,

Dim dtDate as Date = CDate(txtDate.text,Date)

Now, if txtDate.text were to be an empty string, then dtDate would have no
value. Later, when you insert dtDate into your database, it will become
null.

-Scott


[quoted text, click to view]

AddThis Social Bookmark Button