Steve
Steve

Reputation: 1058

ASP.NET Timer - checking for record in SQL SERVER

I created an AJAX Timer on my C# ASP.NET page to check for a record in a database. When the record is not found I would like for the DB polling to continue. Currently it is blowing up when no records are found. It does work great when the record is found, then I just look for a correct status.

I have 2 questions.

1.) How can I continue polling when there is no record found. 2.) How can I get the timer to stop polling after 5 minutes and display a message?

Here is some of my code:

protected void Timer1_Tick(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(8000);
        string fileName = ViewState["FileName"].ToString();
        DBACCT account = new AccountMDB();
        DataTable dt = account.getSTS(fileName);
        String status = dt.Rows[0]["sts"].ToString();
        Label2.Text = status;
       if (dt.Rows.Count == 0)
        {
        // restart timer?
        }
        if (status == "C")
        {            
            Timer1.Enabled = false;
            Label2.Text = "File processed correctly";
        }
        if (status == "E")
        {
            Timer1.Enabled = false;
            Label2.Text = "File processed with Errors";

        }
    }

Upvotes: 1

Views: 883

Answers (1)

docmanhattan
docmanhattan

Reputation: 2416

First, you shouldn't be getting the status until AFTER you've checked the row count (which is why it is probably blowing up).

Second, why are you sleeping inside of the timer method? Why not just have the timer go off every 8 seconds instead? Setting the 'interval' to 8000 should do the trick.

Now, to answer your questions.

1.) Aren't AJAX timers set up to auto repeat? If they are, it should automatically be running this code every 8 seconds, if you make the change specified earlier. If they are not, you'll have to do some research on how to make them repeat.

2.) The easiest way would be to just create another timer that goes off after 5 minutes which will disable both timers as well as print the message.

Upvotes: 1

Related Questions