Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > .NET Framework 2.0
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
.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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 2nd, 2006, 05:54 PM
Authorized User
 
Join Date: Jun 2004
Location: , , USA.
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

Reply With Quote
  #2 (permalink)  
Old May 3rd, 2006, 12:56 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:17 PM.


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