473,661 Members | 2,431 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7985
amber <am***@discussi ons.microsoft.c om> 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.co m>
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***@discussi ons.microsoft.c om> wrote in message
news:12******** *************** ***********@mic rosoft.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.DateApprove d = Convert.DBNull. value

I get the error:

An unhandled exception of type 'System.Invalid CastException' occurred in
microsoft.visua lbasic.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.DateApprove d 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.DateApprove d is a object of type date,
you can cLP.DateApprove d = 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.publi c.dotnet.framew ork.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.DateApprove d = Convert.DBNull. value

I get the error:

An unhandled exception of type 'System.Invalid CastException' occurred in
microsoft.visua lbasic.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.DateApprove d 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.DateApprove d is a object of type date,
you can cLP.DateApprove d = 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.publi c.dotnet.framew ork.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.DateApprove d = Convert.DBNull. value

I get the error:

An unhandled exception of type 'System.Invalid CastException' occurred in
microsoft.visua lbasic.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.DateApprove d = Convert.DBNull. value

I get the error:

An unhandled exception of type 'System.Invalid CastException' occurred in
microsoft.visua lbasic.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
9434
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 can I change display1 to value by reference if there is difference? void display(int &i){// Passing value by reference
5
3968
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 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...
3
5766
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 Oaklodge Technologies http://www.oaklodge.com/technology
2
19331
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 all records that have no data for the specified field that the parameter is a condition of (ie. SELECT * from <table> where <field> is null"), it returns nothing. I think CR is building the SQL statement like "SELECT * from <table> where <field>...
5
3137
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
13624
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 the sproc if a TextBox is empty. SqlCommand getLesNumbers = new SqlCommand(); getLesNumbers.CommandText="usp_getLesNumbers"; getLesNumbers.CommandType = CommandType.StoredProcedure; getLesNumbers.Connection = msoConn;
1
11470
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 this asp as follows: CommentsAdd = IIf(Request.Form("Comments")="",NULL,Request.Form("Comments")) I was hoping this would convert those emptystring textboxes to null.
1
2498
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 needed value as ?int, with the question mark. But if the combo box that the value is coming from has a null value the query throws an exception. How can I configure my query to pass the null value when necessary? Thanks, Bob
4
10298
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 can be Null, so to avoid trying to pass a Null value to a function as a string (which I gather Access wouldn't like) I created an IIF function inside the parameters so if the value was null, it passed the string "NULLDEPOT" into the function, and I...
13
4720
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 AST::appendChildNode()
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8855
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8758
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7364
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1986
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.