Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 29, 2012 05:16 PM by claudia888
Apr 28, 2012 07:33 PM|LINK
I'm building a MVC3 app which has a feature that requires me to measure the time that each user takes to complete a certain task.
I've implemented similar functions before in other languages (Perl), but not sure how to do this in ASP.NET and C#.
What I want to do is:
1) When an user clicks on "Start" button, I get the system time then, assign it to "StartTime";
2) When the user completes, and click on "Submit" button (which could be 5-6 pages later, or on the same page), I get the system time again, assign it to "EndTime";
3) I then get the difference and assign it to the var I want: int TimeTaken = EndTime - StartTime; and convert it to minutes or seconds for display.
Please let me know how should I implement this in C#; sample code is appreciated!
Apr 29, 2012 12:15 PM|LINK
You cqan store them in the session data:
Session("StartTime") = DateTime.Now() ;
Session is available from within any Ation method.
Then you configure the sessions for the maximum time you would like to wait the user provide an answer to a question. If this time expires session data are disposed: when you detect this (Session returning null) you show a messaqge sayng maximum time is
Apr 29, 2012 04:46 PM|LINK
Thanks for the reply - what you suggested makes great sense. I tried your suggestion and read a few MSDN articles on sessionState to get more info. Here is where I am (keep in mind I have never used sessions in my ASP.NET app, even though I used session
variables in Perl and classic ASP before so I understand the concept):
1) I added the following namespaces in my controller.cs - are they enough? or am I still missing some needed namespace?
2) I added the following in my web.config, under "<system.web>...</system.web>":
3) I added this line of code you suggested (Session("StartTime") = DateTime.Now() ;) in my action where it should be, but the VS shows a red line under "Session", saying:
"The name 'Session' does not exist in the current context."
I searched around but have not found the solution. Can you provide more details or sample code to address this?
Apr 29, 2012 05:03 PM|LINK
Within any action method....
but it is Session["StartTime"] not Session("StartTime")....I wrote wrong parentheses..
Apr 29, 2012 05:16 PM|LINK
I just found that out as well, it's [ ], not ( ) -- thanks!
Session["StartTime"] = DateTime.Now;