469,903 Members | 1,525 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

date parameters and null

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

Dim objCmd as New SqlCommand(CommandText,objConn)

with objCmd.Parameters
.Add("@client",SqlDbType.Char,50).value = "the companies next test"
.Add("@dateposted",SqlDbType.datetime).value = dateposted.text
end with

objCmd.ExecuteNonQuery
************************************************** **************************

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 1963
You probably can use DBNull.Value if you want to specify "Null".

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

Dim objCmd as New SqlCommand(CommandText,objConn)

with objCmd.Parameters
.Add("@client",SqlDbType.Char,50).value = "the companies next test"
.Add("@dateposted",SqlDbType.datetime).value = dateposted.text
end with

objCmd.ExecuteNonQuery
************************************************** **************************
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.org> wrote in message
news:O9**************@TK2MSFTNGP14.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**********@ftsolutions.com> wrote in message
news:OB**************@TK2MSFTNGP11.phx.gbl...
I have the following code:

************************************************** *************************
Dim CommandText as String = "INSERT INTO ftsolutions.dbo.position
(client,dateposted) VALUES ( @client,@dateposted)"

Dim objCmd as New SqlCommand(CommandText,objConn)

with objCmd.Parameters
.Add("@client",SqlDbType.Char,50).value = "the companies next test"
.Add("@dateposted",SqlDbType.datetime).value = dateposted.text
end with

objCmd.ExecuteNonQuery

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

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.Parameters
.Add("@client",SqlDbType.Char,50).value = "the companies next test"
if dateposted.text <> "" then
.Add("@dateposted",SqlDbType.datetime).value = dateposted.text
else
.Add("@dateposted",SqlDbType.datetime).value = getdate()
End if
end with
"tshad" wrote:
"Girish Bharadwaj" <gi*****@mvps.org> wrote in message
news:O9**************@TK2MSFTNGP14.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**********@ftsolutions.com> wrote in message
news:OB**************@TK2MSFTNGP11.phx.gbl...
I have the following code:

************************************************** *************************
Dim CommandText as String = "INSERT INTO ftsolutions.dbo.position
(client,dateposted) VALUES ( @client,@dateposted)"

Dim objCmd as New SqlCommand(CommandText,objConn)

with objCmd.Parameters
.Add("@client",SqlDbType.Char,50).value = "the companies next test"
.Add("@dateposted",SqlDbType.datetime).value = dateposted.text
end with

objCmd.ExecuteNonQuery

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

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***@discussions.microsoft.com> wrote in message
news:E3**********************************@microsof t.com...
with objCmd.Parameters
.Add("@client",SqlDbType.Char,50).value = "the companies next test"
if dateposted.text <> "" then
.Add("@dateposted",SqlDbType.datetime).value = dateposted.text
else
.Add("@dateposted",SqlDbType.datetime).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.datetime 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.org> wrote in message
news:O9**************@TK2MSFTNGP14.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**********@ftsolutions.com> wrote in message
> news:OB**************@TK2MSFTNGP11.phx.gbl...
>> I have the following code:
>>
> ************************************************** *************************
>> Dim CommandText as String = "INSERT INTO ftsolutions.dbo.position
>> (client,dateposted) VALUES ( @client,@dateposted)"
>>
>> Dim objCmd as New SqlCommand(CommandText,objConn)
>>
>> with objCmd.Parameters
>> .Add("@client",SqlDbType.Char,50).value = "the companies next test"
>> .Add("@dateposted",SqlDbType.datetime).value = dateposted.text
>> end with
>>
>> objCmd.ExecuteNonQuery
>>
> ************************************************** **************************
>>
>> 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 RequiredFieldValidator and a
RegularExpressionValidator on the text box. Then you don't need the check in
your code any more.

"tshad" wrote:
"vinay" <vi***@discussions.microsoft.com> wrote in message
news:E3**********************************@microsof t.com...
with objCmd.Parameters
.Add("@client",SqlDbType.Char,50).value = "the companies next test"
if dateposted.text <> "" then
.Add("@dateposted",SqlDbType.datetime).value = dateposted.text
else
.Add("@dateposted",SqlDbType.datetime).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.datetime 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.org> wrote in message
news:O9**************@TK2MSFTNGP14.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**********@ftsolutions.com> wrote in message
> news:OB**************@TK2MSFTNGP11.phx.gbl...
>> I have the following code:
>>
> ************************************************** *************************
>> Dim CommandText as String = "INSERT INTO ftsolutions.dbo.position
>> (client,dateposted) VALUES ( @client,@dateposted)"
>>
>> Dim objCmd as New SqlCommand(CommandText,objConn)
>>
>> with objCmd.Parameters
>> .Add("@client",SqlDbType.Char,50).value = "the companies next test"
>> .Add("@dateposted",SqlDbType.datetime).value = dateposted.text
>> end with
>>
>> objCmd.ExecuteNonQuery
>>
> ************************************************** **************************
>>
>> 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Leonardo Almeida | last post: by
1 post views Thread by Michael Albanese | last post: by
7 posts views Thread by Arek | last post: by
5 posts views Thread by Řyvind Isaksen | last post: by
2 posts views Thread by Stevienashaa | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.