Click here to Skip to main content
15,947,389 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys
I am facing some problem is when date time is not saved in DB .its thorws
Failed to convert parameter value from a String to a DateTime this exception

C#
SqlParameter LastLogIn = new SqlParameter("@LastLogIn", objUsers.LastLogIn);
                    LastLogIn.Direction = System.Data.ParameterDirection.Input;
                    LastLogIn.DbType = System.Data.DbType.DateTime;
                    sqlParams[5] = LastLogIn;


1.In DB datatype is also DateTime
2.button click event
objBusinessUI.LastLogIn = DateTime.Now;
current time i wanna show..but its throws exception

Please help

Thnxxxx in advance
Posted
Updated 11-Sep-13 1:45am
v5
Comments
Dholakiya Ankit 11-Sep-13 7:45am    
you can use getdate() in sql..

Hi Indrajeet,

What is the datetime value passed to the DB?
Even though the column is of Datetime type it may not accept all dateformats.

More over value from Dateteime.Now should be converted to string. This might the exception you are receiving not actual date insert issue.

Check this http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^] for more about Datetime.Now usage.

Also you can debug and check the value assigned to objBusinessUI.LastLogIn and then manually insert that value via SSMS and can trace the probelm easily.
 
Share this answer
 
v2
SqlParameter LastName = new SqlParameter("@LastName", objUsers.LastName);
LastName.Direction = System.Data.ParameterDirection.Input;
LastName.DbType = System.Data.DbType.String; //Actually i make mistake here.datatype i chhose ----------------------------------------------datetime instead of String thats y its throws exception
sqlParams[2] = LastName;
 
Share this answer
 
I really thanxx appreciate the efforts who try to help me
 
Share this answer
 
Because datatable only stored date value
'MM/dd/yyyy' or yyyy/MM/dd' format only

DateTime.Now.ToString("MM/dd/yyy");
DateTime.Now.ToShortDateString();

 
Share this answer
 

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