Passing Null value to SQL

  • Thread starter Thread starter Sriram Krishnan
  • Start date Start date
Hello,
I'm not sure if this question should go in the SQL forum or this one...

I'm using VB.NET and SQL Server.
I'm using stored procedures to update and create new records in my database.
On the form I'm working on, I have 2 date fields.
I want the user to have the option of leaving these blank.
I do not want default values.
I'm getting error messages not matter what I try.
What syntax can I use to get VB to pass this null value instead of a date,
into SQL??

Thanks!
amber
 
amber said:
I'm not sure if this question should go in the SQL forum or this one...

I'm using VB.NET and SQL Server.
I'm using stored procedures to update and create new records in my database.
On the form I'm working on, I have 2 date fields.
I want the user to have the option of leaving these blank.
I do not want default values.
I'm getting error messages not matter what I try.
What syntax can I use to get VB to pass this null value instead of a date,
into SQL??

Use a parameter and set the value to DBNull.Value.
 
If I use the code:
cLP.DateApproved = Convert.DBNull.value

I get the error:

An unhandled exception of type 'System.InvalidCastException' occurred in
microsoft.visualbasic.dll

Additional information: Cast from type 'DBNull' to type 'Date' is not valid.

Amber
 
Amber,
I'm not sure how your object is configured but the problem may be your
cLP.DateApproved is a date type which you can't assign a type of
DBNull.Value. You'll have to assign the underlying db object (the column) the
value of DBNull.Value. If indeed cLP.DateApproved is a object of type date,
you can cLP.DateApproved = Date.MinValue which is still a date value, just a
real old one. Unfortunately, that won't put Null into your table. Also
remember that your column in the table must allow nulls.

Try posting your question in microsoft.public.dotnet.framework.adonet -
where the gurus of ado hang out. Someone could probably answer this one
correctly in 5 minutes.
 
You cannot set a DBNull value to Datefield, Instead you are supposed to use
store procedure and through Parameter you have to set the DBNull to the
required filed.

Raj
 
Back
Top