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();
}
%>