Mark Uy

Mark Uy

  • NA
  • 130
  • 0

my script dont work (global.asax)

Dec 13 2009 2:41 AM
heres my code ;


void Application_Start(object sender, EventArgs e)
{



if (Convert.ToBoolean(Session["AppStartStatus"]) == true)
{

// Code that runs on application startup
// Warranty Automation Application
SqlConnection conGlobal = new SqlConnection
(@"Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|\TRANE.mdf;Integrated Security=True;User Instance=True");

conGlobal.Open();

string selService;
selService = "Select * From ServiceLink WHERE DatetimeSched='" + DateTime.Now.ToShortDateString() + "'";

SqlCommand globalCom = new SqlCommand(selService, conGlobal);


SqlDataReader globalReader = globalCom.ExecuteReader();


if (globalReader.HasRows)
{

while (globalReader.Read())
{

string affectedserials;

affectedserials = globalReader["UnitSerial"].ToString();

//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
// the password for both email addresses is "new12345@"


//set the content
mail.Subject = "Service Notification";
mail.Body = "A Product with Serial#: " + affectedserials.ToString() + " needs a service visit. Please check the Service Link records right away";

//send the message
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = true;
smtp.Send(mail);



}

globalReader.Close();
string updateDate;
updateDate = "UPDATE ServiceLink SET DatetimeSched = '" + DateTime.Now.AddMonths(6).ToShortDateString() + "' WHERE DatetimeSched = '" + DateTime.Now.ToShortDateString() + "'";
SqlCommand comDateupdate = new SqlCommand(updateDate, conGlobal);
comDateupdate.ExecuteNonQuery();


}
}
else if (Convert.ToBoolean(Session["AppStartStatus"]) == false)
{

//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
// the password for both email addresses is "new12345@"


//set the content
mail.Subject = "Service Notification";
mail.Body = "Service Link is disabled.";

//send the message
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = true;
smtp.Send(mail);

}
else if (Session["AppStartStatus"] == null)
{
//Make AppStart Status By default true
Session["AppStartStatus"] = true;
}

}
 


its an email service that detects if a product needs a warranty. anyway i also have a separate control page for this with a simple stop/start button.

the appstartstatus idea was given to me by a member of this community but it doesnt seem to work. what am i doing wrong?

the first time i start i get this error message:

Session state is not available in this context.

Session state is not available in this context.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Session state is not available in this context.

Source Error:

Line 16: 
Line 17: 
Line 18:         if (Convert.ToBoolean(Session["AppStartStatus"]) == true)
Line 19:         {
Line 20: 


its appears that i cant fire up the session control?

thanks in advance, and i need the help urgently :(( . . . .





Answers (6)