0

hii I want to insert a countdown timer in my project. right now i am using the following code:

{

    DateTime dt = (DateTime)Session["end_t"];
    DateTime dt_curr = DateTime.Now;
    TimeSpan ts = dt - dt_curr;
    lblTimer.Text = ts.Hours.ToString() + ":" + ts.Minutes.ToString() + ":" + ts.Seconds.ToString();
    if (ts.Minutes == 0)
    {
        Timer1.Enabled = false;
        Response.Redirect("~/Online Exam/result2.aspx");
    }

the code works fine but when we move to some other page and then return back to main page the timer gets restarted. How can i overcome with this? Please help

4
  • are you sure that you are not changing Session["end_t"]'s value anywhere? (maybe Page_Load) Commented Nov 13, 2010 at 7:02
  • the value of session comes from the page load only, here is the code from page_Load DateTime end_time = start_time.AddMinutes(15); Session["end_t"] = end_time; Commented Nov 13, 2010 at 7:17
  • Are you trying to limit users visiting time of website or each page? Commented Nov 13, 2010 at 7:27
  • basically it is an Online Examination app, so the timer is required when a user presses Start Test button. Commented Nov 13, 2010 at 8:08

2 Answers 2

1

It looks like you're resetting the end time on each page load, probably by doing something like:

protected void Page_Load(object sender, EventArgs e)
{
    DateTime start_time = DateTime.Now;
    DateTime end_time = start_time.AddMinutes(15);
    Session["end_t"] = end_time;
}

Instead, you should store the end time only if the timer is not already running:

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["end_t"] == null) {
        DateTime start_time = DateTime.Now;
        DateTime end_time = start_time.AddMinutes(15);
        Session["end_t"] = end_time;
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

by adding the above code it thorws a NullReferenceException saying "Object reference not set to an instance of an object."
@Surbhi, can you specify which line throws the exception?
It throws this exception for about 4-5 times and on the next try the code runs perfectly and the timer is running fine now but now. Ex- the timer is showing 14:30 n on a button click, the timer stops, shows 14:30 for a few seconds and then shows correct time. How do I sync the timer perfectly that it doesn't stop on any user action.
@Frédéric the line which shows the exception is DateTime dt = (DateTime)Session["end_t"];
@Surbhi, maybe that line of code runs before we're setting the session value in Page_Load()? But in that case, you should get an error even without the change. Can you post more code so we can have a better idea of what's going on?
|
0

Make a Master page and make your operations with timer there.
or
You can send the counter of timer to next page then do same operations with timer there

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.