public void AddTreatment(Treatment treatment)
        {
            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ToString()))
                {
                    sqlConnection.Open();
                    SqlTransaction transaction = sqlConnection.BeginTransaction();
                    try
                    {
                        int consentType = GetConsentTypeId(sqlConnection, transaction, treatment._consentType.ToString());
                        int trackingID = GetTrackingId(sqlConnection, transaction, treatment._trackingInformation._device, treatment._trackingInformation._iP);
                        int doctorsAndProceduresID = GetDoctorsAndProcedures(sqlConnection, transaction, treatment._doctorAndPrcedures);
                        int signaturesID = GetSignatures(sqlConnection, transaction, treatment._signatureses);

                        // SqlCommand cmdTreatment = new SqlCommand("insert into Treatment(PatentId,ConsentType,IsPatientunabletosign,Unabletosignreason,TrackingID,Signatures,DoctorandProcedure,TransaltedBy,Date) values(" + treatment._patientId + "," + consentType + "," + (treatment._isPatientUnableSign == true ? 1 : 0) + ",'" + (string.IsNullOrEmpty(treatment._unableToSignReason) ? "" : treatment._unableToSignReason) + "'," + trackingID + "," + signaturesID + "," + doctorsAndProceduresID + ",'" + (string.IsNullOrEmpty(treatment._translatedBy) ? "" : treatment._translatedBy) + "','" + DateTime.Now + "')", sqlConnection, transaction);
                        SqlCommand cmdTreatment = new SqlCommand("insert into Treatment(PatentId,ConsentType,IsPatientunabletosign,IsStatementOfConsentAccepted,IsAutologousUnits,IsDirectedUnits,Unabletosignreason,TrackingID,Signatures,DoctorandProcedure,TransaltedBy,Date) values(" + treatment._patientId + "," + consentType + "," + (treatment._isPatientUnableSign == true ? 1 : 0) + ",'" + (treatment._IsStatementOfConsentAccepted == true ? 1 : 0) + ",'" + (treatment._IsAutologousUnits == true ? 1 : 0) + ",'" + (treatment._IsDirectedUnits == true ? 1 : 0) + ",'" + (string.IsNullOrEmpty(treatment._unableToSignReason) ? "" : treatment._unableToSignReason) + "'," + trackingID + "," + signaturesID + "," + doctorsAndProceduresID + ",'" + (string.IsNullOrEmpty(treatment._translatedBy) ? "" : treatment._translatedBy) + "','" + DateTime.Now + "')", sqlConnection, transaction);
                        cmdTreatment.ExecuteNonQuery();
                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception(ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
 public Treatment GetTreatment(string patientId, ConsentType consentType)
 {
     // open connection to sql server
     var treatment = new Treatment();
     try
     {
         System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
         var conStr = config.AppSettings.Settings["DBConnection"].Value;
         using (var sqlConnection = new SqlConnection(conStr))
         {
             sqlConnection.Open();
             var daTreatment = new SqlDataAdapter(@"GetTreatment", sqlConnection) { SelectCommand = { CommandType = CommandType.StoredProcedure } };
             daTreatment.SelectCommand.Parameters.Add("@consentType", SqlDbType.VarChar).Value = consentType.ToString();
             var dsTreatment = new DataSet();
             daTreatment.Fill(dsTreatment);
             if (dsTreatment.Tables[0].Rows.Count > 0)
             {
                 treatment._patientId = patientId;
                 treatment._consentType = consentType;
                 treatment._isPatientUnableSign = (dsTreatment.Tables[0].Rows[0]["IsPatientunabletosign"].ToString() == "True");
                 treatment._unableToSignReason = dsTreatment.Tables[0].Rows[0]["Unabletosignreason"].ToString();
                 treatment._translatedBy = dsTreatment.Tables[0].Rows[0]["TransaltedBy"].ToString();
                 treatment._trackingInformation = GetTrackingInformation(sqlConnection, dsTreatment.Tables[0].Rows[0]["TrackingID"].ToString());
                 treatment._doctorAndPrcedures = GetDoctorsProceduresInformation(sqlConnection, dsTreatment.Tables[0].Rows[0]["DoctorandProcedure"].ToString());
                 treatment._signatureses = GetSignaturesInformation(sqlConnection, dsTreatment.Tables[0].Rows[0]["Signatures"].ToString());
                 treatment._empID = dsTreatment.Tables[0].Rows[0]["EmpID"].ToString();
             }
         }
         return treatment;
     }
     catch (Exception ex)
     {
         throw new Exception(ex.Message);
     }
 }
        [OperationContract] //This Attribute used to define the method inside of interface
        public void AddTreatment(Treatment treatment)
        {
            try
            {
                System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);

                var conStr = config.AppSettings.Settings["DBConnection"].Value;

                using (var sqlConnection = new SqlConnection(conStr))
                {
                    sqlConnection.Open();
                    SqlTransaction transaction = sqlConnection.BeginTransaction();
                    try
                    {
                        if (treatment._doctorAndPrcedures == null)
                            treatment._doctorAndPrcedures = new List<DoctorAndProcedure>();

                        if (treatment._signatureses == null)
                            treatment._signatureses = new List<Signatures>();

                        int consentType = GetConsentTypeId(sqlConnection, transaction, treatment._consentType.ToString());
                        int trackingID = GetTrackingId(sqlConnection, transaction, treatment._trackingInformation._device, treatment._trackingInformation._iP);
                        int doctorsAndProceduresID = GetDoctorsAndProcedures(sqlConnection, transaction, consentType, treatment._doctorAndPrcedures);
                        int signaturesID = GetSignatures(sqlConnection, transaction, treatment._signatureses);

                        var addTreatmentCommand = new SqlCommand("AddTreatment", sqlConnection, transaction) { CommandType = CommandType.StoredProcedure };
                        addTreatmentCommand.Parameters.Add(new SqlParameter("@PatientID", SqlDbType.NVarChar)).Value = treatment._patientId;
                        addTreatmentCommand.Parameters.Add(new SqlParameter("@ConsentTypeID", SqlDbType.Int)).Value = consentType;
                        addTreatmentCommand.Parameters.Add(new SqlParameter("@isPatientUnableSign", SqlDbType.Int)).Value = (treatment._isPatientUnableSign ? 1 : 0);
                        addTreatmentCommand.Parameters.Add(new SqlParameter("@isStatementOfConsentAccepted", SqlDbType.Int)).Value = (treatment._IsStatementOfConsentAccepted ? 1 : 0);
                        addTreatmentCommand.Parameters.Add(new SqlParameter("@isAutologousUnits", SqlDbType.Int)).Value = (treatment._IsAutologousUnits ? 1 : 0);
                        addTreatmentCommand.Parameters.Add(new SqlParameter("@isDirectedUnits", SqlDbType.Int)).Value = (treatment._IsDirectedUnits ? 1 : 0);
                        addTreatmentCommand.Parameters.Add(new SqlParameter("@unableToSignReason", SqlDbType.VarChar)).Value = (string.IsNullOrEmpty(treatment._unableToSignReason) ? string.Empty : treatment._unableToSignReason);
                        addTreatmentCommand.Parameters.Add(new SqlParameter("@trackingID", SqlDbType.Int)).Value = trackingID;
                        addTreatmentCommand.Parameters.Add(new SqlParameter("@signaturesID", SqlDbType.Int)).Value = signaturesID;
                        addTreatmentCommand.Parameters.Add(new SqlParameter("@doctorsAndProceduresID", SqlDbType.Int)).Value = doctorsAndProceduresID;
                        addTreatmentCommand.Parameters.Add(new SqlParameter("@translatedBy", SqlDbType.VarChar)).Value = (string.IsNullOrEmpty(treatment._translatedBy) ? string.Empty : treatment._translatedBy);
                        addTreatmentCommand.Parameters.Add(new SqlParameter("@empID", SqlDbType.VarChar)).Value = treatment._empID;
                        addTreatmentCommand.ExecuteNonQuery();
                        /*
                        string query = @"insert into Treatment(PatentId,ConsentType,IsPatientunabletosign,IsStatementOfConsentAccepted,
                                                                IsAutologousUnits,IsDirectedUnits,Unabletosignreason,TrackingID,
                                                                Signatures,DoctorandProcedure,TransaltedBy,Date)
                                            values(" + treatment._patientId + "," + consentType + "," + (treatment._isPatientUnableSign == true ? 1 : 0) + ",'"
                                                     + (treatment._IsStatementOfConsentAccepted == true ? 1 : 0) + "','" + (treatment._IsAutologousUnits == true ? 1 : 0) + "','"
                                                     + (treatment._IsDirectedUnits == true ? 1 : 0) + "','"
                                                     + (string.IsNullOrEmpty(treatment._unableToSignReason) ? string.Empty : treatment._unableToSignReason) + "',"
                                                     + trackingID + "," + signaturesID + "," + doctorsAndProceduresID + ",'"
                                                     + (string.IsNullOrEmpty(treatment._translatedBy) ? string.Empty : treatment._translatedBy) + "','" + DateTime.Now + "')";

                        SqlCommand cmdTreatment = new SqlCommand(query, sqlConnection, transaction);
                        cmdTreatment.ExecuteNonQuery(); */
                        transaction.Commit();
                    }

                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception(ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public Treatment GetTreatment(string patientId, ConsentType consentType)
        {
            // open connection to sql server
            Treatment treatment = new Treatment();
            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ToString()))
                {
                    sqlConnection.Open();
                    SqlCommand cmdConsentID = new SqlCommand("select ID from ConsentType where [Name]='" + consentType.ToString() + "'", sqlConnection);
                    string consentID = "0";
                    using (var readId = cmdConsentID.ExecuteReader())
                    {
                        if (readId.Read())
                        {
                            consentID = readId["ID"].ToString();
                        }
                    }

                    // SqlDataAdapter daTreatment = new SqlDataAdapter("select PatentId,ConsentType,IsPatientunabletosign,Unabletosignreason,TrackingID,Signatures,DoctorandProcedure,TransaltedBy,Date from Treatment where PatentId=" + patientId + " and ConsentType=" + consentID + " and date=(select MAX(date) from Treatment where PatentId=" + patientId + " and ConsentType=" + consentID + ")", sqlConnection);
                    SqlDataAdapter daTreatment = new SqlDataAdapter("select PatentId,ConsentType,IsPatientunabletosign,IsStatementOfConsentAccepted,IsAutologousUnits,IsDirectedUnits,Unabletosignreason,TrackingID,Signatures,DoctorandProcedure,TransaltedBy,Date from Treatment where PatentId=" + patientId + " and ConsentType=" + consentID + " and date=(select MAX(date) from Treatment where PatentId=" + patientId + " and ConsentType=" + consentID + ")", sqlConnection);
                    DataSet dsTreatment = new DataSet();
                    daTreatment.Fill(dsTreatment);
                    if (dsTreatment.Tables[0].Rows.Count > 0)
                    {
                        treatment._patientId = patientId;
                        treatment._consentType = consentType;
                        treatment._isPatientUnableSign = (dsTreatment.Tables[0].Rows[0]["IsPatientunabletosign"].ToString() == "1" ? true : false);
                        treatment._unableToSignReason = dsTreatment.Tables[0].Rows[0]["Unabletosignreason"].ToString();
                        treatment._translatedBy = dsTreatment.Tables[0].Rows[0]["TransaltedBy"].ToString();
                        treatment._trackingInformation = GetTrackingInformation(sqlConnection, dsTreatment.Tables[0].Rows[0]["TrackingID"].ToString());

                        treatment._doctorAndPrcedures = GetDoctorsProceduresInformation(sqlConnection, dsTreatment.Tables[0].Rows[0]["DoctorandProcedure"].ToString());
                        treatment._signatureses = GetSignaturesInformation(sqlConnection, dsTreatment.Tables[0].Rows[0]["Signatures"].ToString());
                    }
                }
                return treatment;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }