Solved

Convert something to C#

Posted on 2006-07-18
16
289 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:m-jansen
  • 9
  • 6
16 Comments
 
LVL 9

Expert Comment

by:gdupadhyay
ID: 17130340
Hi,
Can u tell me what is : Application("LastScheduledRun")
0
 

Author Comment

by:m-jansen
ID: 17130405
Take a look at this link. This is where I got the code.

http://www.asp101.com/articles/john/schedule/default.asp
0
 

Author Comment

by:m-jansen
ID: 17130414
And read under Method 1: Faking It
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 35

Expert Comment

by:mrichmon
ID: 17130936
if(((TimeSpan)(Application["LastScheduledRun"] - DateTime.Now)).Days > 0)
{
   Response.Write("Do Something");

    Application.Lock();
    Application["LastScheduledRun"] = DateTime.Now;
    Application.Unlock();
}
0
 

Author Comment

by:m-jansen
ID: 17131104
       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();
        }
0
 
LVL 35

Accepted Solution

by:
mrichmon earned 125 total points
ID: 17131235
In that case you need to do

if(((TimeSpan)(Convert.ToDateTime(Application["LastScheduledRun"]) - DateTime.Now)).Days > 0)
{
   Response.Write("Do Something");

    Application.Lock();
    Application["LastScheduledRun"] = DateTime.Now;
    Application.Unlock();
}
0
 

Author Comment

by:m-jansen
ID: 17131709
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?
0
 
LVL 35

Assisted Solution

by:mrichmon
mrichmon earned 125 total points
ID: 17132557
Oh.

I put the two dates backwards.  You should subtract DateTime.Now from the last run date for a positive number of days.

Also make sure that you initialize Application["LastScheduledRun"] the first time.

(((TimeSpan)(DateTime.Now - Convert.ToDateTime(Application["LastScheduledRun"]))).Days)
0
 

Author Comment

by:m-jansen
ID: 17145071
>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?
0
 

Author Comment

by:m-jansen
ID: 17145096
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?
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 17147125
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.
0
 

Author Comment

by:m-jansen
ID: 17147772
how to initialize Application["LastScheduledRun"] ?
0
 

Author Comment

by:m-jansen
ID: 17148065
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?
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 17148470
If you set it to that then yes.
0
 

Author Comment

by:m-jansen
ID: 17152459
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?
0
 
LVL 35

Assisted Solution

by:mrichmon
mrichmon earned 125 total points
ID: 17154511
Until the application is re-started.

This could be until the server goes down, or the IIS service is restarted, or you change the web.config file in which case the application automatically re-sets.

I think this MSDN article may help answer a lot of questions you are having:
Application State Overview
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconapplicationstate.asp
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question