Click here to Skip to main content
15,915,509 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I am trying to save date in my database' datetime field.
My variable catches date as
VB
vrDteTimePprBgn As Date = Today.Date

then I save it as
VB
drNewRowMCQsAns.Item("DTE") = vrDteTimePprBgn

At runtime, I get error
There was an error in a part of the date format. [ Expression (if known) = ]
Please advise.
Thanks
Posted
Updated 14-Aug-11 0:16am
v2

1 solution

Firstly
Although Today.Date works, I think it is more normal to get Date.Today
secondly, you are trying to pass a VB.net Date object into the DB, which i don't know what type of db etc, you are using, so you will need to look at the suitable methods to convert to db DateTime.

The easiest thing to do is may just to pass in a string representation of the date to the db, and let it take care of things, also unless you need the variable else where, just bin it and work directly;

VB
drNewRowMCQsAns.item("DTE") = Date.Today.ToShortDateString


Depending also on the type of DB you are using, you may have to append a set of #'s to the string e.g.
VB
drNewRowMCQsAns.item("DTE") = "#" & Date.Today.ToShortDateString & "#"



Give that a go and see how you get on.

The proper way would be to use parameter based queries as per the example in this post;
http://forums.asp.net/p/314258/314258.aspx[^]
 
Share this answer
 
v3
Comments
Abhinav S 14-Aug-11 6:57am    
Good answer. 5.
Dalek Dave 14-Aug-11 7:13am    
Good call, earns my 5 too.
Furqan Sehgal 14-Aug-11 8:49am    
Thanks for the answers but nothing worked!
I tried it by changing the type of filed in database, from DateTime to nvchar
this worked..... But I want to save it as date not as string.
Any suggestions?
DaveAuld 14-Aug-11 10:40am    
Don't change the database, thats the type you are wanting. Think you need to update your original question with the code snippet you are using and tell type of db you are using etc.
Furqan Sehgal 14-Aug-11 13:28pm    
I am using SSCE database. The code is what I already entered.
One thing I noticed, If if save the value of a datetimepicker, there is no error.
That means that is compatible type of data. But when I try today.date, then I get problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900