private void btnGetAllData_Click(object sender, EventArgs e)
 {
     using (FingerPrintDB db = new FingerPrintDB())
     {
         dgvLogs.DataSource            = db.Logs.Where(w => w.DateOnlyRecord >= dtpFrom.Value.Date && w.DateOnlyRecord <= dtpTo.Value.Date).ToList();
         dgvLogs.Columns["Id"].Visible = false;
     }
 }
 private void btnGetLogs_Click(object sender, EventArgs e)
 {
     try
     {
         using (FingerPrintDB db = new FingerPrintDB())
         {
             SqlParameter dtFrom = new SqlParameter("@dtFrom", dtpFrom.Value.Date);
             SqlParameter dtTo   = new SqlParameter("@dtTo", dtpTo.Value.Date);
             var          logs   = db.Database.SqlQuery <LogsHours>("GetHours @dtFrom,@dtTo", dtFrom, dtTo).ToList();
             logsHours = logs.Select(s => new LogsHoursDisplay
             {
                 IndRegID = s.IndRegID,
                 Date     = s.Date,
                 Hours    = (s.Minutes / 60).ToString("D2") + ":" + (s.Minutes % 60).ToString("D2")
             }).ToList();
             dgvLogs.DataSource = logsHours;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
        private void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                using (FingerPrintDB db = new FingerPrintDB())
                {
                    foreach (var machine in machines)
                    {
                        string ipAddress = machine.IP;
                        string port      = machine.Port;
                        if (ipAddress == string.Empty || port == string.Empty)
                        {
                            throw new Exception("The Device IP Address and Port is mandotory !!");
                        }

                        int portNumber = 4370;
                        if (!int.TryParse(port, out portNumber))
                        {
                            throw new Exception("Not a valid port number");
                        }

                        bool isValidIpA = UniversalStatic.ValidateIP(ipAddress);
                        if (!isValidIpA)
                        {
                            throw new Exception("The Device IP is invalid !!");
                        }

                        isValidIpA = UniversalStatic.PingTheDevice(ipAddress);
                        if (!isValidIpA)
                        {
                            throw new Exception("The device at " + ipAddress + ":" + port + " did not respond!!");
                        }

                        objZkeeper = new ZkemClient(RaiseDeviceEvent);
                        objZkeeper.Connect_Net(ipAddress, portNumber);

                        ICollection <MachineInfo> lstMachineInfo = manipulator.GetLogData(objZkeeper, machine.Number);

                        foreach (var log in lstMachineInfo)
                        {
                            string timeOnly = Convert.ToDateTime(log.DateTimeRecord).ToShortTimeString();
                            if (db.Logs.Any(f => f.IndRegID == log.IndRegID && f.DateOnlyRecord == log.DateOnlyRecord && f.TimeOnlyRecord == timeOnly) != true)
                            {
                                db.Logs.Add(new Log
                                {
                                    Status         = machine.Type,
                                    MachineNumber  = machine.Number,
                                    IndRegID       = log.IndRegID,
                                    DateTimeRecord = Convert.ToDateTime(log.DateTimeRecord),
                                    DateOnlyRecord = log.DateOnlyRecord,
                                    TimeOnlyRecord = Convert.ToDateTime(log.DateTimeRecord).ToShortTimeString()
                                });
                                db.SaveChanges();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnExportAllData_Click(object sender, EventArgs e)
        {
            try
            {
                using (FingerPrintDB db = new FingerPrintDB())
                {
                    var allData = db.Logs.Where(w => w.DateOnlyRecord >= dtpFrom.Value.Date && w.DateOnlyRecord <= dtpTo.Value.Date).ToList();

                    if (dgvLogs.Rows.Count == 0)
                    {
                        return;
                    }
                    SaveFileDialog dlg = new SaveFileDialog
                    {
                        FileName   = "جميع البيانات",
                        DefaultExt = ".xls",
                        Filter     = "Text documents (.xls)|*.xls"
                    };
                    if (dlg.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }
                    int i = 2;
                    Excel.Application xlApp;
                    Excel.Workbook    xlWorkBook;
                    Excel.Worksheet   xlWorkSheet;
                    object            misValue = System.Reflection.Missing.Value;

                    xlApp                   = new Excel.Application();
                    xlWorkBook              = xlApp.Workbooks.Add(misValue);
                    xlWorkSheet             = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                    xlWorkSheet.Cells[1, 1] = "MachineNumber";
                    xlWorkSheet.Cells[1, 2] = "Status";
                    xlWorkSheet.Cells[1, 3] = "IndRegID";
                    xlWorkSheet.Cells[1, 4] = "DateTimeRecord";
                    xlWorkSheet.Cells[1, 5] = "DateOnlyRecord";
                    xlWorkSheet.Cells[1, 6] = "TimeOnlyRecord";

                    foreach (var item in allData)
                    {
                        xlWorkSheet.Cells[i, 1] = item.MachineNumber;
                        xlWorkSheet.Cells[i, 2] = item.Status;
                        xlWorkSheet.Cells[i, 3] = item.IndRegID;
                        xlWorkSheet.Cells[i, 4] = item.DateTimeRecord;
                        xlWorkSheet.Cells[i, 5] = item.DateOnlyRecord;
                        xlWorkSheet.Cells[i, 6] = item.TimeOnlyRecord;
                        i++;
                    }
                    xlWorkBook.SaveAs(dlg.FileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                    xlWorkBook.Close(true, misValue, misValue);
                    xlApp.Quit();
                    ReleaseObject(xlWorkSheet);
                    ReleaseObject(xlWorkBook);
                    ReleaseObject(xlApp);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }