private void RemoveRecord() { if (MessageBox.Show("Are sure to delete '" + CurrentSelectedItem.StandardName + "' and it's all students ? ", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No) { return; } SchoolObjContext context = new SchoolObjContext(); TestApp.EntityDatabase.DomainClasses.Standard std = context.Standards.Where(s => s.StandardId == CurrentSelectedItem.StandardId).FirstOrDefault(); if (std == null) { return; } context.Standards.Remove(std); context.SaveChanges(); MainViewModel.RefreshView("Result Report"); //MainViewModel.CloseTabs("Subjects"); MainViewModel.RefreshView("Subjects"); MainViewModel.RefreshView("Add Students"); MainViewModel.RefreshView("Show Students"); StandardList.Remove(CurrentSelectedItem); }
private void AddNewStandard() { SchoolObjContext context = new SchoolObjContext(); if (NameToBeAdded != null && NameToBeAdded != " " && NameToBeAdded != "") { EntityDatabase.DomainClasses.Standard std = new EntityDatabase.DomainClasses.Standard() { StandardName = NameToBeAdded }; context.Standards.Add(std); context.SaveChanges(); StandardList.Add(context.Standards.AsEnumerable().Last()); var StdId = context.Standards.Where(s => s.StandardName == NameToBeAdded).FirstOrDefault().StandardId; NameToBeAdded = ""; MainViewModel.RefreshView("Subjects"); MainViewModel.RefreshView("Show Students"); MainViewModel.RefreshView("Result Report"); MainViewModel.Tabs.Add(new SubjectViewModel(StdId)); } }
public StandardViewModel() { TabTitle = "Standards"; SchoolObjContext context = new SchoolObjContext(); StandardList = new ObservableCollection <EntityDatabase.DomainClasses.Standard>(context.Standards.ToList()); //var Result = ( // from Standard in context.Standards // select new // { // StandardId = Standard.StandardId, // StandardName = Standard.StandardName, // TotalSubjects = context.Subjects.Where(s => s.StandardId == Standard.StandardId).Count() // }).ToList(); //StandardList = new ObservableCollection<object>(Result); SaveChangesCommand = new RelayCommand((x) => { context.SaveChanges(); MainViewModel.RefreshView("Subjects"); MainViewModel.RefreshView("Add Students"); MainViewModel.RefreshView("Show Students"); }); AddStandardCommand = new RelayCommand((x) => AddNewStandard()); RemoveRecordCommand = new RelayCommand((x) => RemoveRecord()); ShowSubjectsCommand = new RelayCommand((x) => MainViewModel.Tabs.Add(new SubjectViewModel(CurrentSelectedItem.StandardId.ToString()))); this.UserControl = new UserControls.Standard() { DataContext = this }; }
private void SaveMarks() { if (EditBox > 100 || EditBox < 0) { MessageBox.Show("Marks must be >=0 and <100", "Invalid Entry", MessageBoxButton.OK, MessageBoxImage.Warning); return; } if (SelectedSubjectItem == null) { return; } dynamic tmp = SelectedSubjectItem; int ID = tmp.MarksheetId; SchoolObjContext Context = new SchoolObjContext(); var SubjectMark = Context.AllMarks.Where(s => s.Id == ID).FirstOrDefault(); SubjectMark.Mark = EditBox; Context.SaveChanges(); MainViewModel.RefreshView("Result Report"); if (SelectedSubjectItem != null) { var index = SubjectAndMarks.IndexOf(SelectedSubjectItem); if (SubjectAndMarks.Count - 1 > index) { SelectedSubjectItem = SubjectAndMarks[index + 1]; } else { SelectedSubjectItem = SubjectAndMarks[0]; } } Calculate(); if (Result == "PASS") { CaryForwardVisibility = Visibility.Visible; } else { CaryForwardVisibility = Visibility.Hidden; } }
private void AddSubject() { if (EditBox == "") { return; } SchoolObjContext Context = new SchoolObjContext(); var subject = new Subject() { Name = EditBox, StandardId = SelectedStandard.StandardId, Standard = null }; Context.Subjects.Add(subject); if (Context.SaveChanges() > 0) { //MessageBox.Show("Subject Added Successfully", "Success !", MessageBoxButton.OK, MessageBoxImage.Information); AllSubjects.Add(subject); EditBox = ""; MainViewModel.RefreshView("Subjects"); } }
private void RemoveSubject() { if (SelectedGridItem == null) { return; } dynamic tmp = SelectedGridItem; int id = tmp.Id; SchoolObjContext Context = new SchoolObjContext(); var obj = Context.Subjects.Where(s => s.Id == id).FirstOrDefault(); Context.Subjects.Remove(obj); if (Context.SaveChanges() > 0) { MainViewModel.RefreshView("Subjects"); MessageBox.Show("Subject Deleted Successfully", "Success !", MessageBoxButton.OK, MessageBoxImage.Information); AllSubjects.Remove(SelectedGridItem); EditBox = ""; } }
private void SaveChanges() { if (SelectedGridItem == null) { return; } dynamic tmp = SelectedGridItem; int id = tmp.Id; SchoolObjContext Context = new SchoolObjContext(); Subject obj = Context.Subjects.Where(s => s.Id == id).FirstOrDefault(); if (obj != null) { obj.Name = EditBox; if (Context.SaveChanges() > 0) { MainViewModel.RefreshView(this.TabTitle); MessageBox.Show("Subject Edited Successfully", "Success !", MessageBoxButton.OK, MessageBoxImage.Information); } //int i = AllSubjects.IndexOf(SelectedGridItem); //AllSubjects[i] = obj as object; EditBox = ""; } }
private void GenerateResult() { SchoolObjContext context = new SchoolObjContext(); //var Student = context.Students.OrderByDescending(s => s.StudentId).FirstOrDefault<Student>(); List <Subject> SubList = (from Subject in context.Subjects where (Subject.StandardId == Student.StandardId) select Subject).ToList(); foreach (Subject Item in SubList) { AllMarks sheet = new AllMarks() { StudentId = Student.StudentId, StandardId = Student.StandardId, SubjectId = Item.Id, Mark = 0 }; context.AllMarks.Add(sheet); } context.SaveChanges(); MainViewModel.RefreshView("Result Report"); }
//Call when new adding public StudentViewModel() { TabTitle = "Add Students"; ButtonName = "Add"; SchoolObjContext context = new SchoolObjContext(); AllStandards = context.Standards.ToList(); SubmitStudentCommand = new RelayCommand(x => { if (!CheckCanSubmit()) { MessageBox.Show("Fill all the field properly", "Validation Error", MessageBoxButton.OK, MessageBoxImage.Error); //CanShowResult = false; return; } SchoolObjContext ctx = new SchoolObjContext(); //EntityDatabase.DomainClasses.Standard stan = ctx.Standards.Where(s => s.StandardId == this.SelectedStandard.StandardId).FirstOrDefault(); ctx.Students.Add(new Student() { FirstName = this.FirstName, MiddleName = this.MiddleName, LastName = this.LastName, Age = this.Age, City = this.City, StandardId = this.SelectedStandard.StandardId, Doc = this.DocFile, DocType = this.DocFileType }); if (ctx.SaveChanges() > 0) { MessageBox.Show("Student Registered Successfully", "Registered !", MessageBoxButton.OK, MessageBoxImage.Information); //CanShowResult = true; MainViewModel.RefreshView("Show Students"); } else { //CanShowResult = false; MessageBox.Show("Student Registration Failed", "Registration Failed !", MessageBoxButton.OK, MessageBoxImage.Error); } this.FirstName = MiddleName = LastName = ""; this.Age = 0; this.City = ""; this.SelectedStandard = ctx.Standards.FirstOrDefault(); this.DocFile = null; OnPropertyChanged("SelectedStandard"); }); //ShowResultCommand = new RelayCommand(x => //{ // SubmitStudentCommand.Execute(null); // if (CanShowResult) // MainViewModel.Tabs.Add(new ResultViewModel(context.Students.OrderByDescending(s => s.StudentId).FirstOrDefault<Student>().StudentId)); //}); UserControl = new UserControls.Student { DataContext = this }; }
//Call when Editing public StudentViewModel(int Id) { TabTitle = "Edit Students"; ButtonName = "Save Changes"; SchoolObjContext context = new SchoolObjContext(); if (Id > 0) { using (var Context = new SchoolObjContext()) { Student obj = Context.Students.Where(s => s.StudentId == Id).FirstOrDefault(); AllStandards = context.Standards.Where(s => s.StandardId == obj.StandardId).ToList(); if (obj != null) { this.FirstName = obj.FirstName; this.MiddleName = obj.MiddleName; this.LastName = obj.LastName; this.Age = obj.Age; this.City = obj.City; this.DocFileType = obj.DocType; this.DocFile = obj.Doc; SelectedStandard = Context.Standards.Where(s => s.StandardId == obj.StandardId).FirstOrDefault(); for (int i = 0; i < AllStandards.Count; i++) { if (AllStandards[i].StandardId == SelectedStandard.StandardId) { StdIndex = i; break; } } } } } //Update edited student SubmitStudentCommand = new RelayCommand(x => { if (!CheckCanSubmit()) { MessageBox.Show("Fill all the field properly", "Validation Error", MessageBoxButton.OK, MessageBoxImage.Error); //CanShowResult = false; return; } //else // CanShowResult = true; EntityDatabase.DomainClasses.Student stud = context.Students.Where(s => s.StudentId == Id).FirstOrDefault(); if (stud != null) { stud.FirstName = this.FirstName; stud.MiddleName = this.MiddleName; stud.LastName = this.LastName; stud.Age = this.Age; stud.City = this.City; stud.StandardId = this.SelectedStandard.StandardId; stud.Doc = this.DocFile; stud.DocType = this.DocFileType; if (context.SaveChanges() > 0) { MainViewModel.RefreshView("Show Students"); MainViewModel.RefreshView("Result Report"); MessageBox.Show("Student Edited Successfully", "Edited !", MessageBoxButton.OK, MessageBoxImage.Information); //CanShowResult = true; } //else //{ // MessageBox.Show("Student not Edited", "Failed !", MessageBoxButton.OK, MessageBoxImage.Error); // CanShowResult = false; //} OnPropertyChanged("SelectedStandard"); this.CloseTabCommand.Execute(null); MainViewModel.Tabs.Add(new ShowAllStudentViewModel()); } }); //ShowResultCommand = new RelayCommand(x => //{ // this.SubmitStudentCommand.Execute(null); // if (CanShowResult) // MainViewModel.Tabs.Add(new ViewModels.ResultViewModel(Id)); //}); UserControl = new UserControls.Student { DataContext = this }; }