?
Solved

Convert something to C#

Posted on 2006-07-18
16
Medium Priority
?
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 500 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 500 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 500 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

649 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