protected void Page_Command(Object sender, CommandEventArgs e)
 {
     try
     {
         if (e.CommandName == "Edit")
         {
             Response.Redirect("edit.aspx?ID=" + gID.ToString());
         }
         else if (e.CommandName == "Duplicate")
         {
             Response.Redirect("edit.aspx?DuplicateID=" + gID.ToString());
         }
         else if (e.CommandName == "Delete")
         {
             SqlProcs.spCAMPAIGNS_Delete(gID);
             Response.Redirect("default.aspx");
         }
         else if (e.CommandName == "SendTest")
         {
             SqlProcs.spCAMPAIGNS_SendEmail(gID, true);
             // 12/22/2007 Paul.  Send all queued emails, but include the date so that only these will get sent.
             EmailUtils.SendQueued(Application, Guid.Empty, gID, false);
             Response.Redirect("view.aspx?ID=" + gID.ToString());
         }
         else if (e.CommandName == "SendEmail")
         {
             SqlProcs.spCAMPAIGNS_SendEmail(gID, false);
             Response.Redirect("default.aspx");
         }
         else if (e.CommandName == "MailMerge")
         {
             // 08/12/2007 Paul.  Mail merge is not supported yet.
             Response.Redirect("default.aspx");
         }
         else if (e.CommandName == "Cancel")
         {
             Response.Redirect("default.aspx");
         }
     }
     catch (Exception ex)
     {
         SplendidError.SystemError(new StackTrace(true).GetFrame(0), ex);
         ctlDetailButtons.ErrorText = ex.Message;
     }
 }
 protected void Page_Command(object sender, CommandEventArgs e)
 {
     try
     {
         if (e.CommandName == "Clear")
         {
             ctlSearch.ClearForm();
             Server.Transfer("default.aspx");
         }
         else if (e.CommandName == "Search")
         {
             // 10/13/2005 Paul.  Make sure to clear the page index prior to applying search.
             grdMain.CurrentPageIndex = 0;
             grdMain.ApplySort();
             grdMain.DataBind();
         }
         else if (e.CommandName == "MassDelete")
         {
             string[] arrID = Request.Form.GetValues("chkMain");
             if (arrID != null)
             {
                 string sIDs = Utils.ValidateIDs(arrID);
                 if (!Sql.IsEmptyString(sIDs))
                 {
                     SqlProcs.spEMAILMAN_MassDelete(sIDs);
                     Response.Redirect("default.aspx");
                 }
             }
         }
         else if (e.CommandName == "SendQueued")
         {
             // 12/20/2007 Paul.  Send all queued emails, regardless of send date.
             EmailUtils.SendQueued(Application, Guid.Empty, Guid.Empty, true);
             Response.Redirect("default.aspx");
         }
     }
     catch (Exception ex)
     {
         SplendidError.SystemError(new StackTrace(true).GetFrame(0), ex);
         lblError.Text = ex.Message;
     }
 }
 protected void Page_Command(object sender, CommandEventArgs e)
 {
     try
     {
         if (e.CommandName == "Delete")
         {
             SqlProcs.spEMAILMAN_Delete(gID);
             // 12/20/2007 Paul.  Use RegisterStartupScript so that the rest of the page is rendered before the code is run.
             Page.ClientScript.RegisterStartupScript(System.Type.GetType("System.String"), "UpdateParent", "<script type=\"text/javascript\">UpdateParent();</script>");
         }
         else if (e.CommandName == "Send")
         {
             EmailUtils.SendQueued(Application, gID, Guid.Empty, true);
             Page.ClientScript.RegisterStartupScript(System.Type.GetType("System.String"), "UpdateParent", "<script type=\"text/javascript\">UpdateParent();</script>");
         }
     }
     catch (Exception ex)
     {
         SplendidError.SystemError(new StackTrace(true).GetFrame(0), ex);
         lblError.Text = ex.Message;
     }
 }
        public static void OnTimer(Object sender)
        {
            // 12/22/2007 Paul.  In case the timer takes a long time, only allow one timer event to be processed.
            if (!bInsideTimer)
            {
                bInsideTimer = true;
                HttpApplication global = sender as HttpApplication;
                try
                {
                    // 12/30/2007 Paul.  Workflow events always get processed.
                    WorkflowUtils.Process(global.Application);

                    DbProviderFactory dbf = DbProviderFactories.GetFactory(global.Application);
                    using (DataTable dt = new DataTable())
                    {
                        using (IDbConnection con = dbf.CreateConnection())
                        {
                            con.Open();
                            string sSQL;
                            sSQL = "select *               " + ControlChars.CrLf
                                   + "  from vwSCHEDULERS_Run" + ControlChars.CrLf
                                   + " order by NEXT_RUN     " + ControlChars.CrLf;
                            using (IDbCommand cmd = con.CreateCommand())
                            {
                                cmd.CommandText = sSQL;
                                // 01/01/2008 Paul.  The scheduler query should always be very fast.
                                // In the off chance that there is a problem, abort after 15 seconds.
                                cmd.CommandTimeout = 15;

                                using (DbDataAdapter da = dbf.CreateDataAdapter())
                                {
                                    ((IDbDataAdapter)da).SelectCommand = cmd;
                                    da.Fill(dt);
                                }
                            }
                        }
                        // 01/13/2008 Paul.  Loop outside the connection so that only one connection will be used.
                        foreach (DataRow row in dt.Rows)
                        {
                            Guid     gID        = Sql.ToGuid(row["ID"]);
                            string   sJOB       = Sql.ToString(row["JOB"]);
                            DateTime dtNEXT_RUN = Sql.ToDateTime(row["NEXT_RUN"]);
                            try
                            {
                                switch (sJOB)
                                {
                                case "function::BackupDatabase":
                                {
                                    // 01/28/2008 Paul.  Cannot perform a backup or restore operation within a transaction. BACKUP DATABASE is terminating abnormally.
                                    using (IDbConnection con = dbf.CreateConnection())
                                    {
                                        con.Open();
                                        try
                                        {
                                            string sFILENAME = String.Empty;
                                            string sTYPE     = "FULL";
                                            //SqlProcs.spSqlBackupDatabase(ref sNAME, "FULL", trn);
                                            using (IDbCommand cmd = con.CreateCommand())
                                            {
                                                cmd.CommandType = CommandType.StoredProcedure;
                                                cmd.CommandText = "spSqlBackupDatabase";
                                                IDbDataParameter parFILENAME = Sql.AddParameter(cmd, "@FILENAME", sFILENAME, 255);
                                                IDbDataParameter parTYPE     = Sql.AddParameter(cmd, "@TYPE", sTYPE, 20);
                                                parFILENAME.Direction = ParameterDirection.InputOutput;
                                                cmd.ExecuteNonQuery();
                                                sFILENAME = Sql.ToString(parFILENAME.Value);
                                            }
                                            SplendidError.SystemMessage(global.Application, "Information", new StackTrace(true).GetFrame(0), "Database backup complete " + sFILENAME);
                                        }
                                        catch (Exception ex)
                                        {
                                            SplendidError.SystemMessage(global.Application, "Error", new StackTrace(true).GetFrame(0), Utils.ExpandException(ex));
                                        }
                                    }
                                    break;
                                }

                                case "function::BackupTransactionLog":
                                {
                                    // 01/28/2008 Paul.  Cannot perform a backup or restore operation within a transaction. BACKUP DATABASE is terminating abnormally.
                                    using (IDbConnection con = dbf.CreateConnection())
                                    {
                                        con.Open();
                                        try
                                        {
                                            string sFILENAME = String.Empty;
                                            string sTYPE     = "LOG";
                                            //SqlProcs.spSqlBackupDatabase(ref sNAME, "LOG", trn);
                                            using (IDbCommand cmd = con.CreateCommand())
                                            {
                                                cmd.CommandType = CommandType.StoredProcedure;
                                                cmd.CommandText = "spSqlBackupDatabase";
                                                IDbDataParameter parFILENAME = Sql.AddParameter(cmd, "@FILENAME", sFILENAME, 255);
                                                IDbDataParameter parTYPE     = Sql.AddParameter(cmd, "@TYPE", sTYPE, 20);
                                                parFILENAME.Direction = ParameterDirection.InputOutput;
                                                cmd.ExecuteNonQuery();
                                                sFILENAME = Sql.ToString(parFILENAME.Value);
                                            }
                                            SplendidError.SystemMessage(global.Application, "Information", new StackTrace(true).GetFrame(0), "Transaction Log backup complete " + sFILENAME);
                                        }
                                        catch (Exception ex)
                                        {
                                            SplendidError.SystemMessage(global.Application, "Error", new StackTrace(true).GetFrame(0), Utils.ExpandException(ex));
                                        }
                                    }
                                    break;
                                }

                                case "function::runMassEmailCampaign":
                                {
                                    // 12/30/2007 Paul.  Update the last run date before running so that the date marks the start of the run.
                                    EmailUtils.SendQueued(global.Application, Guid.Empty, Guid.Empty, false);
                                    break;
                                }

                                case "function::pruneDatabase":
                                {
                                    using (IDbConnection con = dbf.CreateConnection())
                                    {
                                        con.Open();
                                        using (IDbTransaction trn = con.BeginTransaction())
                                        {
                                            try
                                            {
                                                SqlProcs.spSqlPruneDatabase(trn);
                                                trn.Commit();
                                            }
                                            catch (Exception ex)
                                            {
                                                trn.Rollback();
                                                SplendidError.SystemMessage(global.Application, "Error", new StackTrace(true).GetFrame(0), Utils.ExpandException(ex));
                                            }
                                        }
                                    }
                                    break;
                                }

                                case "function::pollMonitoredInboxes":
                                {
                                    EmailUtils.CheckMonitored(global.Application, Guid.Empty);
                                    break;
                                }

                                case "function::pollMonitoredInboxesForBouncedCampaignEmails":
                                {
                                    EmailUtils.CheckBounced(global.Application, Guid.Empty);
                                    break;
                                }

                                case "function::CheckVersion":
                                {
                                    DataTable dtVersions = Utils.CheckVersion(global.Application);

                                    DataView vwVersions = dtVersions.DefaultView;
                                    vwVersions.RowFilter = "New = '1'";
                                    if (vwVersions.Count > 0)
                                    {
                                        global.Application["available_version"]             = Sql.ToString(vwVersions[0]["Build"]);
                                        global.Application["available_version_description"] = Sql.ToString(vwVersions[0]["Description"]);
                                    }
                                    break;
                                }
                                }
                            }
                            finally
                            {
                                using (IDbConnection con = dbf.CreateConnection())
                                {
                                    con.Open();
                                    using (IDbTransaction trn = con.BeginTransaction())
                                    {
                                        try
                                        {
                                            // 01/12/2008 Paul.  Make sure the Last Run value is updated after the operation.
                                            SqlProcs.spSCHEDULERS_UpdateLastRun(gID, dtNEXT_RUN, trn);
                                            trn.Commit();
                                        }
                                        catch (Exception ex)
                                        {
                                            trn.Rollback();
                                            SplendidError.SystemMessage(global.Application, "Error", new StackTrace(true).GetFrame(0), Utils.ExpandException(ex));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    SplendidError.SystemMessage(global.Application, "Error", new StackTrace(true).GetFrame(0), Utils.ExpandException(ex));
                }
                finally
                {
                    bInsideTimer = false;
                }
            }
        }