/** * save new term */ private void saveNewTerm() { if (validate()) { //check if term exited or not if (isExistedTermCode(txbTerm_termCode.Text.Trim())) { txbTerm_termCode.BackColor = Color.Red; MessageBox.Show("マスタコードが存在しています"); return; } txbTerm_termCode.BackColor = Color.Empty; //save new term CpTerm term = new CpTerm(); term.SchoolId = schoolId; var year = tbdropbxTerm_Year.SelectedValue; if ((year != null) && (year.GetType() == typeof(int))) term.Year = Convert.ToInt32(year); term.Code = txbTerm_termCode.Text.Trim(); term.Name = txbTerm_termName.Text.Trim(); db.CpTerms.Add(term); try { db.SaveChanges(); //set term_id for view grade and class term_id = term.Id; //change to edit mode isEditMode = true; } catch (Exception ex) { MessageBox.Show(ex.Message); } //save new grade if (isNullString(txbTerm_gradeCode.Text) || isNullString(txbTerm_gradeName.Text)) { MessageBox.Show("保存完了しました"); return; } CpGradeCode gradeCode = new CpGradeCode(); gradeCode.Code = txbTerm_gradeCode.Text.Trim(); gradeCode.Name = txbTerm_gradeName.Text.Trim(); gradeCode.TermId = term.Id; db.CpGradeCodes.Add(gradeCode); try { db.SaveChanges(); } catch (Exception ex) { MessageBox.Show(ex.Message); } //save new class if (isNullString(txbTerm_classCode.Text) || isNullString(txbTerm_className.Text)) { MessageBox.Show("保存完了しました"); return; } CpClass classOfGrade = new CpClass(); classOfGrade.Code = txbTerm_classCode.Text.Trim(); classOfGrade.Name = txbTerm_className.Text.Trim(); classOfGrade.SchoolId = schoolId; classOfGrade.TermId = term.Id; classOfGrade.GradeCodeId = gradeCode.Id; db.CpClasses.Add(classOfGrade); try { db.SaveChanges(); MessageBox.Show("保存完了しました"); resetInput(); } catch (Exception ex) { MessageBox.Show(ex.Message); } loadGradeAndClassOfTerm(term.Id); } }
/** * save edited term */ private void saveEditedTerm() { if (!validate()) return; //check term code existed or not var term = (from c in db.CpTerms where c.Code == txbTerm_termCode.Text.Trim() && c.SchoolId == VariableGlobal.school_id select c).SingleOrDefault(); if (term != null) { if (term.Id != term_id) { txbTerm_termCode.BackColor = Color.Red; MessageBox.Show("指定したマスタコードが既に存在しています"); return; } } txbTerm_termCode.BackColor = Color.Empty; //save term info term = (from c in db.CpTerms where c.Id == term_id select c).SingleOrDefault(); if (term == null) return; term.Code = txbTerm_termCode.Text.Trim(); term.Name = txbTerm_termName.Text.Trim(); var year = tbdropbxTerm_Year.SelectedValue; if ((year != null) && (year.GetType() == typeof(int))) term.Year = Convert.ToInt32(year); try { db.SaveChanges(); } catch (Exception ex) { MessageBox.Show(ex.Message); } //save grade info //if there is no grade input, just save term and exit if (isNullString(txbTerm_gradeCode.Text) || isNullString(txbTerm_gradeName.Text)) { MessageBox.Show("編集完了しました"); loadGradeAndClassOfTerm(term.Id); return; } // if there is grade input //1. check if grade of term existed or not var grade = (from c in db.CpGradeCodes where c.Code == txbTerm_gradeCode.Text.Trim() && c.TermId == term.Id select c).SingleOrDefault(); //1. if grade of term not existed, create new if (grade == null) { grade = new CpGradeCode(); grade.Code = txbTerm_gradeCode.Text.Trim(); grade.Name = txbTerm_gradeName.Text.Trim(); grade.TermId = term.Id; db.CpGradeCodes.Add(grade); } else { grade.Code = txbTerm_gradeCode.Text.Trim(); grade.Name = txbTerm_gradeName.Text.Trim(); } try { db.SaveChanges(); } catch (Exception ex) { MessageBox.Show(ex.Message); } //save class info //if there is no class input, just save term, grade and exit if (isNullString(txbTerm_classCode.Text) || isNullString(txbTerm_className.Text)) { MessageBox.Show("編集完了しました"); loadGradeAndClassOfTerm(term.Id); return; } //1. check if class of grade existed or not var classOfGrade = (from c in db.CpClasses where c.Code == txbTerm_classCode.Text.Trim() && c.TermId == term.Id && c.GradeCodeId == grade.Id select c).SingleOrDefault(); if (classOfGrade == null) { classOfGrade = new CpClass(); classOfGrade.Code = txbTerm_classCode.Text.Trim(); classOfGrade.Name = txbTerm_className.Text.Trim(); classOfGrade.SchoolId = schoolId; classOfGrade.TermId = term.Id; classOfGrade.GradeCodeId = grade.Id; db.CpClasses.Add(classOfGrade); } else { classOfGrade.Code = txbTerm_classCode.Text.Trim(); classOfGrade.Name = txbTerm_className.Text.Trim(); } try { db.SaveChanges(); MessageBox.Show("編集完了しました"); loadGradeAndClassOfTerm(term.Id); } catch (Exception ex) { MessageBox.Show(ex.Message); } }