protected void btnAddQuestion_Click(object sender, EventArgs e)
    {
        DbTrans trans = new DbTrans();
        trans.CreateTransaction();
        if (ValidQuestion() == true) {
            if (SaveQuestionnaire(trans) == true){
                QuestionnaireQuestionsPara para = new QuestionnaireQuestionsPara();
                para.ID = Convert.ToInt64(txtQuestionID.Text);
                para.QUESTIONNAIRE_SECTION_ID = Convert.ToInt64(cmbSectionID.SelectedValue);
                para.QUESTION_NAME = txtQuestionName.Text.Trim();
                para.CHOICE_TYPE_ID = Convert.ToInt64(cmbChoiceTypeID.SelectedValue);
                para.QUESTION_POINT = Convert.ToDouble(txtPoint.Text.Trim());
                para.IS_REQUIRE = (chkRequire.Checked ? 'Y' : 'N');

                QuestionnaireENG eng = new QuestionnaireENG();
                bool ret = eng.SaveQuestion(para, trans, Config.GetUserName());
                if (ret == true)
                {
                    if (SaveQuestionChoice(eng.QUESTION_ID, trans) == true)
                    {
                        trans.CommitTransaction();
                        txtQuestionID.Text = eng.QUESTION_ID.ToString();
                        SetQuestionnaireForm(Convert.ToInt64(txtID.Text));
                        ClearQuestionForm();
                        Config.SetAlert("บันทึกข้อมูลเรียบร้อย", this);
                    }
                    else {
                        trans.RollbackTransaction();
                        Config.SetAlert(eng.ErrorMessage, this);
                    }
                }
                else {
                    trans.RollbackTransaction();
                    Config.SetAlert(eng.ErrorMessage, this);
                }
            }
        }
    }
    protected void btnAddSection_Click(object sender, EventArgs e)
    {
        DbTrans trans = new DbTrans();
        trans.CreateTransaction();
        if (ValidSection() == true)
        {
            if (SaveQuestionnaire(trans) == true) {
                QuestionnaireSectionPara para = new QuestionnaireSectionPara();
                para.ID = Convert.ToInt64(txtQuestionSectionID.Text);
                para.QUESTIONNAIRE_ID = Convert.ToInt64(txtID.Text);
                para.SECTION_NAME = txtSectionName.Text.Trim();
                para.DESCRIPTION = txtSectionDesc.Text.Trim();
                para.SECTION_TYPE_ID = Convert.ToInt64(cmbSectionTypeID.SelectedValue);
                para.CHOICE_QTY = (txtChoiceQty.Text.Trim()=="" ? 0 : Convert.ToInt64(txtChoiceQty.Text));

                QuestionnaireENG eng = new QuestionnaireENG();
                bool ret = eng.SaveQuestionnaireSection(para, trans, Config.GetUserName());
                if (ret == true)
                {
                    trans.CommitTransaction();
                    txtQuestionSectionID.Text = eng.QUESTIONNAIRE_SECTION_ID.ToString();
                    SetQuestionnaireForm(Convert.ToInt64(txtID.Text));
                    SetSectionList();
                    Config.SetAlert("บันทึกข้อมูลเรียบร้อย", this);
                }
                else
                {
                    trans.RollbackTransaction();
                    Config.SetAlert(eng.ErrorMessage, this);
                }
            }
        }
    }
 protected void btnSave_Click(object sender, EventArgs e)
 {
     if (ValidQuestionnaire() == true) {
         DbTrans trans = new DbTrans();
         trans.CreateTransaction();
         if (SaveQuestionnaire(trans) == true)
         {
             trans.CommitTransaction();
             Config.SetAlert("บันทึกข้อมูลเรียบร้อย", this);
             TabContainer1.Visible = true;
         }
         else {
             trans.RollbackTransaction();
             Config.SetAlert(_err, this);
         }
     }
 }
    protected void btnSave_Click(object sender, System.EventArgs e)
    {
        if (Valid() == true)
        {
            bool ret = false;

            DbTrans trans = new DbTrans();
            trans.CreateTransaction();

            string LoginName = "SYSTEM";

            QuestionnaireENG eng = new QuestionnaireENG();

            AnswerPara ans = new AnswerPara();
            ans.QUESTIONNAIRE_ID =Convert.ToInt64(txtID.Text);
            ans.ANSWER_NAME = "System Admin";
            ans.ANSWER_DATE = DateTime.Now;
            ans.SESSION_ID = Session.SessionID;

            if (eng.SaveAnswer(ans, LoginName, trans) == true)
            {
                for (int m = 0; m < gvSection.Rows.Count; m++)
                {
                    GridView gvQuestion = (GridView)gvSection.Rows[m].Cells[1].FindControl("gvQuestion");
                    for (int i = 0; i < gvQuestion.Rows.Count; i++)
                    {
                        Label lblQuestionName = (Label)gvQuestion.Rows[i].Cells[2].FindControl("lblQuestionName");
                        long QuestionID = Convert.ToInt64(gvQuestion.Rows[i].Cells[0].Text);

                        AnswerResultPara Apara = new AnswerResultPara();
                        //Apara.ANSWER_NAME =
                        //Apara.QUESTIONNAIRE_ID = Convert.ToInt64(txtID.Text);
                        Apara.ANSWER_ID = eng.ANSWER_ID;
                        Apara.QUESTIONNAIRE_QUESTION_ID = QuestionID;
                        Label lblChoiceTypeID = (Label)gvQuestion.Rows[i].Cells[1].FindControl("lblChoiceTypeID");
                        Label lblQuestionPoint = (Label)gvQuestion.Rows[i].Cells[1].FindControl("lblQuestionPoint");

                        if (lblChoiceTypeID.Text == "1")
                        {
                            //Section
                            long SectionID = Convert.ToInt64(gvSection.Rows[m].Cells[0].Text);
                            QuestionnaireSectionPara QsPara = eng.GetQuestionnaireSectionPara(SectionID, trans);
                            if (QsPara.SECTION_TYPE_ID == 1)
                            {
                                //แบบหลายตัวเลือก ตอบได้ 1 ข้อ สำหรับส่วนที่ 1
                                RadioButtonList rdiChoice = (RadioButtonList)gvQuestion.Rows[i].Cells[2].FindControl("rdiChoice");
                                if (rdiChoice.SelectedValue != "")
                                {
                                    QuestionnaireQuestionsChoicePara ctPara = eng.GetQuestionnaireQuestionChoicePara(Convert.ToInt64(rdiChoice.SelectedValue), trans);
                                    Apara.CHOICE_TYPE_ID = 1;
                                    Apara.RESULT_CHOICE_NAME = rdiChoice.SelectedItem.Text;
                                    Apara.POINT = ctPara.POINT;
                                    if (ctPara.IS_OTHER == 'Y')
                                    {
                                        TextBox txtRadioChoice = (TextBox)gvQuestion.Rows[i].Cells[2].FindControl("txtRadioChoice");
                                        Apara.RESULT_CHOICE_NAME = txtRadioChoice.Text;
                                    }
                                    ret = eng.SaveAnswerResult(Apara, LoginName, trans);
                                }
                            }
                            else if (QsPara.SECTION_TYPE_ID == 2)
                            {
                                //แบบหลายตัวเลือก ตอบได้ 1 ข้อ สำหรับส่วนที่ 2
                                RadioButtonList rdiSec2Choice = (RadioButtonList)gvQuestion.Rows[i].Cells[3].FindControl("rdiSec2Choice");
                                if (rdiSec2Choice.SelectedValue != "")
                                {
                                    Apara.CHOICE_TYPE_ID = 1;
                                    Apara.RESULT_CHOICE_NAME = rdiSec2Choice.SelectedValue;
                                    Apara.POINT = Convert.ToDouble(rdiSec2Choice.SelectedValue);
                                    ret = eng.SaveAnswerResult(Apara, LoginName, trans);
                                }
                            }
                        }
                        else if (lblChoiceTypeID.Text == "2")
                        {
                            //แบบหลายตัวเลือก ตอบได้มากกว่า 1 ข้อ
                            CheckBoxList chkChoice = (CheckBoxList)gvQuestion.Rows[i].Cells[2].FindControl("chkChoice");
                            for (int j = 0; j < chkChoice.Items.Count; j++)
                            {
                                if (chkChoice.Items[j].Selected)
                                {
                                    Apara.CHOICE_TYPE_ID = 2;
                                    Apara.RESULT_CHOICE_NAME = chkChoice.Items[j].Text;
                                    QuestionnaireQuestionsChoicePara ctPara = eng.GetQuestionnaireQuestionChoicePara(Convert.ToInt64(chkChoice.Items[j].Value), trans);
                                    Apara.POINT = ctPara.POINT;
                                    if (ctPara.IS_OTHER == 'Y')
                                    {
                                        TextBox txtCheckBoxChoice = (TextBox)gvQuestion.Rows[i].Cells[2].FindControl("txtCheckBoxChoice");
                                        Apara.RESULT_CHOICE_NAME = txtCheckBoxChoice.Text;
                                    }

                                    ret = eng.SaveAnswerResult(Apara, LoginName, trans);
                                }
                            }
                        }
                        else if (lblChoiceTypeID.Text == "3")
                        {
                            //แบบให้ความเห็น ระบุเป็นข้อความ
                            TextBox txtChoice = (TextBox)gvQuestion.Rows[i].Cells[2].FindControl("txtChoice");
                            Apara.CHOICE_TYPE_ID = 3;
                            Apara.RESULT_CHOICE_NAME = txtChoice.Text;
                            Apara.POINT = Convert.ToInt64(lblQuestionPoint.Text);
                            ret = eng.SaveAnswerResult(Apara, LoginName, trans);
                        }
                        else if (lblChoiceTypeID.Text == "4")
                        {
                            //แบบให้ความเห็น ระบุเป็นตัวเลข
                            TextBox txtChoice = (TextBox)gvQuestion.Rows[i].Cells[2].FindControl("txtChoice");
                            Apara.CHOICE_TYPE_ID = 4;
                            Apara.RESULT_CHOICE_NAME = txtChoice.Text;
                            Apara.POINT = Convert.ToInt64(lblQuestionPoint.Text);
                            ret = eng.SaveAnswerResult(Apara, LoginName, trans);
                        }

                        if (ret == false)
                        {
                            break;
                        }
                    }
                }
                if (ret == true)
                    trans.CommitTransaction();
                else
                {
                    trans.RollbackTransaction();
                    Config.SetAlert("Error!!!", this);
                }
            }
            else
            {
                trans.RollbackTransaction();
                Config.SetAlert("Answer Error!!!", this);
            }
        }
    }