Solved

Convert something to C#

Posted on 2006-07-18
16
297 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

724 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