/// Add a new result.
        /// </summary>
        protected void grdResults_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("AddNew"))
            {
                lblMsgDtl.Text = "";

                int          ActID                 = Session["ActivityIDX"].ConvertOrDefault <int>();
                DropDownList ddlNewChar            = (DropDownList)grdResults.FooterRow.FindControl("ddlNewChar");
                DropDownList ddlNewTaxa            = (DropDownList)grdResults.FooterRow.FindControl("ddlNewTaxa");
                TextBox      txtNewResultVal       = (TextBox)grdResults.FooterRow.FindControl("txtNewResultVal");
                DropDownList ddlNewUnit            = (DropDownList)grdResults.FooterRow.FindControl("ddlNewUnit");
                DropDownList ddlNewAnalMethod      = (DropDownList)grdResults.FooterRow.FindControl("ddlNewAnalMethod");
                TextBox      txtNewDetectLimit     = (TextBox)grdResults.FooterRow.FindControl("txtNewDetectLimit");
                TextBox      txtNewPQL             = (TextBox)grdResults.FooterRow.FindControl("txtNewPQL");
                TextBox      txtNewLowerQuantLimit = (TextBox)grdResults.FooterRow.FindControl("txtNewLowerQuantLimit");
                TextBox      txtNewUpperQuantLimit = (TextBox)grdResults.FooterRow.FindControl("txtNewUpperQuantLimit");


                DropDownList ddlNewSampFraction    = (DropDownList)grdResults.FooterRow.FindControl("ddlNewSampFraction");
                DropDownList ddlNewResultValueType = (DropDownList)grdResults.FooterRow.FindControl("ddlNewResultValueType");
                DropDownList ddlNewResultStatus    = (DropDownList)grdResults.FooterRow.FindControl("ddlNewResultStatus");
                TextBox      txtNewAnalysisDate    = (TextBox)grdResults.FooterRow.FindControl("txtNewAnalysisDate");

                DropDownList ddlNewBioIntent = (DropDownList)grdResults.FooterRow.FindControl("ddlNewBioIntent");
                DropDownList ddlNewFreqClass = (DropDownList)grdResults.FooterRow.FindControl("ddlNewFreqClass");

                TextBox txtNewComment = (TextBox)grdResults.FooterRow.FindControl("txtNewComment");

                double num;
                if (!(double.TryParse(txtNewResultVal.Text, out num) || txtNewResultVal.Text == "ND" || txtNewResultVal.Text == "NR" || txtNewResultVal.Text == "PAQL" || txtNewResultVal.Text == "PBQL" || txtNewResultVal.Text == "DNQ"))
                {
                    lblMsgDtl.Text = "Result must be numeric, or one of the following values: ND, NR, PAQL, PBQL, DNQ";
                    return;
                }

                //if numeric, then check if result is within valid range (if available for the char/unit pairing)
                if (double.TryParse(txtNewResultVal.Text, out num))
                {
                    T_WQX_REF_CHAR_LIMITS cl = db_Ref.GetT_WQX_REF_CHAR_LIMITS_ByNameUnit(ddlNewChar.SelectedValue, ddlNewUnit.SelectedValue);
                    if (cl != null)
                    {
                        if ((decimal)num < cl.LOWER_BOUND || (decimal)num > cl.UPPER_BOUND)
                        {
                            lblMsgDtl.Text = "Result value is outside acceptable range. Please provide another value and save again.";
                            return;
                        }
                    }
                }
                //*********************END VALIDATION************************************

                db_WQX.InsertOrUpdateT_WQX_RESULT(null, ActID, null, ddlNewChar.SelectedValue, ddlNewSampFraction.SelectedValue,
                                                  txtNewResultVal.Text, ddlNewUnit.SelectedValue, ddlNewResultStatus.SelectedValue, ddlNewResultValueType.SelectedValue, txtNewComment.Text,
                                                  ddlNewBioIntent.SelectedValue, null, ddlNewTaxa.SelectedValue, null, ddlNewAnalMethod.SelectedValue.ConvertOrDefault <int?>(), null, txtNewAnalysisDate.Text.ConvertOrDefault <DateTime?>(),
                                                  txtNewDetectLimit.Text, txtNewPQL.Text, txtNewLowerQuantLimit.Text, txtNewUpperQuantLimit.Text, null, null, null, ddlNewFreqClass.SelectedValue, null, User.Identity.Name);

                //also update activity to set to "U" so it will be flagged for submission to EPA
                db_WQX.UpdateWQX_ACTIVITY_WQXStatus(ActID, "U", chkActInd.Checked, chkWQXInd.Checked);

                grdResults.FooterRow.Visible = false;
                PopulateResultsGrid();
            }
        }
        /// Update an existing result record.
        /// </summary>
        protected void grdResults_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            lblMsgDtl.Text = "";

            int ActID = Session["ActivityIDX"].ConvertOrDefault <int>();

            DropDownList ddlChar            = (DropDownList)grdResults.Rows[e.RowIndex].FindControl("ddlChar");
            DropDownList ddlTaxa            = (DropDownList)grdResults.Rows[e.RowIndex].FindControl("ddlTaxa");
            TextBox      txtResultVal       = (TextBox)grdResults.Rows[e.RowIndex].FindControl("txtResultVal");
            DropDownList ddlUnit            = (DropDownList)grdResults.Rows[e.RowIndex].FindControl("ddlUnit");
            DropDownList ddlAnalMethod      = (DropDownList)grdResults.Rows[e.RowIndex].FindControl("ddlAnalMethod");
            TextBox      txtDetectLimit     = (TextBox)grdResults.Rows[e.RowIndex].FindControl("txtDetectLimit");
            TextBox      txtPQL             = (TextBox)grdResults.Rows[e.RowIndex].FindControl("txtPQL");
            TextBox      txtLowerQuantLimit = (TextBox)grdResults.Rows[e.RowIndex].FindControl("txtLowerQuantLimit");
            TextBox      txtUpperQuantLimit = (TextBox)grdResults.Rows[e.RowIndex].FindControl("txtUpperQuantLimit");

            DropDownList ddlSampFraction    = (DropDownList)grdResults.Rows[e.RowIndex].FindControl("ddlSampFraction");
            DropDownList ddlResultValueType = (DropDownList)grdResults.Rows[e.RowIndex].FindControl("ddlResultValueType");
            DropDownList ddlResultStatus    = (DropDownList)grdResults.Rows[e.RowIndex].FindControl("ddlResultStatus");
            TextBox      txtAnalysisDate    = (TextBox)grdResults.Rows[e.RowIndex].FindControl("txtAnalysisDate");

            DropDownList ddlBioIntent = (DropDownList)grdResults.Rows[e.RowIndex].FindControl("ddlBioIntent");
            DropDownList ddlFreqClass = (DropDownList)grdResults.Rows[e.RowIndex].FindControl("ddlFreqClass");

            TextBox txtComment = (TextBox)grdResults.Rows[e.RowIndex].FindControl("txtComment");

            //*********************VALIDATION****************************************
            double num;

            if (!(double.TryParse(txtResultVal.Text, out num) || txtResultVal.Text == "ND" || txtResultVal.Text == "NR" || txtResultVal.Text == "PAQL" || txtResultVal.Text == "PBQL" || txtResultVal.Text == "DNQ"))
            {
                lblMsgDtl.Text = "Result must be numeric, or one of the following values: ND, NR, PAQL, PBQL, DNQ";
                return;
            }

            //if numeric, then check if result is within valid range (if available for the char/unit pairing)
            if (double.TryParse(txtResultVal.Text, out num))
            {
                T_WQX_REF_CHAR_LIMITS cl = db_Ref.GetT_WQX_REF_CHAR_LIMITS_ByNameUnit(ddlChar.SelectedValue, ddlUnit.SelectedValue);
                if (cl != null)
                {
                    if ((decimal)num < cl.LOWER_BOUND || (decimal)num > cl.UPPER_BOUND)
                    {
                        lblMsgDtl.Text = "Result value is outside acceptable range. " + cl.LOWER_BOUND + " - " + cl.UPPER_BOUND + " Please provide another value and save again.";
                        return;
                    }
                }
            }
            //*********************END VALIDATION************************************

            int SuccID = db_WQX.InsertOrUpdateT_WQX_RESULT(grdResults.DataKeys[e.RowIndex].Values[0].ConvertOrDefault <int>(), ActID, null, ddlChar.SelectedValue,
                                                           ddlSampFraction.SelectedValue, txtResultVal.Text, ddlUnit.SelectedValue,
                                                           ddlResultStatus.SelectedValue, ddlResultValueType.SelectedValue, txtComment.Text, null, null, ddlTaxa.SelectedValue, null,
                                                           ddlAnalMethod.SelectedValue.ConvertOrDefault <int?>(), ddlLabName.SelectedValue.ConvertOrDefault <int?>(), txtAnalysisDate.Text.ConvertOrDefault <DateTime?>(), txtDetectLimit.Text, txtPQL.Text,
                                                           txtLowerQuantLimit.Text, txtUpperQuantLimit.Text, ddlPrepMethod.SelectedValue.ConvertOrDefault <int?>(), txtPrepStartDate.Text.ConvertOrDefault <DateTime?>(), txtDilution.Text,
                                                           ddlFreqClass.SelectedValue, null, User.Identity.Name);

            if (SuccID > 0)
            {
                //also update activity to set to "U" so it will be flagged for submission to EPA
                db_WQX.UpdateWQX_ACTIVITY_WQXStatus(ActID, "U", null, null, User.Identity.Name);

                grdResults.EditIndex = -1;
                PopulateResultsGrid();
            }
            else
            {
                lblMsgDtl.Text = "Error saving update.";
            }
        }