Skip to content

C# 2010 session variable

scampercat

Asked Jun 21, 2012, 2:10 PM EDTSolved
Share
LinkedInX
In a C# 2010 web application, I would like to know how to convert a session variable that is in a date time format to a string value that displays in a textbox on the web application. I would like to only obtain the date part of the value.

I have tried lots of versions that look like the following:

Date1.Text = Session["Date1"].ToString("yyyy-MM-dd HH:mm:ss");

How would you setup this code so the value is acutally set correctly for the textbox?

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • ✓ Accepted answer0
    First convert the session["Date1"] object to DateTime type and then do the formatting.

    Session["Date1"] = System.DateTime.Now;
    TextBox1.Text  = Convert.ToDateTime(Session["Date1"]).ToString("yyyy-MM-dd HH:mm:ss");


    Hope this resolves your problem.
    0