473,503 Members | 1,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Insert statement for the DateTime field

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

Nov 21 '05 #1
4 4590
"kd" <kd@discussions.microsoft.com> schrieb:
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"


Instead of building the command strings yourself, use a parameterized
command object.

Using Parameters with a 'DataAdapter'
<URL:http://msdn.microsoft.com/library/en-us/cpguide/html/cpconusingparameterswithdataadapters.asp>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2
KD,

I had it in OleDB right before my eyes so have a look at this I changed it
to SQL however watch typos

\\\
cmd.CommandText = "INSERT INTO Tab1 (name, dateval) VALUES (@MyName,
@MyDat)"
cmd.Parameters.Add _
(New SqlParameter("@MyName", SqlDbType.VarWChar))
cmd.Parameters.Add _
(New SqlParameter("@MyDat", SqlDbType.DateTime))
cmd.Parameters(0).Value = "Cor"
cmd.Parameters(1).Value = CDate( "16/03/2005 14:20") 'in Europe
cmd.ExecuteNonQuery
////

I hope this helps,

Cor
Nov 21 '05 #3

might have to change it to
str = "#2005/03/16 14:20#"

"=?Utf-8?B?a2Q=?=" <kd@discussions.microsoft.com> wrote in
news:CF**********************************@microsof t.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
Nov 21 '05 #4
J L
Yes, must use # to enclose the date. Or create parameters.

John

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

might have to change it to
str = "#2005/03/16 14:20#"

"=?Utf-8?B?a2Q=?=" <kd@discussions.microsoft.com> wrote in
news:CF**********************************@microso ft.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()
...
...


Nov 21 '05 #5

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

Similar topics

2
13366
by: george | last post by:
This is like the bug from hell. It is kind of hard to explain, so please bear with me. Background Info: SQL Server 7.0, on an NT box, Active Server pages with Javascript, using ADO objects. ...
9
3449
by: Martin | last post by:
Hello, I'm new with triggers and I can not find any good example on how to do the following: I have two tables WO and PM with the following fields: WO.WONUM, VARCHAR(10) WO.PMNUM,...
6
16245
by: FatboyCanteen | last post by:
When I using dataset to append a null value to the datetime field. It throw a error -> can not convert db.null to system.date Can there is any standard to pass a Null value to the DateTime...
2
12946
by: Tim::.. | last post by:
Can someone please tell me why I keep getting the following error from the code below! Error: INSERT statement conflicted with COLUMN FOREIGN KEY constraint...
3
3647
by: | last post by:
I have a datetime field in table. I want to insert the current time of database server to the table thr ASP.NET (C#). I have dataset to do the insert: DataSet1.TableNameRow rowNew =...
3
3426
by: Shapper | last post by:
Hello, I have created 3 functions to insert, update and delete an Access database record. The Insert and the Delete code are working fine. The update is not. I checked and my database has all...
6
3439
by: rn5a | last post by:
During registration, users are supposed to enter the following details: First Name, Last Name, EMail, UserName, Password, Confirm Password, Address, City, State, Country, Zip & Phone Number. I am...
0
2135
ak1dnar
by: ak1dnar | last post by:
There is a Error getting while i am entering records using this jsp file. <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> <%@ include...
2
966
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
I have a database field of type datetime. This field should store a system time when a record is inserted. The other fields in the record are bound to a control and I have no problems with them. I...
0
7203
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,...
0
7282
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,...
1
6995
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...
0
5581
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,...
0
3168
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...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1515
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 ...
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
389
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...

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.