Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to pass datetime value from application to stored procedure (SP).

Last login date of user is stored in session variable and i want to pass it in SP.

i am doing it like this-
C#
DateTime myDt = DateTime.ParseExact(Session["Last_Login_date"].ToString(), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);


but i am getting error-
C#
String was not recognized as a valid DateTime.  


My Session["Last_Login_date"] contains date - "30, Jun 2014 12:08: PM"

Should i need to change format and assign?
Please help
Posted

Try this:
DateTime dt = DateTime.ParseExact("30, Jun 2014 12:08 PM", "dd, MMM yyyy hh:mm tt", CultureInfo.InvariantCulture);

Before this, make sure your date format is appropriate as specified in the string underlined above.
 
Share this answer
 
Comments
[no name] 30-Jun-14 3:02am    
right. now its working.
But i didnt understood, what exactly ParseExact do?
n whats use of InvariantCulture?
Use TryParseExact method[^] to check for validity of date and format.
 
Share this answer
 
try with
C#
DateTime myDt = Convert.ToDateTime(Session["Last_Login_date"]);

if you going to use DateTime.ParseExact then you need to give exact format as your input, otherwise it will fail.
Set session value with DateTime object directly.
C#
Session["Last_Login_date"] =DateTime.Now;

when you need to retrieve value back,
C#
DateTime myDt = Convert.ToDateTime(Session["Last_Login_date"]);
 
Share this answer
 
v2
Comments
DamithSL 30-Jun-14 2:51am    
how you set the session value?
[no name] 30-Jun-14 3:08am    
Thanks for pointing out ParseExact info.
try it


DateTime fDate = DateTime.Parse(Session["Last_Login_date"]);
 
Share this answer
 
v3

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