protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int j = 0, k = 0;
        var listOfFiles = new List <FileValidationOutcomeDetails>();

        foreach (GridViewRow row in gvMasterFiles.Rows)
        {
            var ddlOutcome       = row.FindControl("ddlOutcome") as DropDownList;
            var txtOutcomeReason = row.FindControl("txtOutcomeReason") as TextBox;
            if (ddlOutcome != null && txtOutcomeReason != null)
            {
                ddlOutcome.BackColor = Color.Transparent;
                if (ddlOutcome.SelectedIndex <= 0)
                {
                    ddlOutcome.BackColor = Color.Red;
                    k++;
                    break;
                }
                var dataKey = gvMasterFiles.DataKeys[row.RowIndex];
                if (dataKey != null)
                {
                    var fileId = Convert.ToDecimal(dataKey.Value);
                    listOfFiles.Add(new FileValidationOutcomeDetails {
                        Id                  = 0,
                        SubmissionId        = Convert.ToDecimal(SubmissionId),
                        FileId              = fileId,
                        ValidationOutcomeId = Convert.ToInt32(ddlOutcome.SelectedValue),
                        SID                 = Sars.Systems.Security.ADUser.CurrentSID,
                        OutcomeReason       = string.IsNullOrEmpty(txtOutcomeReason.Text) ? null : txtOutcomeReason.Text
                    }
                                    );
                }
            }
        }

        foreach (GridViewRow row in gvLocalFiles.Rows)
        {
            var ddlOutcome       = row.FindControl("ddlOutcome") as DropDownList;
            var txtOutcomeReason = row.FindControl("txtOutcomeReason") as TextBox;
            if (ddlOutcome != null && txtOutcomeReason != null)
            {
                ddlOutcome.BackColor = Color.Transparent;
                if (ddlOutcome.SelectedIndex <= 0)
                {
                    ddlOutcome.BackColor = Color.Red;
                    j++;
                    break;
                }
                var dataKey = gvLocalFiles.DataKeys[row.RowIndex];
                if (dataKey != null)
                {
                    var fileId = Convert.ToDecimal(dataKey.Value);
                    listOfFiles.Add(
                        new FileValidationOutcomeDetails {
                        Id                  = 0,
                        SubmissionId        = Convert.ToDecimal(SubmissionId),
                        FileId              = fileId,
                        ValidationOutcomeId = Convert.ToInt32(ddlOutcome.SelectedValue),
                        SID                 = Sars.Systems.Security.ADUser.CurrentSID,
                        OutcomeReason       = string.IsNullOrEmpty(txtOutcomeReason.Text) ? null : txtOutcomeReason.Text
                    }
                        );
                }
            }
        }

        if (k > 0)
        {
            tbMain.ActiveTabIndex = 0;
            MessageBox.Show("Please select validation outcome for all the files.");
            return;
        }
        if (j > 0)
        {
            tbMain.ActiveTabIndex = 1;
            MessageBox.Show("Please select validation outcome for all the files.");
            return;
        }
        if (listOfFiles.Any())
        {
            var numSaved = 0;
            foreach (var detailse in listOfFiles)
            {
                numSaved += DBWriteManager.SaveFileValidationOutcome(detailse);
            }
            if (_fileSubmission == null)
            {
                _fileSubmission = DBReadManager.GeFileSubmissionById(Convert.ToDecimal(SubmissionId));
            }
            if (numSaved == (gvLocalFiles.Rows.Count + gvMasterFiles.Rows.Count))
            {
                btnSave.Enabled   = false;
                btnSubmit.Enabled = false;
                MessageBox.Show("Validation submitted successfully");
            }

            var status   = 0;
            var accepted = listOfFiles.Count(a => a.ValidationOutcomeId == 1);
            var rejected = listOfFiles.Count(a => a.ValidationOutcomeId == 2);
            if (accepted == listOfFiles.Count())
            {
                // accepted = 3
                status = 3;
                try{
                    if (!string.IsNullOrEmpty(_fileSubmission.ContactMobileNumber) && _fileSubmission.ContactMobileNumber.IsValid(StringValidationType.CellularNumber))
                    {
                        var smsBody = DBReadManager.GetMasterLocalFileNotificationSmsBodyTemplate(2);
                        if (!string.IsNullOrEmpty(smsBody))
                        {
                            var service = new FDRQueueService();
                            service.SendSms(
                                _fileSubmission.ContactMobileNumber
                                , _fileSubmission.TaxRefNo
                                , string.Format(smsBody, _fileSubmission.TaxRefNo)
                                , _fileSubmission.Year
                                );
                            DBWriteManager.SaveSentSmsCommunications(
                                _fileSubmission.TaxRefNo
                                , string.Format(smsBody, _fileSubmission.TaxRefNo)
                                , _fileSubmission.Year
                                );
                        }
                    }
                }
                catch (Exception) {
                    ;
                }
                try{
                    if (!string.IsNullOrEmpty(_fileSubmission.ContactEmail))
                    {
                        byte[] attachment  = null;//FdrCommon.GetFileAcceptenceLetter(SubmissionId);
                        var    service     = new FDRQueueService();
                        var    messageBody = string.Format(DBReadManager.GetMasterLocalFileNotificationEmailBodyTemplate(2), _fileSubmission.TaxRefNo);
                        service.SendEmail
                        (
                            messageBody
                            , "CBC File validation outcome"
                            , _fileSubmission.ContactEmail
                            , null
                            , "CBC File validation outcome"
                            , _fileSubmission.TaxRefNo
                            , _fileSubmission.Year
                        );
                    }
                }
                catch (Exception) {
                    ;
                }

                try{
                    var attachment = FdrCommon.GetEfilingAcceptanceLetter(_fileSubmission.Year);
                    if (!string.IsNullOrEmpty(attachment))
                    {
                        var service = new FDRQueueService();
                        service.SendLetter
                        (
                            _fileSubmission.SubmissionId
                            , _fileSubmission.TaxRefNo
                            , _fileSubmission.Year
                            , attachment
                            , "Acceptance of Master Files and Local Files"
                            , true
                            , Sars.Systems.Security.ADUser.CurrentSID
                        );
                    }
                }
                catch (Exception)
                {
                    ;
                }
            }
            else if (rejected == listOfFiles.Count())
            {
                //rejected = 4
                status = 4;
                try{
                    if (!string.IsNullOrEmpty(_fileSubmission.ContactMobileNumber) && _fileSubmission.ContactMobileNumber.IsValid(StringValidationType.CellularNumber))
                    {
                        var smsBody = DBReadManager.GetMasterLocalFileNotificationSmsBodyTemplate(3);
                        if (!string.IsNullOrEmpty(smsBody))
                        {
                            var service = new FDRQueueService();
                            service.SendSms
                            (
                                _fileSubmission.ContactMobileNumber
                                , _fileSubmission.TaxRefNo
                                , string.Format(smsBody, _fileSubmission.TaxRefNo)
                                , _fileSubmission.Year
                            );

                            DBWriteManager.SaveSentSmsCommunications(
                                _fileSubmission.TaxRefNo
                                , string.Format(smsBody, _fileSubmission.TaxRefNo)
                                , _fileSubmission.Year
                                );
                        }
                    }
                }
                catch (Exception) {
                    ;
                }
                try{
                    if (!string.IsNullOrEmpty(_fileSubmission.ContactEmail))
                    {
                        //var attachment = FdrCommon.GetFileRejectionLetter(SubmissionId);
                        var service     = new FDRQueueService();
                        var messageBody = string.Format(
                            DBReadManager.GetMasterLocalFileNotificationEmailBodyTemplate(3),
                            _fileSubmission.TaxRefNo);
                        service.SendEmail(
                            messageBody
                            , "CBC File validation outcome"
                            , _fileSubmission.ContactEmail
                            , null
                            , "CBC File validation outcome"
                            , _fileSubmission.TaxRefNo
                            , _fileSubmission.Year
                            );
                    }
                }
                catch (Exception) {
                    ;
                }

                //SEND EFILING REJECTION LETTERS
                try{
                    var attachment = FdrCommon.GetEfilingRejectionLetter(_fileSubmission.TaxRefNo, _fileSubmission.Year, Convert.ToDecimal(SubmissionId));
                    if (!string.IsNullOrEmpty(attachment))
                    {
                        var service = new FDRQueueService();
                        service.SendLetter
                        (
                            _fileSubmission.SubmissionId
                            , _fileSubmission.TaxRefNo
                            , _fileSubmission.Year
                            , attachment
                            , "Rejection of Master Files and Local Files"
                            , false
                            , Sars.Systems.Security.ADUser.CurrentSID
                        );
                    }
                }
                catch (Exception exception) {
                    MessageBox.Show(exception.ToString());
                }
            }
            else
            {
                try
                {
                    if (!string.IsNullOrEmpty(_fileSubmission.ContactMobileNumber) && _fileSubmission.ContactMobileNumber.IsValid(StringValidationType.CellularNumber))
                    {
                        var smsBody = DBReadManager.GetMasterLocalFileNotificationSmsBodyTemplate(4);
                        if (!string.IsNullOrEmpty(smsBody))
                        {
                            var service = new FDRQueueService();
                            service.SendSms
                            (
                                _fileSubmission.ContactMobileNumber
                                , _fileSubmission.TaxRefNo
                                , string.Format(smsBody, _fileSubmission.TaxRefNo)
                                , _fileSubmission.Year
                            );
                            DBWriteManager.SaveSentSmsCommunications(
                                _fileSubmission.TaxRefNo
                                , string.Format(smsBody, _fileSubmission.TaxRefNo)
                                , _fileSubmission.Year
                                );
                        }
                    }
                }
                catch (Exception)
                {
                    ;
                }
                try
                {
                    if (!string.IsNullOrEmpty(_fileSubmission.ContactEmail))
                    {
                        //var attachment = FdrCommon.GetFileRejectionLetter(SubmissionId);
                        var service     = new FDRQueueService();
                        var messageBody = string.Format(
                            DBReadManager.GetMasterLocalFileNotificationEmailBodyTemplate(4),
                            _fileSubmission.TaxRefNo);
                        service.SendEmail(
                            messageBody
                            , "CBC File validation outcome"
                            , _fileSubmission.ContactEmail
                            , null
                            , "CBC File validation outcome"
                            , _fileSubmission.TaxRefNo
                            , _fileSubmission.Year
                            );
                    }
                }
                catch (Exception)
                {
                    ;
                }

                //SEND EFILING ACCEPTANCE LETTERS

                try
                {
                    var attachment = FdrCommon.GetEfilingRejectionLetter(_fileSubmission.TaxRefNo, _fileSubmission.Year, Convert.ToDecimal(SubmissionId));
                    if (!string.IsNullOrEmpty(attachment))
                    {
                        var service = new FDRQueueService();
                        service.SendLetter
                        (
                            _fileSubmission.SubmissionId
                            , _fileSubmission.TaxRefNo
                            , _fileSubmission.Year
                            , attachment
                            , "Rejection of Master Files and Local Files"
                            , false
                            , Sars.Systems.Security.ADUser.CurrentSID
                        );
                    }
                }
                catch (Exception)
                {
                    ;
                }
                //Accepted With Warnings = 5
                status = 5;
            }
            if (status != 0)
            {
                DBWriteManager.ChangeSubmissionStatus(Convert.ToDecimal(SubmissionId), status);
            }
        }
        else
        {
            MessageBox.Show("No validation was submitted");
        }
    }