473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Passing Null value to SQL

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
Nov 22 '05 #1
6 7963
amber <am***@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #2
Try using DBNull

--
Sriram Krishnan

http://www.dotnetjunkies.com/weblog/sriram
"amber" <am***@discussions.microsoft.com> wrote in message
news:12**********************************@microsof t.com...
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

Nov 22 '05 #3
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
Nov 22 '05 #4
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.
"amber" wrote:
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

Nov 22 '05 #5
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.
"amber" wrote:
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

Nov 22 '05 #6
Raj
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

"amber" wrote:
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

Nov 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: sam pal | last post by:
I have the following programs and please clear my doubts. Passing the value by reference is same as pointer by reference. Is this true? What is the difference between display and display2? How...
5
by: amber | last post by:
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...
3
by: Michael Tissington | last post by:
I have a DLL written in C++ and I'm calling it from C# One of the parameters for a function in the DLL is a DATE value or NULL. How do I pass NULL to my DLL from C#? -- Michael Tissington...
2
by: PK | last post by:
Hi, I have an application that opens a Crystal report document and passes in a value to a parameter in the report (pointing to an Oracle DB). However, if I want to pass a "null" value to retrieve...
5
by: Jet | last post by:
Hi all, I had a code like the follow: class A{ ... public void get(int i,params OleDbParameter p) { if((p!=null)||(p.Length>0)) { // Do some work }
1
by: John Hoge | last post by:
Is it possible to pass a null value to a stored procedure in .net? I have a search Sproc that can take one of two numbers to search on, but not both. I use the code below to pass a null value to...
1
by: David Shorthouse | last post by:
Hey folks, I am attempting to pass null as the input value from a series of textboxes if the user does not input a value prior to submit. To try and do this, I am using a vbscript function on...
1
by: RvGrah | last post by:
I have a TabelAdapter that has an int column that allows null (no lectures please, I have my reasons). When I use intellisense to fill in the parameters in the InserQuery method, it shows the...
4
by: mwanstall | last post by:
Hi All, I have started tearing my hair out over this problem! I am pulling some data from a table and passing it as variables into a function in Access. One of the variables I'm passing through...
13
by: seaside | last post by:
I have a method function appendChildNode( AST $aNewChild ) { ... } <<< where AST is a class. If I pass null, PHP renders this message: Catchable fatal error: Argument 1 passed to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.