CapDemo.GUI.ImportQuestion.btn_SaveImport_Click C# (CSharp) Method

btn_SaveImport_Click() private method

private btn_SaveImport_Click ( object sender, EventArgs e ) : void
sender object
e System.EventArgs
return void
        private void btn_SaveImport_Click(object sender, EventArgs e)
        {
            if (cmb_Catalogue.SelectedItem != null)
            {
                try
                {
                    if (txt_FilePath.Text == "")
                    {

                        MessageBox.Show("Vui lòng chọn đường dẫn đến tập tin trước khi lưu!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        int count = 0;
                        foreach (DataGridViewRow row in dgv_Question.Rows)
                        {
                            if (row.Cells["Check"].Value != null && (bool)row.Cells["Check"].Value == true)
                            {
                                count++;
                            }
                        }
                        if (count>0)
                        {
                            //GET CATALOGUE ID
                            CatalogueBL CatBL = new CatalogueBL();
                            List<DO.Catalogue> CatList;
                            CatList = CatBL.GetCatalogue();
                            if (CatList != null)
                                for (int i = 0; i < CatList.Count; i++)
                                {
                                    if (CatList.ElementAt(i).NameCatalogue == cmb_Catalogue.SelectedItem.ToString())
                                    {
                                        IDCat = Convert.ToInt32(CatList.ElementAt(i).IDCatalogue);
                                    }
                                }

                            int CheckQuestion = 0;
                            Question question = new Question();
                            Answer answer = new Answer();
                            QuestionBL questionBL = new QuestionBL();

                            foreach (DataGridViewRow row in dgv_Question.Rows)
                            {
                                if (row.Cells["Check"].Value != null && (bool)row.Cells["Check"].Value == true)
                                {
                                    if (row.Cells["TypeQuestion"].Value.ToString().Trim() == "shortanswer")
                                    {
                                        string[] AnswerContent = row.Cells["AnswerContent"].Value.ToString().Trim().Split(new string[] { "</answer>" }, StringSplitOptions.None);
                                        string[] AnswerItem = AnswerContent[0].Split(new string[] { "---" }, StringSplitOptions.None);

                                        question.QuestionTitle = row.Cells["QuestionTitle"].Value.ToString().Trim();
                                        question.NameQuestion = row.Cells["NameQuestion"].Value.ToString().Trim();
                                        question.TypeQuestion = row.Cells["TypeQuestion"].Value.ToString().Trim();
                                        question.IDCatalogue = IDCat;
                                        question.Date = DateTime.Now;
                                        questionBL.AddQuestion(question);

                                        //answer.IsCorrect = true;
                                        answer.Check = 1;
                                        answer.ContentAnswer = AnswerItem[1].ToString().Trim();
                                        answer.IDQuestion = questionBL.MaxIDQuestion();
                                        answer.IDCatalogue = IDCat;
                                        questionBL.AddAnswer(answer);

                                        CheckQuestion++;

                                    }
                                    else
                                    {
                                        string[] AnswerContent = row.Cells["AnswerContent"].Value.ToString().Trim().Split(new string[] { "</answer>" }, StringSplitOptions.None);
                                        //ADD QUESTION MULTIPLE CHOICE
                                        question.QuestionTitle = row.Cells["QuestionTitle"].Value.ToString().Trim();
                                        question.NameQuestion = row.Cells["NameQuestion"].Value.ToString().Trim();
                                        question.TypeQuestion = "";
                                        question.IDCatalogue = IDCat;
                                        question.Date = DateTime.Now;
                                        questionBL.AddQuestion(question);
                                        CheckQuestion++;

                                        int countMultipleChoice = 0;

                                        for (int i = 0; i < AnswerContent.Length - 1; i++)
                                        {
                                            string[] AnswerItem = AnswerContent[i].Split(new string[] { "---" }, StringSplitOptions.None);

                                            if (Convert.ToInt32(AnswerItem[0].ToString().Trim()) > 0)
                                            {
                                                //answer.IsCorrect = true;
                                                answer.Check = 1;
                                                answer.ContentAnswer = AnswerItem[1].ToString().Trim();
                                                answer.IDQuestion = questionBL.MaxIDQuestion();
                                                answer.IDCatalogue = IDCat;
                                                questionBL.AddAnswer(answer);
                                                countMultipleChoice++;
                                            }
                                            else
                                            {
                                                //answer.IsCorrect = false;
                                                answer.Check = 0;
                                                answer.ContentAnswer = AnswerItem[1].ToString().Trim();
                                                answer.IDQuestion = questionBL.MaxIDQuestion();
                                                answer.IDCatalogue = IDCat;
                                                questionBL.AddAnswer(answer);
                                            }//end if
                                        }//end for

                                        //UPDATE QUESTION TYPE
                                        if (countMultipleChoice == 1)
                                        {
                                            question.TypeQuestion = "onechoice";
                                            question.IDQuestion = questionBL.MaxIDQuestion();
                                            questionBL.EditQuestionTypebyID(question);
                                        }
                                        else
                                        {
                                            question.TypeQuestion = "multichoice";
                                            question.IDQuestion = questionBL.MaxIDQuestion();
                                            questionBL.EditQuestionTypebyID(question);
                                        }
                                    }
                                }

                            }//end foreach

                            //CLOSE FORM
                            if (CheckQuestion > 0)
                            {

                                MessageBox.Show("Nhập " + CheckQuestion + " câu hỏi từ file thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                CheckQuestion = 0;
                                Form FindForm = this.FindForm();
                                FindForm.Close();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Vui lòng chọn câu hỏi trước khi lưu!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }

                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Hệ thống lưu không thành công vì do định dạng file không đúng!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Vui lòng chọn chủ đề!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }