Community
Showing results for 
Search instead for 
Do you mean 
Reply

User Database Backup Reminder code and plugin

Copper Contributor
Posts: 62
Country: USA

User Database Backup Reminder code and plugin

This dll prompts the user to backup the database if it has not been backed up in 7 days. I creates a trusted Sql connection, so it may not work on a remote database or if the user is not an admin on the machine. It fires on the second CurrentViewLoaded event, for me this usaully is Welcome>Contacts.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Drawing;

using System.Data.SqlClient;

using System.Data.OleDb;

using System.Data;

 

using Act.Framework;

using Act.Framework.ComponentModel;

using Act.Framework.Contacts;

using Act.UI;

using Act.UI.Contacts;

using Act.UI.Core;

using Act.Framework.MutableEntities;

using Act.Framework.Database;

 

namespace BackupPrompt

{

  public class Class1 : IPlugin

    {

    private ActApplication actApp;

         bool hasBackup = false;

         DateTime Wait;

 

    void IPlugin.OnLoad(ActApplication actApp)

         {

       this.actApp = actApp;

                actApp.AfterLogon += new EventHandler(Application_AfterLogon);

                actApp.BeforeLogoff += new EventHandler(Application_BeforeLogoff);

        }

    void IPlugin.OnUnLoad()

        {

        }

    private void Application_AfterLogon(object sender, EventArgs e)

       {

              actApp.CurrentViewLoaded += new EventHandler(actApp_CurrentViewLoaded);

       }

   private void Application_BeforeLogoff(object sender, EventArgs e)

      {

             CheckIfNeedsBackUp();

      if (actApp != null)

             {

                   actApp.AfterLogon -= new EventHandler(Application_AfterLogon);

                   actApp.BeforeLogoff -= new EventHandler(Application_BeforeLogoff);

                   actApp.CurrentViewLoaded -= new EventHandler(actApp_CurrentViewLoaded);

            }

      }

   private void actApp_CurrentViewLoaded(object sender, EventArgs e)

      {

            CheckIfNeedsBackUp();

      }

   private void CheckIfNeedsBackUp()

      {

     if (hasBackup == true)

            {

        int isItBackupTime;

        int WaitHowLong;

                   isItBackupTime = CalculateDays(GetLastBackupDay());

                   WaitHowLong = CalculateDays(Wait);

        if (isItBackupTime >= 7 && WaitHowLong >= 1)

                  {

                         PromptUserToBackup();

                  }

           }

     else

           {

                  hasBackup = true;

           }

      }

   private void PromptUserToBackup()

      {

   DialogResult result;

      result = MessageBox.Show("WARNING! \r\n \r\n It has been over a week since your data was last backed up. \r\n \r\n Do you wish to Backup your Data?", "Sage ACT! Backup Reminder", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

 

   if (result == DialogResult.Yes)

      {

             actApp.ExecuteCommand("act-ui://com.act/application/menu/file/backup/database");

      }

   else

     {

             Wait = DateTime.Now;

     }

     }

  public DateTime GetLastBackupDay()

    {

      DateTime lastDate = DateTime.Now;

      string[] parStrConn = newstring[6];

      string[] DBName = newstring[2];

              System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();

              string strConn = actApp.ActFramework.CurrentACTOLEDB2;

              parStrConn = (strConn.Split(';'));

              conn.ConnectionString = "integrated security=SSPI;" + parStrConn[1] + ";persist security info=False;"+ parStrConn[2];

              DBName = (parStrConn[2].Split('='));

              string strSQL = "SELECT TOP 1 [EVENTDATE] FROM [" + DBName[1] + "].[dbo].[VWX_EVENTLOGINFO] e where e.EVENTLOGTYPEID = '3' order by e.EVENTDATE desc";

              SqlCommand cmd = new SqlCommand(strSQL);

              try

              {

                     conn.Open();

                     cmd.Connection = conn;

         SqlDataReader dr = cmd.ExecuteReader(); 

         if (dr.HasRows)

                           while (dr.Read())

                           lastDate = (DateTime)dr.GetSqlDateTime(0);

                     else

                     {

                           lastDate = DateTime.Now.Subtract(TimeSpan.FromDays(7));

                     }

                    dr.Close();

                    conn.Close();

              }

      catch

              {

              }

      return lastDate;

      }

   public static int CalculateDays(DateTime lastDate)

      {

      DateTime now = DateTime.Now;

              TimeSpan days = now.Subtract(lastDate);

      return days.Days;

      }

   }

}

Copper Contributor
Posts: 62
Country: USA

Re: User Database Backup Reminder code and plugin

[ Edited ]

This is an updated version that is very similiar the Backup Prefs in AFFP. To set the backup perfereces go to the top menu>Tools>Backup Preferences.

Nickel Elite Contributor
Posts: 1,445
Country: USA

Re: User Database Backup Reminder code and plugin

If a database backup is completed successfully via the scheduler on another machine, does it still prompt the user to backup?

-----------
Marshall Knapp
AspenTech Consulting Group, Inc.
Act! Certified Consultants - Diamond Level
866-880-4ACT (4228)
www.aspen-tech.com
Copper Contributor
Posts: 62
Country: USA

Re: User Database Backup Reminder code and plugin

Which ever machine you put the plugin into the plugin folder, it will run the backup prompt dll. If one machine runs the backup, I would think that the others that have the plugin would also get prompt to backup because it backups the local database. I hope this helps.

 

Thank you,

Mark R. Wheeler

Tuned Listener
Posts: 35
Country: Australia

Re: User Database Backup Reminder code and plugin

I have complied the program and working

 

However it doesnt run the backup...

 

After reviewing the error log Im finding its having issue with  SQL connection string

IT fails on   sqlConnection.Open(); command line

  ((System.Data.SqlClient.SqlException)(sqlConnection.Open()))._errors, hidden Cannot convert type 'void' to 'System.Data.SqlClient.SqlException' 

 

I have made the settings cjeck on close.

 

Any idea

 

 

 

Reece harper

Act! Certified Consultant Australia
http://www.databasepartners.com.au
Copper Contributor
Posts: 62
Country: USA

Re: User Database Backup Reminder code and plugin

[ Edited ]

The plugin uses windows authentication to connect to SQL. Are you a Windows administrator? You compiled the program, did you add all the assemblies first. In the zip files there is a compiled dll. The first zip file has a backup reminder that reminds you every seven days. The second zip, I think is much better, it has preferences you set, it is very similar to the backup reminder in AFFP.