Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi iam yogesh and i m having problems in date saving in sql table
my code for saving a date is
C#
datetime mdat;
mdat = dtpdate.Value;     //dtpdate is datepicker
insert into saltrn (dat) values(mdat.ToString("dd/MM/yyyy"))
//for eg: when i saved 27-11-2011 in  my table(.entry is saved)..... Now when i am //looking from sql select query, the same entry 
// looks like  1894-07-16 00:00:00.000
//why is it happening? 

HELP ME PLEASE .. instead of 27-11-2011, 1894-07-16 00:00:00.000 is getting saved.
Posted
Updated 27-Nov-11 6:32am
v2

1 solution

If you are using a DateTime type in the SQL table, the proper string format to pass would be: YYYY-MM-DD, so 2011-11-27 in your case.
 
Share this answer
 
Comments
yogesh a sharma 27-Nov-11 12:37pm    
THANKS , but i tried it and still failed
this time i am getting 1905-05-28 00:00:00.000 instead.
Akos Orban 27-Nov-11 12:52pm    
Try to add the datetime object as the value to your query, which is inserting the value to the table. If you are using ADO.NET something like this:
SqlCommand command = new SqlCommand("INSERT INTO saltrn (dat) VALUES(@date)", connection);
command.Parameters.Add("@date", SqlDbType.DateTime);
command.Parameters["@date"].Value = dtpdate.Value;
command.ExecuteNonQuery();

Also make sure that dtpdate.Value contains the proper value you want to save. Add a breakpoint at the insert statement, and check the value of it.
yogesh a sharma 27-Nov-11 13:21pm    
Thanks sir i got my answer..
RaviRanjanKr 27-Nov-11 13:27pm    
5+

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