473,566 Members | 3,309 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,datev al) values(" & "'" & nameval & "'," & "'"
str & "')"
....
...
ocmd.ExecuteNon Query()
....
....

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 4593
"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,datev al) values(" & "'" & nameval & "'," &
"'"
str & "')"
...
..
ocmd.ExecuteNon Query()
...
...

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/cpconusingparam eterswithdataad apters.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.VarWC har))
cmd.Parameters. Add _
(New SqlParameter("@ MyDat", SqlDbType.DateT ime))
cmd.Parameters( 0).Value = "Cor"
cmd.Parameters( 1).Value = CDate( "16/03/2005 14:20") 'in Europe
cmd.ExecuteNonQ uery
////

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******** *************** ***********@mic rosoft.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,datev al) values(" & "'" & nameval & "',"
& "'" str & "')"
...
..
ocmd.ExecuteNon Query()
...
...


--
---------
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********@ins ightbb.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******* *************** ************@mi crosoft.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,datev al) values(" & "'" & nameval & "',"
& "'" str & "')"
...
..
ocmd.ExecuteNon Query()
...
...


Nov 21 '05 #5

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

Similar topics

2
13379
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. I'm inserting simple records into a table. But one insert command is placing 2 or 3 records into the table. The 'extra' records, have the same...
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, VARCHAR(10) WO.PROBLEMCODE, VARCHAR(8)
6
16248
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 Field! (It is successful to pass the Null Value to other type of Field)
2
12953
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 'FK_tblOfficePageContent_tblPageContent'. The conflict occurred in database 'CPNCMS', table 'tblPageContent', column 'pageID'. The statement has been terminated. I cant seem to see...
3
3652
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 = (DataSet1.TableNameRow)DataSet1.TableName.NewRow(); rowNew.DateTimeField = System.DateTime.Now;
3
3434
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 the necessary records in it when testing it. I get the error "No value given for one or more required parameters." when I try to update the...
6
3445
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 using MS-Access 2000 database table for this app. Note that the datatype of all the fields mentioned above are Text. Apart from the above columns,...
0
2140
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 file="../Connections/conn.jsp" %> <% // *** Edit Operations: declare variables // set the form action variable String MM_editAction =...
2
969
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 tried setting the datetime field with a default value using getdate(), but it still does not work. How can I use a statement similar these beow to...
0
7673
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...
1
7645
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...
0
6263
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...
1
5485
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...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2085
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
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.