Link to home
Start Free TrialLog in
Avatar of Feivi99
Feivi99

asked on

implementing a giveaway in .net/c#

Hi everyone,

I'd like to implement a giveaway on mysite, whereby threel times a day a random visitor will receive a message that he won "xyz" (really!).

Is there a way to implement this without a database? I would need to be sure that only three people will get the message and not more!

Thanks in advance
Avatar of Imran Javed Zia
Imran Javed Zia
Flag of Pakistan image

Hi,
You can do so with following,

Add a counter in Application and just randomly chose one user.  // but there is a disadvantage of it, if your application restarts then it will restart the counter. you can avoid then by saving this counter with date in some file on the server.

Thanks
Avatar of Feivi99
Feivi99

ASKER

Hi IJZ,

I'm not sure what you meant with "add a counter in application".
Check this out regarding adding a counter in Application_OnStart event of the Global.asax file.

http://forums.asp.net/p/1509963/3594591.aspx
By the way, the sample in my previous post is for VB.NET.  Sorry.  The one below is C# inside a Global.asax file:
public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            // Set our user count to 0 when we start the server
            Application["HitCounter"] = 0;
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            Application.Lock();
            Application["HitCounter"] = (int)(Application["HitCounter"]) + 1;
            Application.UnLock();
        }

        protected void Session_End(object sender, EventArgs e)
        {
            Application.Lock();
            Application["HitCounter"] = (int)(Application["HitCounter"]) - 1;
            Application.UnLock();
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }

Open in new window

Avatar of Feivi99

ASKER

Alfred,

Thanks for this code. There is one problem, however: I'm planning on serving the page from the ASP.NET cache, so I don't think Application_OnStart would work (I may be wrong). Do you know if this or any other event will fire when a page is served from cache?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Alfred A.
Alfred A.
Flag of Australia image

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