Link to home
Start Free TrialLog in
Avatar of m-jansen
m-jansen

asked on

Convert something to C#

How to convert this to C#?

If DateDiff("d", Application("LastScheduledRun"), Now()) > 0 Then
    ' This is where you put the commands you want to run on the
    ' schedule set up by the above condition.
    Response.Write "Do Something!"
       
    ' Reset our "LastScheduledRun" variable to the current date
    ' and time indicating we just did it.
    Application.Lock
    Application("LastScheduledRun") = Now()
    Application.UnLock
End If
Avatar of gdupadhyay
gdupadhyay
Flag of United States of America image

Hi,
Can u tell me what is : Application("LastScheduledRun")
Avatar of m-jansen
m-jansen

ASKER

Take a look at this link. This is where I got the code.

http://www.asp101.com/articles/john/schedule/default.asp
And read under Method 1: Faking It
if(((TimeSpan)(Application["LastScheduledRun"] - DateTime.Now)).Days > 0)
{
   Response.Write("Do Something");

    Application.Lock();
    Application["LastScheduledRun"] = DateTime.Now;
    Application.Unlock();
}
       if (((TimeSpan)(Application["LastScheduledRun"] - DateTime.Now)).Days > 0) // Error      3      Operator '-' cannot be applied to operands of type 'object' and 'System.DateTime'
        {
            Response.Write("Do Something");

            Application.Lock();
            Application["LastScheduledRun"] = DateTime.Now;
            Application.UnLock();
        }
ASKER CERTIFIED SOLUTION
Avatar of mrichmon
mrichmon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Label4.Text = (((TimeSpan)(Convert.ToDateTime(Application["LastScheduledRun"]) - DateTime.Now)).Days).ToString();
Always gives me the value:
-732509
Why? It should be bigger than 0 the first time I run the script on the server?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>Also make sure that you initialize Application["LastScheduledRun"] the first time.
I'm not sure about this.... When to do it? In the contructor? If I use the dev server in VS2005 does this work? Or must I use IIS to test this?
This code
Label4.Text = (((TimeSpan)(DateTime.Now - Convert.ToDateTime(Application["LastScheduledRun"]))).Days).ToString();
gives me
732511
After I refresh the page this one should be negative?
No - it depends on what the original value is in the  Application["LastScheduledRun"] variable.

You can set this in your global.aspx file when the application first loads.  Or you could set it on the page that uses it.  It is really up to you as to where it goes or when to do it.

But this is getting beyond the scope of the question.

The code provided is the translation of your code converted to C#.  How you decide to implement it or put it in different places is really up to you.
how to initialize Application["LastScheduledRun"] ?
Application["LastScheduledRun"] gives me the current time when hitting refresh. I have placed Label5.Text = Application["LastScheduledRun"].ToString(); in the Page_Load. I thought this should give me the last time I ran the page. Is that right?
If you set it to that then yes.
I'm not so sure about this one
Application["LastScheduledRun"]
yet

How long does this one contain what I have set it to? Until the server goes down?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial