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 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)); } }
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 RemoveRecord() { if (MessageBox.Show("Are sure to delete this student ? ", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No) { return; } SchoolObjContext context = new SchoolObjContext(); dynamic item = CurrentItem; EntityDatabase.DomainClasses.Student SelectedStudent = CurrentItem.Student; EntityDatabase.DomainClasses.Student obj = context.Students.Where(s => s.StudentId == SelectedStudent.StudentId).FirstOrDefault(); if (obj == null) { return; } context.Students.Remove(obj); context.SaveChanges(); AllStudents.Remove(CurrentItem); }
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 CaryForward() { if (SelectedStandard == null) { return; } if (MessageBox.Show("Are you sure to caryforward \"" + Student.MiddleName + "\" in standard " + Standard.StandardName + " to " + SelectedStandard.StandardName, "Confirm !!!", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes) { using (SchoolObjContext context = new SchoolObjContext()) { //List<Subject> SubList = (from Subject in context.Subjects // where (Subject.StandardId == SelectedStandard.StandardId) // select Subject).ToList(); //foreach (Subject Item in SubList) //{ // AllMarks sheet = new AllMarks() // { // StudentId = Student.StudentId, // StandardId = SelectedStandard.StandardId, // SubjectId = Item.Id, // Mark = 0 // }; // context.AllMarks.Add(sheet); //} var stud = context.Students.Where(s => s.StudentId == Student.StudentId).FirstOrDefault(); //stud.Standard = SelectedStandard; stud.StandardId = SelectedStandard.StandardId; var tmp = context.SaveChanges(); this.CloseTabCommand.Execute(null); MainViewModel.CloseTabs("Show Students"); MainViewModel.Tabs.Add(new ShowAllStudentViewModel()); } } return; }
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 }; }
public ReportResultViewModel() { this.TabTitle = "Result Report"; this.UserControl = new ResultReport() { DataContext = this }; using (var context = new SchoolObjContext()) { AllStandards = context.Standards.ToList(); AllStandards.Insert(0, new EntityDatabase.DomainClasses.Standard() { StandardId = 0, StandardName = "All" }); SelectedStandard = AllStandards[0]; } ShowResultCommand = new RelayCommand(x => MainViewModel.Tabs.Add(new ViewModels.ResultViewModel((int)CurrentItem.Student.StudentId, (int)CurrentItem.StandardId))); PrintResultCommand = new RelayCommand(x => { Student student = x as Student; student.StandardId = (int)(CurrentItem as dynamic).StandardId; SchoolObjContext Context = new SchoolObjContext(); SaveFileDialog Dialog = new SaveFileDialog() { Filter = "FDF (.*pdf)|*.pdf" }; if (Dialog.ShowDialog() == false) { return; } System.IO.FileStream fs = new FileStream(Dialog.FileName, FileMode.Create); Document document = new Document(PageSize.A4, 7f, 5f, 5f, 0f); PdfWriter writer = PdfWriter.GetInstance(document, fs); document.Open(); PdfPTable StudentInfoTable = new PdfPTable(2); PdfPCell cell = new PdfPCell(new Phrase("Student")); cell.Colspan = 2; cell.HorizontalAlignment = Element.ALIGN_CENTER; cell.Padding = 10; StudentInfoTable.AddCell(cell); StudentInfoTable.AddCell("Name"); StudentInfoTable.AddCell(student.FirstName + " " + student.MiddleName + " " + student.LastName); StudentInfoTable.AddCell("Seat No."); StudentInfoTable.AddCell(student.StudentId.ToString()); StudentInfoTable.AddCell("Standard"); StudentInfoTable.AddCell(Context.Standards.Where(s => s.StandardId == student.StandardId).Select(s => s.StandardName).FirstOrDefault().ToString()); StudentInfoTable.AddCell("Age"); StudentInfoTable.AddCell(student.Age.ToString()); StudentInfoTable.AddCell("City"); StudentInfoTable.AddCell(student.City); document.Add(StudentInfoTable); PdfPTable ResultTable = new PdfPTable(3); cell = new PdfPCell(new Phrase("Result")); cell.Colspan = 3; cell.HorizontalAlignment = Element.ALIGN_CENTER; cell.Padding = 10; ResultTable.AddCell(cell); var SubAndMarks = (from Subject in Context.Subjects join Marksheet in Context.AllMarks on Subject.Id equals Marksheet.SubjectId orderby Marksheet.StudentId descending where Marksheet.StudentId == student.StudentId && Marksheet.StandardId == student.StandardId select new { MarksheetId = Marksheet.Id, SubjectName = Subject.Name, Mark = Marksheet.Mark, Result = Marksheet.Mark < 35 ? "Fail" : "Pass" }).ToList(); int Total = 0, Count = 0; foreach (dynamic item in SubAndMarks) { ResultTable.AddCell(item.SubjectName as string); int Mark = ((int)item.Mark); Total += Mark; Count++; ResultTable.AddCell(Mark.ToString()); ResultTable.AddCell(item.Result as string); } cell = new PdfPCell(new Phrase("Total : " + Total)); cell.Colspan = 3; cell.Padding = 5; ResultTable.AddCell(cell); cell = new PdfPCell(new Phrase("Percentage : " + (CurrentItem as dynamic).Percentage + "%")); cell.Colspan = 3; cell.Padding = 5; ResultTable.AddCell(cell); cell = new PdfPCell(new Phrase("Result : " + (CurrentItem as dynamic).Result)); cell.Colspan = 3; cell.Padding = 5; ResultTable.AddCell(cell); document.Add(ResultTable); document.Close(); writer.Close(); fs.Close(); Context.StudentResultCanEditables.Add(new StudentResultCanEditable() { StandardId = student.StandardId, StudentId = student.StudentId }); Context.SaveChanges(); }); }