473,767 Members | 2,152 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

date parameters and null

I have the following code:
*************** *************** *************** *************** ***************
Dim CommandText as String = "INSERT INTO ftsolutions.dbo .position
(client,datepos ted) VALUES ( @client,@datepo sted)"

Dim objCmd as New SqlCommand(Comm andText,objConn )

with objCmd.Paramete rs
.Add("@client", SqlDbType.Char, 50).value = "the companies next test"
.Add("@datepost ed",SqlDbType.d atetime).value = dateposted.text
end with

objCmd.ExecuteN onQuery
*************** *************** *************** *************** *************** *

My problem is that if the dateposted.text is blank, then I get an error
saying date is invalid. If I take out the dateposted line all together (as
well as in the insert statement), it uses todays date - which is what I want
as I have getdate() as the default.

How do I use a parameter and allow it to be null so it takes the default?

Thanks,

Tom.
Nov 18 '05 #1
5 2152
You probably can use DBNull.Value if you want to specify "Null".

--
Girish Bharadwaj
http://msmvps.com/gbvb
"tshad" <ts**********@f tsolutions.com> wrote in message
news:OB******** ******@TK2MSFTN GP11.phx.gbl...
I have the following code:
*************** *************** *************** *************** *************** Dim CommandText as String = "INSERT INTO ftsolutions.dbo .position
(client,datepos ted) VALUES ( @client,@datepo sted)"

Dim objCmd as New SqlCommand(Comm andText,objConn )

with objCmd.Paramete rs
.Add("@client", SqlDbType.Char, 50).value = "the companies next test"
.Add("@datepost ed",SqlDbType.d atetime).value = dateposted.text
end with

objCmd.ExecuteN onQuery
*************** *************** *************** *************** *************** *
My problem is that if the dateposted.text is blank, then I get an error
saying date is invalid. If I take out the dateposted line all together (as well as in the insert statement), it uses todays date - which is what I want as I have getdate() as the default.

How do I use a parameter and allow it to be null so it takes the default?

Thanks,

Tom.

Nov 18 '05 #2
"Girish Bharadwaj" <gi*****@mvps.o rg> wrote in message
news:O9******** ******@TK2MSFTN GP14.phx.gbl...
You probably can use DBNull.Value if you want to specify "Null".
But how would I handle that with the Parameters statement?

I want to allow the user to put a date in, but if he doesn't I want to use
getdate(), which is the default on the field.

Tom.

--
Girish Bharadwaj
http://msmvps.com/gbvb
"tshad" <ts**********@f tsolutions.com> wrote in message
news:OB******** ******@TK2MSFTN GP11.phx.gbl...
I have the following code:

*************** *************** *************** *************** ***************
Dim CommandText as String = "INSERT INTO ftsolutions.dbo .position
(client,datepos ted) VALUES ( @client,@datepo sted)"

Dim objCmd as New SqlCommand(Comm andText,objConn )

with objCmd.Paramete rs
.Add("@client", SqlDbType.Char, 50).value = "the companies next test"
.Add("@datepost ed",SqlDbType.d atetime).value = dateposted.text
end with

objCmd.ExecuteN onQuery

*************** *************** *************** *************** *************** *

My problem is that if the dateposted.text is blank, then I get an error
saying date is invalid. If I take out the dateposted line all together

(as
well as in the insert statement), it uses todays date - which is what I

want
as I have getdate() as the default.

How do I use a parameter and allow it to be null so it takes the default?

Thanks,

Tom.


Nov 18 '05 #3
with objCmd.Paramete rs
.Add("@client", SqlDbType.Char, 50).value = "the companies next test"
if dateposted.text <> "" then
.Add("@datepost ed",SqlDbType.d atetime).value = dateposted.text
else
.Add("@datepost ed",SqlDbType.d atetime).value = getdate()
End if
end with
"tshad" wrote:
"Girish Bharadwaj" <gi*****@mvps.o rg> wrote in message
news:O9******** ******@TK2MSFTN GP14.phx.gbl...
You probably can use DBNull.Value if you want to specify "Null".


But how would I handle that with the Parameters statement?

I want to allow the user to put a date in, but if he doesn't I want to use
getdate(), which is the default on the field.

Tom.

--
Girish Bharadwaj
http://msmvps.com/gbvb
"tshad" <ts**********@f tsolutions.com> wrote in message
news:OB******** ******@TK2MSFTN GP11.phx.gbl...
I have the following code:

*************** *************** *************** *************** ***************
Dim CommandText as String = "INSERT INTO ftsolutions.dbo .position
(client,datepos ted) VALUES ( @client,@datepo sted)"

Dim objCmd as New SqlCommand(Comm andText,objConn )

with objCmd.Paramete rs
.Add("@client", SqlDbType.Char, 50).value = "the companies next test"
.Add("@datepost ed",SqlDbType.d atetime).value = dateposted.text
end with

objCmd.ExecuteN onQuery

*************** *************** *************** *************** *************** *

My problem is that if the dateposted.text is blank, then I get an error
saying date is invalid. If I take out the dateposted line all together

(as
well as in the insert statement), it uses todays date - which is what I

want
as I have getdate() as the default.

How do I use a parameter and allow it to be null so it takes the default?

Thanks,

Tom.



Nov 18 '05 #4
"vinay" <vi***@discussi ons.microsoft.c om> wrote in message
news:E3******** *************** ***********@mic rosoft.com...
with objCmd.Paramete rs
.Add("@client", SqlDbType.Char, 50).value = "the companies next test"
if dateposted.text <> "" then
.Add("@datepost ed",SqlDbType.d atetime).value = dateposted.text
else
.Add("@datepost ed",SqlDbType.d atetime).value = getdate()
End if
end with
That will work fine. But I was curious why I get an error that says "String
was not recognized as a valid DateTime." if the value is blank. I would
have thought the program would have seen the type as SqlDbType.datet ime and
the field as blank and set it to Null, which would have allowed Sql Server
to use the default value (getdate()).

Thanks,

Tom

"tshad" wrote:
"Girish Bharadwaj" <gi*****@mvps.o rg> wrote in message
news:O9******** ******@TK2MSFTN GP14.phx.gbl...
> You probably can use DBNull.Value if you want to specify "Null".


But how would I handle that with the Parameters statement?

I want to allow the user to put a date in, but if he doesn't I want to
use
getdate(), which is the default on the field.

Tom.
>
> --
> Girish Bharadwaj
> http://msmvps.com/gbvb
> "tshad" <ts**********@f tsolutions.com> wrote in message
> news:OB******** ******@TK2MSFTN GP11.phx.gbl...
>> I have the following code:
>>
> *************** *************** *************** *************** ***************
>> Dim CommandText as String = "INSERT INTO ftsolutions.dbo .position
>> (client,datepos ted) VALUES ( @client,@datepo sted)"
>>
>> Dim objCmd as New SqlCommand(Comm andText,objConn )
>>
>> with objCmd.Paramete rs
>> .Add("@client", SqlDbType.Char, 50).value = "the companies next test"
>> .Add("@datepost ed",SqlDbType.d atetime).value = dateposted.text
>> end with
>>
>> objCmd.ExecuteN onQuery
>>
> *************** *************** *************** *************** *************** *
>>
>> My problem is that if the dateposted.text is blank, then I get an
>> error
>> saying date is invalid. If I take out the dateposted line all
>> together
> (as
>> well as in the insert statement), it uses todays date - which is what
>> I
> want
>> as I have getdate() as the default.
>>
>> How do I use a parameter and allow it to be null so it takes the
>> default?
>>
>> Thanks,
>>
>> Tom.
>>
>>
>
>


Nov 18 '05 #5
An empty string is not the same as a NULL value in SQL and SQLServer won't
automatically convert from an empty string to NULL, nor will it be able to
parse an empty string into a DateTime value. NULL is a special value that can
be stored in a DateTime column as long as the database schema specifies that
the column is NULLable. It sounds like your DateTime column may not be
NULLable, but you have a default value specified to set the value to
GetDate() when a NULL value is specified.

By the way, you could check for an empty string in the stored procedure
instead of in your VB code, then any code that uses the SProc could avoid
having the conditional statement. Also, if you're going to allow an empty
date field, I'd recommend going the extra distance and stripping out any
whitespace before checking for an empty string. However, I think the best
way, if the default value in the database is today's date, would be to just
fill in the text box with today's date and put a RequiredFieldVa lidator and a
RegularExpressi onValidator on the text box. Then you don't need the check in
your code any more.

"tshad" wrote:
"vinay" <vi***@discussi ons.microsoft.c om> wrote in message
news:E3******** *************** ***********@mic rosoft.com...
with objCmd.Paramete rs
.Add("@client", SqlDbType.Char, 50).value = "the companies next test"
if dateposted.text <> "" then
.Add("@datepost ed",SqlDbType.d atetime).value = dateposted.text
else
.Add("@datepost ed",SqlDbType.d atetime).value = getdate()
End if
end with


That will work fine. But I was curious why I get an error that says "String
was not recognized as a valid DateTime." if the value is blank. I would
have thought the program would have seen the type as SqlDbType.datet ime and
the field as blank and set it to Null, which would have allowed Sql Server
to use the default value (getdate()).

Thanks,

Tom


"tshad" wrote:
"Girish Bharadwaj" <gi*****@mvps.o rg> wrote in message
news:O9******** ******@TK2MSFTN GP14.phx.gbl...
> You probably can use DBNull.Value if you want to specify "Null".

But how would I handle that with the Parameters statement?

I want to allow the user to put a date in, but if he doesn't I want to
use
getdate(), which is the default on the field.

Tom.

>
> --
> Girish Bharadwaj
> http://msmvps.com/gbvb
> "tshad" <ts**********@f tsolutions.com> wrote in message
> news:OB******** ******@TK2MSFTN GP11.phx.gbl...
>> I have the following code:
>>
> *************** *************** *************** *************** ***************
>> Dim CommandText as String = "INSERT INTO ftsolutions.dbo .position
>> (client,datepos ted) VALUES ( @client,@datepo sted)"
>>
>> Dim objCmd as New SqlCommand(Comm andText,objConn )
>>
>> with objCmd.Paramete rs
>> .Add("@client", SqlDbType.Char, 50).value = "the companies next test"
>> .Add("@datepost ed",SqlDbType.d atetime).value = dateposted.text
>> end with
>>
>> objCmd.ExecuteN onQuery
>>
> *************** *************** *************** *************** *************** *
>>
>> My problem is that if the dateposted.text is blank, then I get an
>> error
>> saying date is invalid. If I take out the dateposted line all
>> together
> (as
>> well as in the insert statement), it uses todays date - which is what
>> I
> want
>> as I have getdate() as the default.
>>
>> How do I use a parameter and allow it to be null so it takes the
>> default?
>>
>> Thanks,
>>
>> Tom.
>>
>>
>
>


Nov 18 '05 #6

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

Similar topics

4
6182
by: Leonardo Almeida | last post by:
Hi, I have a table with the follow fields : ID - Int Date - Datetime I need to make a simple query to result the records between to dates with a single ID.
1
5149
by: Michael Albanese | last post by:
I am building an application to report on-the-job injuries and incidents. There are a lot of Date fields, some of which are optional and can be left blank by the user. I have allowed Nulls on these fields in my SQL Server DB, as well as in my stored proc. The problem is that when I submit this data i get an error that states "Input not in date format" or a similar statement when there arevnull date fields.
8
2672
by: peashoe | last post by:
I have an asp page that uses a calendar.js (pop-up) file to add an exact date format in the text field (txtDDate). My problem is I need some javascript that sets an alert that does not allow them to select today. example: var dtToday = Date() if(document.frmSoftware.txtDDate.value == dtToday) {
1
6612
by: Vern | last post by:
I'm using the Microsoft Enterprise Data Access block to call the stored procedure. One of the values the stored procedure returns is the stop date. When the stop date is null, the program crashes. I would like to be able to test for a null date, and then store the mindate value in the class property. I've tried the following, but it still crashes. if (dbCommandWrapper.GetParameterValue("@StopDt") == null) {
7
2461
by: Arek | last post by:
Hey, I am inserting values in the table: Dim sqlcomm1 As SqlCommand = New SqlCommand("INSERT INTO tblTasks (idTask, outdate) VALUES ('" & IDTask.text & "','" & txtOutdate.Text & "')", conn) sqlcomm1.ExecuteNonQuery() Query is working fine if there is value in the txtOutDate, but if user leaves the field txtOutdate empty, system insert date 1900-01-01. I want
5
10603
by: darrel | last post by:
I have the following right now to enter a date into SQL getting the data from some pull down menus: ------------------------------------------------- dim dateCCJApprovedDate as DateTime if cbx_ccjDateNone.Checked = True then dateCCJApprovedDate = ctype("", DateTime) else dateCCJApprovedDate = ctype(ddl_CCJDateMonth.SelectedValue.tostring &
5
3051
by: Øyvind Isaksen | last post by:
I have a page with an optional integer-field, and one asp:calendar control. I use a stored procedure to save the data in SQL Server. When all fields contains data, the code works great! But if the user dont fill in the optional "price-field" (integer value), or if the user dont choose a date in the asp:calendar control, I get the message "Input string was not in a correct format". How do I save "Null" value if the price-field is blank,...
2
2362
by: Stevienashaa | last post by:
Hello I'm using Access 2003, and I have a query (written in SQL) which has two parameters and asks the user for two dates. This has been working fine. Today I modified the query, removing the paramerters and hard- coding some dates in, in dd/mm/yy format. To my surprise I got a different number of records returned. I then, as an experiment, change the date format to dd/mm/yyyy. This again returned a different number of records,...
0
3688
by: jans78 | last post by:
Appreciate if you all can help me to solve my Crystal Report problems First, I create some parameters and one of the parameters is Date. I set the parameter for the date is String. For example : parameter 1 - From Collection Date parameter 2 - To Collection Date I should display the results base on this selection criteria : 1) specific date - to enter specific date in FROM and TO field eg 20071227 to 20071227 2) greater or equal date -...
0
9571
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
9405
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10013
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
9960
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
8838
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
7383
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
5280
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5424
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3930
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.