void ftpTransfer_FullUploadCompleted(object sender, EventArgs e)
        {
            if (pnlResults.InvokeRequired)
            {
                this.Invoke((MethodInvoker)(() => ftpTransfer_FullUploadCompleted(sender, e)));
            }
            else
            {
                // Have an email in, but we just want to refresh the search here. FTP form needs to figure out the status on the claims

                // Mark all as "waiting" or "sent"


                string sql         = string.Format("UPDATE eligibility_data SET last_check = '{0}', last_status = {1}", DateTime.Now.ToShortDateString(), Convert.ToString((int)Eligibility_Status.Sent));
                string whereClause = " WHERE ID IN(";

                foreach (Control aControl in pnlResults.Controls)
                {
                    if (aControl is ctlPatientEligibility)
                    {
                        ctlPatientEligibility anAppointment = (ctlPatientEligibility)aControl;
                        if (whereClause != " WHERE ID IN(")
                        {
                            whereClause += ",";
                        }
                        whereClause += anAppointment.EligibilityData.id;
                    }
                }

                whereClause += ")";
                claim c = new claim();
                c.ExecuteNonQuery(sql + whereClause);

                Search();
            }
        }
        private void AddAppointment(eligibility_data ed, DateTime?lastDate)
        {
            if (lastDate.Value.Date != ed.appt_date.Date)
            {
                if (lastDateLabel != null)
                {
                    UpdateLastLabelSafe(lastDateLabel, numAppointmentsCurrentDate);
                }

                Label dateHeader = new Label();
                lastDateLabel = dateHeader;
                numAppointmentsCurrentDate = 0;

                dateHeader.AutoSize    = false;
                dateHeader.Text        = ed.appt_date.ToLongDateString();
                dateHeader.BorderStyle = BorderStyle.FixedSingle;
                dateHeader.BackColor   = Color.SteelBlue;
                dateHeader.ForeColor   = Color.White;
                dateHeader.TextAlign   = ContentAlignment.MiddleLeft;
                dateHeader.Font        = new Font(new FontFamily("Tahoma"), 10.0f, FontStyle.Bold);
                dateHeader.Height      = 40;

                pnlResults.Controls.Add(dateHeader);

                dateHeader.Dock = DockStyle.Top;
                dateHeader.BringToFront();
            }

            ctlPatientEligibility toAdd = new ctlPatientEligibility(ed);

            toAdd.BorderStyle = BorderStyle.FixedSingle;
            pnlResults.Controls.Add(toAdd);
            numAppointmentsCurrentDate++;
            toAdd.Dock = DockStyle.Top;
            toAdd.BringToFront();
        }
        private void CheckEligibility()
        {
            data_mapping_schema dms = data_mapping_schema.GetDefaultSchema;
            OleDbConnection     oConnect;
            DataTable           resultData = new DataTable();
            OleDbDataAdapter    oAdapter;

            try
            {
                oConnect = new OleDbConnection(dms.GetConnectionString(true));
                oConnect.Open();
            }
            catch (Exception err)
            {
                LoggingHelper.Log("Could not connect to DB to send claims", LogSeverity.Error, err, false);
                return;
            }

            string sql = @"SELECT appt.patNAme as patient, 
payer.inscoName AS PAYER_NAME, payer.insID + '-' + payer.insDB AS Payer_ID, payer.STREET AS PAYER_ADDRESS1, payer.STREET2 AS PAYER_ADDRESS2, payer.CITY AS PAYER_CITY, payer.STATE AS PAYER_STATE,payer.ZIP AS PAYER_ZIP, 
appt.APPTDATE AS APPT_DATE, appt.time_hour AS APPT_HOUR, appt.time_minute AS APPT_MINUTE,
prov.NAME_FIRST AS Provider_FirstName, prov.NAME_LAST AS Provider_LastName, prov.ID1 AS Provider_NPI, prov.STATEID AS Provider_LicenseNo, 'CT' AS provider_state,
insured.idnum as Subscriber_ID, sub.lastname AS Subscriber_LastName, sub.firstname AS Subscriber_FirstName, sub.gender AS Subscriber_Gender, sub.birthdate AS Subscriber_DOB, payer.GroupNum as GroupNo, dep.patID + '-' + dep.patDB AS SubscriberDependentID,
dep.PRINSREL AS PatientRelation,
dep.patID + '-' + dep.PATDB as Dependent_ID, dep.LastName as Dependent_LastName, dep.FirstName as Dependent_FirstName, dep.gender AS Dependent_Gender, dep.BirthDate as Dependent_DOB

FROM DDB_APPT appt
INNER JOIN DDB_PAT_BASE dep ON (appt.PATID = dep.PATID AND appt.PATDB = dep.PATDB)
INNER JOIN DDB_INSURED insured ON (dep.PRINSUREDID = insured.INSUREDID AND dep.PRINSUREDDB = insured.insuredDB)
INNER JOIN DDB_INSURANCE_BASE payer ON (insured.insID = payer.insID AND insured.INSDB = payer.insDB)
INNER JOIN DDB_PAT_BASE sub ON (insured.insuredPartyID = sub.PATID AND insured.insuredPartyDB = sub.PATDB)
INNER JOIN DDB_RSC_BASE prov ON (prov.URSCID = appt.PRPROVID AND prov.RSCDB = appt.PRPROVDB)
WHERE (OPTYPE = 3) AND (";

            string whereClause = "";

            foreach (Control aControl in pnlResults.Controls)
            {
                if (aControl is ctlPatientEligibility)
                {
                    ctlPatientEligibility anAppointment = (ctlPatientEligibility)aControl;
                    if (anAppointment.CheckState)
                    {
                        if (whereClause != "")
                        {
                            whereClause += " OR";
                        }
                        whereClause += string.Format(" (APPTID = {0} AND APPTDB = {1})", anAppointment.EligibilityData.dentrix_id, anAppointment.EligibilityData.dentrix_db);
                    }
                }
            }

            sql += whereClause + ")";


            oAdapter = new OleDbDataAdapter(sql, oConnect);
            oAdapter.Fill(resultData);

            string fullDocAsList = EligibilityHelper.ConvertFromTable(resultData);

            ftpTransfer.Initialize();
            ftpTransfer.Show();

            ftpTransfer.UpdateMainLabel("Creating files to send to Mercury...", false);

            ParameterizedThreadStart ts = new ParameterizedThreadStart(UploadThreadSafe);
            Thread t = new Thread(ts);

            t.Start(fullDocAsList);
        }