Solved

Convert something to C#

Posted on 2006-07-18
16
285 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Handling arrays 14 33
ConsoleSql 1 14
dynamic menu in asp.net c# 11 26
Different Delete Messages 7 10
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now