Unable to find the error!!!!!!!

Last post 07-06-2009 1:13 AM by venkatu2005. 3 replies.

Sort Posts:

  • Unable to find the error!!!!!!!

    07-05-2009, 5:33 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.
    Values are ID=1,COUNTS=1(for starting),PDATE=yesterday date with this format('05-07-2009')

    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: Unable to find the error!!!!!!!

    07-05-2009, 7:21 AM
    • Contributor
      5,474 point Contributor
    • sukumarraju
    • Member since 06-14-2006, 10:01 PM
    • Scotland, UK
    • Posts 1,171

     You should have an entry for Session_end in Global.asax

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


     

    Let me know further Queries.

    -- "Mark As Answer" if my reply helped you --
    Recommended Book
    Application Architecture Guide 2.0
    My Blog
  • Re: Unable to find the error!!!!!!!

    07-05-2009, 7:31 AM

    hey i dont want to reduce my counter...

    i used global.asax only for counter. I dono want to find online and offline users i made that code for counting the application hit rate...

    Application["OnlineUsers"] = 0;

    Application["Counter"] = 0;

    Instead of word counter i have used onlineuser dats it....

    Please go through the code fully which i pasted you itself understand what is my problem...Please give the solution...

  • Re: Unable to find the error!!!!!!!

    07-06-2009, 1:13 AM
    Answer
    • All-Star
      21,630 point All-Star
    • venkatu2005
    • Member since 07-01-2008, 6:48 AM
    • Posts 4,679

    rsunilsurender:

    Instead of word counter i have used onlineuser dats it....

    Please go through the code fully which i pasted you itself understand what is my problem...Please give the solution

    AS you said at particualt time the counter becomes 1 . it may be you are reboot or restart your application so it comes to 1.

    I already said in your last post you have to get the counter from your database ie: first get the counter value from ur database from this value increment and show it on your page.


    I have Changed My Blog from (http://venkat-dotnetsamples.blogspot.com) to (http://venkat-dotnetsnippets.blogspot.com)

    Regards,
    Venkatesan.M

    Please Mark as Answered If its helpful and Un-Mark as Answered if it not help u.
Page 1 of 1 (4 items)