public LabelPollingService()
        {
            InitializeComponent();

            Config.ReadSettingsFromRegistry();

            Config.Log("Label Polling Started", false);

            try
            {
                SqlCmd = new SqlCommands();
            }
            catch (System.Exception ex)
            {
                Config.Log(ex.Message);
            }

            // ja - read in the Last Key used for each "Trigger"
            foreach (ITriggerTables obj in SqlCmd.Trig)
            {
                // ja - enumerators are read only so copy the object
                ITriggerTables writeObj = obj;
                Config.ReadLastKey(ref writeObj);
            }
        }
        public int FillJobsQueue(ITriggerTables triggerObj, string sSerialNumber)
        {
            SqlCommand sqlCommand = new SqlCommand(ExecuteFillLabelGen(triggerObj, sSerialNumber), TheConnection);
            int        nResults   = sqlCommand.ExecuteNonQuery();

            return(nResults);
        }
        public SqlDataReader GetTrigerReader(ITriggerTables triggerObj)
        {
            SqlCommand    sqlSelect  = GetTriggerCommand(triggerObj);
            SqlDataReader dataReader = sqlSelect.ExecuteReader();

            return(dataReader);
        }
        private bool MonitorTriggerTable(ITriggerTables theObj)
        {
            SqlDataReader dataReader  = null;
            bool          bFound      = false;
            int           nKey        = -1;
            List <string> serialsList = new List <string>();

            try
            {
                Console.WriteLine(DateTime.Now.ToString() + " - Polling " + theObj.GetTableName() + "...");

                // ja - read from the generic trigger table (only unprocessed keys) ordered by the table key
                dataReader = SqlCmd.GetTrigerReader(theObj);

                // ja - only read first result
                while (dataReader.Read())
                {
                    bFound = true;

                    string sKey          = dataReader[theObj.GetKeyName()].ToString().Trim();
                    string sSerialNumber = dataReader[theObj.GetBasePlate()].ToString().Trim();

                    Console.WriteLine("Found Key in " + theObj.GetTableName() + ": Key = " + sKey + "...");
                    Console.WriteLine("Found Serial in " + theObj.GetTableName() + ": Serial = " + sSerialNumber + "...");

                    // ja - store serial numbers so they can be written after this reader is closed as I only maintain one DB connection at a time
                    if (!String.IsNullOrEmpty(sSerialNumber) && !sSerialNumber.Contains("End"))
                    {
                        serialsList.Add(sSerialNumber);
                    }

                    // ja - get the last key used
                    nKey = Convert.ToInt32(sKey);
                }

                // ja - store the last key...
                if (nKey > -1)
                {
                    theObj.LastKeyUsed = nKey;
                }

                dataReader.Close();

                foreach (string serial in serialsList)
                {
                    SqlCmd.FillJobsQueue(theObj, serial);
                }
            }
            catch (Exception ex)
            {
                dataReader.Close();
                Config.Log(ex.Message);
            }

            return(bFound);
        }
        private string GetTriggerTableSql(ITriggerTables TheClass)
        {
            string sSql = "Select ";

            sSql += TheClass.GetKeyName() + ", " + TheClass.GetBasePlate();
            sSql += " from " + TheClass.GetTableName();
            sSql += " where " + TheClass.GetKeyName() + " > " + TheClass.LastKeyUsed.ToString();
            sSql += " order by " + TheClass.GetKeyName();

            return(sSql);
        }
        public static void WriteLastKey(ITriggerTables triggerObj)
        {
            try
            {
                Console.WriteLine("WriteLastKey - Writing Last Key Values");

                int    nLastKey    = triggerObj.LastKeyUsed;
                string sRegKeyName = GetLastKeyName(triggerObj);

                RegistryKey amcKey  = GetRegistryKey(false);
                RegistryKey burnKey = amcKey.CreateSubKey(APPLICATION_NAME);

                // ja store the default values
                burnKey.SetValue(sRegKeyName, nLastKey);

                burnKey.Close();
            }
            catch (System.Exception ex)
            {
                Log(ex.Message);
            }
        }
        public static void ReadLastKey(ref ITriggerTables triggerObj)
        {
            string sRegKeyName = GetLastKeyName(triggerObj);

            try
            {
                RegistryKey key = GetRegistryKey(true);

                // ja - this should throw an exception if it does not yet exists
                int nLastKey = Convert.ToInt32(key.GetValue(sRegKeyName).ToString());

                if (nLastKey > 0)
                {
                    triggerObj.LastKeyUsed = nLastKey;
                }

                key.Close();
            }
            catch (System.Exception)
            {
                // ja - do nothing because the key might not of been written yet
            }
        }
 public static string GetLastKeyName(ITriggerTables triggerObj)
 {
     return("LastKeyFor_" + triggerObj.GetTableName());
 }
        private string ExecuteFillLabelGen(ITriggerTables triggerObj, string sSerialNumber)
        {
            string sSql = "exec usp_FillLabelGen '" + triggerObj.GetTableName() + "', '" + sSerialNumber + "'";

            return(sSql);
        }
        public SqlCommand GetTriggerCommand(ITriggerTables triggerObj)
        {
            SqlCommand sqlCommand = new SqlCommand(GetTriggerTableSql(triggerObj), TheConnection);

            return(sqlCommand);
        }