Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > .NET Framework 2.0
|
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 2nd, 2006, 05:54 PM
Authorized User
 
Join Date: Jun 2004
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default passing a null value thru sql parameter

Hi There,

My latest problem is passing a null value to a stored procedure thru a VB .NET class. The date column in the sql server table can take a null. The procedure works fine when running thru a sql query window in sql server (ie exec procname <i>par_list</i>).

The problem is when calling the proc thru vb code, I get an error when passing a null value after declaring the parameter to be a datetime.

The code below generates an error for cmd.ExecuteNonQuery() if prmDOB is a null value or nothing. It works fine if there is an actual date, but the table can hold a null and I would like to keep it that way.

Dim prmDOB As SqlParameter = New SqlParameter("@DOB", SqlDbType.DateTime)

        prmDOB.IsNullable = True
        prmDOB.Direction = ParameterDirection.Input
        prmDOB.Value = DOB
        cmd.Parameters.Add(prmDOB)

        cmd.ExecuteNonQuery()

Thanks for listening and the prior help.

Sincerely,

Paul O

 
Old May 3rd, 2006, 12:56 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Paul,

You need to pass DBNull.Value instead:

If DOB IsNot Nothing Then
   prmDOB.Value = DOB
Else
   prmDOB.Value = DBNull.Value
End If

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004





Similar Threads
Thread Thread Starter Forum Replies Last Post
Sql Server Parameter Passing ctranjith SQL Language 4 October 8th, 2004 12:35 AM
difficulty in passing parameter to SP in sql happytony Classic ASP Databases 1 July 11th, 2004 05:00 AM
null value for parameter schockp VB Databases Basics 2 October 31st, 2003 08:52 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.