Insert DateTime Value In DB Table?

A

Arpan

A SQL Server 2005 DB table has 4 columns namely "ID" (IDENTITY int
column), "FirstName" (varchar(50)), "LastName" (varchar(50)) & "DOB"
(datetime). Now when I am trying to add a new row using the following
INSERT statement:

sqlDapter.InsertCommand = New SqlCommand
sqlDapter.InsertCommand.CommandText = "INSERT INTO UserDetailss
VALUES('bobby','simpson','" & Now() & "')"
sqlDapter.InsertCommand.Connection = sqlConn
sqlDapter.Update(dSet, "UserDetails")
'Response.Write("INSERT INTO UserDetailss VALUES('bobby','simpson','" &
Now() & "')"

the following error gets generated pointing to the Update line:

The conversion of a char data type to a datetime data type resulted in
an out-of-range datetime value.

I even tried using the different DateTime Format functions but none of
them work. WHere am I going wrong?

Thanks

Arpan
 
K

Kevin Spencer

DateTime.Now is a DateTime value, not a string. Try using
DateTime.Now.ToString()

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Orange you bland I stopped splaying bananas?
 
A

Arpan

DateTime.Now is a DateTime value, not a string. Try using
DateTime.Now.ToString()

Kevin, DateTime.Now.ToString() still generates the same out-of-range
error & why wouldn't it? The data type of the column named "DOB" in the
DB table (& hence in the DataSet as well) is "datetime" which means SQL
Server is expecting a datetime record in that column; so converting it
to string - how will that work?

Please correct me if I am wrong.

Thanks,

Regards,

Arpan
 
J

Juan T. Llibre

Is the date format whch you are trying to insert the same
format as your SQL Server's default date format ?
 
A

Arpan

Is the date format whch you are trying to insert the same
format as your SQL Server's default date format ?

Juan, the datetime records already present in SQL Server look like
this:

19/08/2006 11:17:45 AM

& when I do a

Response.Write(Now())

the output is exactly the same i.e.

19/08/2006 11:17:45 AM

So I guess they are the same, aren't they?

Thanks,

Regards,

Arpan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top