Home All Groups Group Topic Archive Search About

Insert statement for the DateTime field

Author
16 Mar 2005 11:05 AM
kd
Hi All,

I have a datetime column in a table on the SQL database. I need to insert
values into the datetime column from vb.net code. Here is my code:
dim nameval, str, qry as string
nameval = "abc"
str = "2005/03/16 14:20"
qry = "insert into tab1(name,dateval) values(" & "'" & nameval & "'," & "'"
str & "')"
....
...
ocmd.ExecuteNonQuery()
....
....

The error message that I get is as follows:
"The conversion of a char data type to a datetime data type resulted in an
out-of-range datetime value. The statement has been terminated. .Net
SqlClient Data Provider"

The problem I think is due to passing a string for a datetime field. My
question is, if I convert the string to datetype using CDate(str), then I
would have to again convert the date to string in order to form the insert
statement. So, the ultimate result will be again passing a string for the
datetime field!

I know that this is a simple syntax problem, which I don't seem to get right!

Would anybody be able to give me insert statement for the above?

Thanks.
kd

Author
9 Apr 2005 8:36 PM
Chef Groovy
might have to change it to
str = "#2005/03/16 14:20#"



Show quoteHide quote
"=?Utf-8?B?a2Q=?=" <k*@discussions.microsoft.com> wrote in
news:CFD42B61-21CC-444B-9C08-CEF950CD9125@microsoft.com:

> Hi All,
>
> I have a datetime column in a table on the SQL database. I need to
> insert values into the datetime column from vb.net code. Here is my
> code: dim nameval, str, qry as string
> nameval = "abc"
> str = "2005/03/16 14:20"
> qry = "insert into tab1(name,dateval) values(" & "'" & nameval & "',"
> & "'" str & "')"
> ...
> ..
> ocmd.ExecuteNonQuery()
> ...
> ...
>

--
---------
Der Chef Groovy
ICQ: 1529949
Author
9 Apr 2005 11:32 PM
J L
Yes, must use # to enclose the date. Or create parameters.

John

On Sat, 09 Apr 2005 20:36:15 GMT, Chef Groovy
<chefgro***@insightbb.com> wrote:

Show quoteHide quote
>
>might have to change it to
>str = "#2005/03/16 14:20#"
>
>
>
>"=?Utf-8?B?a2Q=?=" <k*@discussions.microsoft.com> wrote in
>news:CFD42B61-21CC-444B-9C08-CEF950CD9125@microsoft.com:
>
>> Hi All,
>>
>> I have a datetime column in a table on the SQL database. I need to
>> insert values into the datetime column from vb.net code. Here is my
>> code: dim nameval, str, qry as string
>> nameval = "abc"
>> str = "2005/03/16 14:20"
>> qry = "insert into tab1(name,dateval) values(" & "'" & nameval & "',"
>> & "'" str & "')"
>> ...
>> ..
>> ocmd.ExecuteNonQuery()
>> ...
>> ...
>>