Help Help!!!!!!!!!

Last post 07-05-2009 11:46 AM by hans_v. 7 replies.

Sort Posts:

  • Help Help!!!!!!!!!

    07-05-2009, 1:23 AM

    Hi Friends,

    I need your's help i have created one table in mssql containing 1 row.The row contains three columns such as ID,COUNTS,PDATE.

    My requirement is::: when user opens a website or a domain-     Application counter will start counting...Always it checks wheather yesterday date and today date is same or not from the database if not same it should send mail of counts of yesterday number and same time it should change to today date in table and also application counter should be reset to ZERO....if equal its keep on counting the application counts....

    My Doubt: When iam testing this coding its running perfectly but when iam hosted in server upto a certain time its working fine suddenly COUNTS from table replaced to value 1 i dono why its happening iam thinking i done a mistake in reseting the counter value Application["OnlineUsers"] = 0;

    Please please help me what is the error or a mistake i have done in it....

    Global.asax

    <%@ Application Language="C#" %>

    <script runat="server">

        void Application_Start(object sender, EventArgs e)
        {
            Application["OnlineUsers"] = 0;

        }
        
        void Session_Start(object sender, EventArgs e)
        {
            Application.Lock();

            Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;

            Application.UnLock();

        }
        
    </script>

    Web.Config

    <?xml version="1.0"?>

    <configuration>
        <appSettings/>
        <connectionStrings/>
        <system.web>
            <compilation debug="true"/>
            <authentication mode="Windows"/>
           <customErrors mode="Off"/>
           
           
        </system.web>
    </configuration>

    My Coding:

    <%
          String datee=null;
          String countee=null;
          SqlDataReader rdr = null;
          String count = Application["OnlineUsers"].ToString();
          String date = DateTime.Today.ToString("dd-MM-yyyy");
         
          SqlConnection sqlcon = new SqlConnection("data source=localhost;Initial Catalog=udhyogadb; User Id=udhy; Password=yoga");
          sqlcon.Open();  
          SqlCommand cmd1 = new SqlCommand("select * from counter;", sqlcon);
          rdr = cmd1.ExecuteReader();
          while (rdr.Read())
          {
              datee = rdr.GetString(rdr.GetOrdinal("pdate"));
              countee = rdr.GetString(rdr.GetOrdinal("counts"));
          }
          sqlcon.Close();
          if (datee != date)
          {
              SmtpClient smtp = new SmtpClient();
              MailMessage mail = new MailMessage();
              mail.From = new MailAddress("XXX@gmail.com"); // this is your from mail id
              smtp.Credentials = new NetworkCredential("XXXUSERNAME", "XXXPASSWORD");
              mail.To.Add("XXX@gmail.com");        
              mail.Subject = "Counter";
              mail.Body = countee;
              mail.ReplyTo = mail.From;
              mail.Sender = mail.From;
              mail.IsBodyHtml = true;
              smtp.Port = 587;
              smtp.Host = "smtp.gmail.com";
              smtp.EnableSsl = true;
              smtp.Send(mail);
        
              sqlcon.Open();
              SqlCommand cmd = new SqlCommand("update counter set pdate=@dat where id =1;", sqlcon);
              cmd.Parameters.Add(new SqlParameter("@dat", date));
              cmd.ExecuteNonQuery();          
              sqlcon.Close();
              Application["OnlineUsers"] = 0;
          }

          else
          {
              sqlcon.Open();
              SqlCommand cmd2 = new SqlCommand("update counter set counts=@count where id =1;", sqlcon);
              cmd2.Parameters.Add(new SqlParameter("@count", count));
              cmd2.ExecuteNonQuery();
              sqlcon.Close();
          }

             %>

  • Re: Help Help!!!!!!!!!

    07-05-2009, 9:12 AM
    Answer
    • Member
      549 point Member
    • adeelehsan
    • Member since 07-18-2005, 10:56 AM
    • UAE
    • Posts 94

    Hello

    I think there might be a problem with the Application Pool on IIS where your application is running. It gets restarted or recycled after sometimes and you lose the value in the application state. So the count value is reset. Just make sure that settings on IIS are correct for your application.

    Please Mark AS Answer if it helped.
    Regards
    ADEEL EHSAN
  • Re: Help Help!!!!!!!!!

    07-05-2009, 9:27 AM

    hi brother thx for your reply but how to check and change the settings of IIS and application pool...Please guide me...

  • Re: Help Help!!!!!!!!!

    07-05-2009, 9:47 AM
    • Star
      9,242 point Star
    • hans_v
    • Member since 01-29-2007, 4:03 PM
    • Posts 1,603

     No matter if the settngs in IIS, there is always a possibility that the application is restarted. I would advice you to store the number in the database

  • Re: Help Help!!!!!!!!!

    07-05-2009, 9:59 AM

    What number....Can you tell me clearly....

  • Re: Help Help!!!!!!!!!

    07-05-2009, 10:45 AM
    • Star
      9,242 point Star
    • hans_v
    • Member since 01-29-2007, 4:03 PM
    • Posts 1,603

    The number (onlineusers) that you're storing in Application("onlineusers") and what your question is all about!

  • Re: Help Help!!!!!!!!!

    07-05-2009, 11:21 AM

    Friend, Iam storing (Applicationuser) value in database only...

    My question: When iam testing this coding its running perfectly but when iam hosted in server upto a certain time its working fine suddenly COUNTS from table replaced to value 1 i dono why its happening

  • Re: Help Help!!!!!!!!!

    07-05-2009, 11:46 AM
    Answer
    • Star
      9,242 point Star
    • hans_v
    • Member since 01-29-2007, 4:03 PM
    • Posts 1,603

    I understand your question perfectly, but you don't understand what's going on and what I'm telling you!

    Adeel Ehsan already explained that when the application recycles that the value is reset, causing the problem you're describing. I know that you don't store the number of online users in the database, but I'm advicing you to do so. because then you've full control when you want to reset the number to 0....

Page 1 of 1 (8 items)