private void LoadData() { using (RRSContext ctx = new RRSContext()) { userAccountBindingSource.DataSource = ctx.UserAccounts.ToList(); } dataGridView_User.ClearSelection(); }
private void CourseManagerForm_Load(object sender, EventArgs e) { using (RRSContext ctx = new RRSContext()) { courseBindingSource.DataSource = ctx.Courses.ToList(); } //dataGridView_Course.Rows[0].Selected = false; dataGridView_Course.ClearSelection(); }
private void cb_Course_SelectedIndexChanged(object sender, EventArgs e) { using (RRSContext ctx = new RRSContext()) { if (cb_Course.SelectedIndex == 0) { } else { try { string tempCourseI = cb_Course.SelectedValue.ToString(); int tempCourseId = Int32.Parse(tempCourseI); var classfication_List = ctx.Classifacations.Where(x => x.CourseId == tempCourseId).ToList(); if (classfication_List.Count > 0) { cb_Classfication.DataSource = classfication_List; } //cb_Course.Items.Add(cb_Course.DataSource = ctx.Courses.ToString()); cb_Classfication.DisplayMember = "ClassificationName"; cb_Classfication.ValueMember = "ClassificationId"; } catch (Exception) { } } } //if (cb_Course. == 0) //{ // using (RRSContext ctx = new RRSContext()) // { // int tempCourseId = 2; // var classfication_List = ctx.Classifacations.Where(x => x.CourseId == tempCourseId).ToList(); // if (classfication_List.Count > 0) // cb_Classfication.DataSource = classfication_List; // //cb_Course.Items.Add(cb_Course.DataSource = ctx.Courses.ToString()); // cb_Classfication.DisplayMember = "ClassificationName"; // cb_Classfication.ValueMember = "ClassificationId"; // } //} //else //{ //} }
private void FillCbCourse() { using (RRSContext ctx = new RRSContext()) { var course_List = ctx.Courses.Where(x => x.IsActive == true).ToList(); if (course_List.Count > 0) { cb_Course.DataSource = course_List; //cb_Course.Items.Add(cb_Course.DataSource = ctx.Courses.ToString()); cb_Course.DisplayMember = "CourseName"; cb_Course.ValueMember = "CourseId"; } } }
private void LoadThesis() { using (RRSContext ctx = new RRSContext()) { var query = from p1 in ctx.ResearchBooks join f1 in ctx.Authors on p1.ThesisTitleId equals f1.AuthorId select new { p1.Title, p1.PublishedYear, p1.CourseNameRb, p1.Remarks, f1.AuthorName /*, f1.AuthorLName*/ }; //Bind the gridview. dataGridView_Thesis.DataSource = query.ToList(); dataGridView_Thesis.Columns[0].HeaderCell.Value = "Title"; dataGridView_Thesis.Columns[1].HeaderCell.Value = "Published Year"; dataGridView_Thesis.Columns[2].HeaderCell.Value = "Course"; dataGridView_Thesis.Columns[3].HeaderCell.Value = "Remarks"; dataGridView_Thesis.Columns[4].HeaderCell.Value = "Author Name"; //dataGridView_Thesis.Columns[4].HeaderCell.Value = "Author Last Name"; } }
private void SaveCoure() { if (dataGridView_Course.SelectedRows.Count > 0) { using (RRSContext ctx = new RRSContext()) { foreach (var item in courseBindingSource.List) { var course = (Course)item; if (course.CourseId <= 0) { ctx.Courses.Add(course); } else { ctx.Courses.Attach(course); ctx.Entry(course).State = System.Data.Entity.EntityState.Modified; } } ctx.SaveChanges(); MessageBox.Show(this, "Update Successfull"); CourseManagerForm_Load(null, null); } } else { using (var ctx = new RRSContext()) { var course = new Course { CourseName = tb_Course.Text, IsActive = true, }; ctx.Courses.Add(course); ctx.SaveChanges(); ClearText(); MessageBox.Show(this, "Save Successfull"); CourseManagerForm_Load(null, null); } } }
private void EditUser() { using (RRSContext ctx = new RRSContext()) { foreach (var item in userAccountBindingSource.List) { var account = (UserAccount)item; if (account.UserId <= 0) { ctx.UserAccounts.Add(account); } else { ctx.UserAccounts.Attach(account); ctx.Entry(account).State = System.Data.Entity.EntityState.Modified; } } ctx.SaveChanges(); MessageBox.Show(this, "Save Successfull"); } }
private void DeleteUser(object dataBoundItem) { if (dataBoundItem == null) { return; } var account = (UserAccount)dataBoundItem; using (RRSContext ctx = new RRSContext()) { var toBeDeleted = ctx.UserAccounts.Where(u => u.UserId == account.UserId).FirstOrDefault(); if (toBeDeleted == null) { return; } ctx.UserAccounts.Remove(toBeDeleted); ctx.SaveChanges(); MessageBox.Show(this, "Delete Successfull"); } }
private void DeleteCourse(object dataBoundItem) { if (dataBoundItem == null) { return; } var course = (Course)dataBoundItem; using (RRSContext ctx = new RRSContext()) { var toBeDeleted = ctx.Courses.Where(u => u.CourseId == course.CourseId).FirstOrDefault(); if (toBeDeleted == null) { return; } ctx.Courses.Remove(toBeDeleted); ctx.SaveChanges(); MessageBox.Show(this, "Delete Successfull"); } }
private void SaveUser() { if (dataGridView_User.SelectedRows.Count > 0) { using (RRSContext ctx = new RRSContext()) { foreach (var item in userAccountBindingSource.List) { var account = (UserAccount)item; if (account.UserId <= 0) { ctx.UserAccounts.Add(account); } else { ctx.UserAccounts.Attach(account); ctx.Entry(account).State = System.Data.Entity.EntityState.Modified; } } ctx.SaveChanges(); MessageBox.Show(this, "Update Successfull"); LoadData(); } } else { if (!(string.IsNullOrEmpty(tb_FirstName.Text) || string.IsNullOrEmpty(tb_LastName.Text) || (String.IsNullOrEmpty(tb_UserName.Text) || string.IsNullOrEmpty(tb_UserPassword.Text)))) { try { using (var ctx = new RRSContext()) { if (ctx.UserAccounts.Any(o => o.Username == tb_UserName.Text)) { MessageBox.Show("Use different username"); } else { var user = new UserAccount() { FirstName = tb_FirstName.Text, MiddleName = tb_MiddleName.Text, LastName = tb_LastName.Text, Username = tb_UserName.Text, UserPassword = tb_UserPassword.Text, UserRole = cb_Role.SelectedItem.ToString(), IsActive = true, }; ctx.UserAccounts.Add(user); ctx.SaveChanges(); LoadData(); MessageBox.Show(this, "Save Successfull"); } } } catch (Exception) { MessageBox.Show("Select Role"); } } else { MessageBox.Show("Please filled up all field)"); } } ClearText(); AccountManagerForm_Load(null, null); }
private void SaveCourse() { if (!(tb_Title.Text == "" || cb_PublishedYear.SelectedIndex == -1 || cb_Course.SelectedIndex == -1 || dataGridView1 == null || dataGridView1.Rows.Count == 0)) { try { using (var ctx = new RRSContext()) { if (ctx.ResearchBooks.Any(o => o.Title == tb_Title.Text)) { MessageBox.Show("Title Already Exist"); } else { //var course = new Course(); //course.ThesisTitleId = (Int32)cb_Course.SelectedValue; var thesisTitle = new ResearchBook() { Title = tb_Title.Text, Remarks = tb_Remarks.Text, PublishedYear = cb_PublishedYear.SelectedItem.ToString(), CourseNameRb = cb_Course.GetItemText(cb_Course.SelectedItem), NumberOfCopies = Int32.Parse(tb_NumberOfCopies.Text), Cataloging = tb_Cataloging.Text, ApprovalSheetEcopy = appSheEcopy, AbstractEcopy = abstractEcopy, Classification = cb_Classfication.GetItemText(cb_Classfication.SelectedItem), }; ctx.ResearchBooks.Add(thesisTitle); ctx.SaveChanges(); var author = new Author(); //author.AuthorName = tb_AuthorFName.Text; ////AuthorMName = tb_AuthorMName.Text, ////AuthorLName = tb_AuthorLName.Text, //author.AuthorContactNumber = tb_AuthorContactNumber.Text; //author.Gender = cb_Gender.SelectedItem.ToString(); //author.ThesisTitleId = thesisTitle.ThesisTitleId; //System.Text.StringBuilder sb = new System.Text.StringBuilder(); //foreach (object item in listBox1.Items) //{ // author.AuthorName = (item.ToString()); // author.ThesisTitleId = thesisTitle.ThesisTitleId; // ctx.Authors.Add(author); //} for (int row = 0; row <= dataGridView1.Rows.Count; row++) { author.AuthorName = dataGridView1.Rows[row].Cells["Column1"].Value.ToString(); author.AuthorContactNumber = dataGridView1.Rows[row].Cells["Column2"].Value.ToString(); author.Gender = dataGridView1.Rows[row].Cells["Column3"].Value.ToString(); author.ThesisTitleId = thesisTitle.ThesisTitleId; ctx.Authors.Add(author); ctx.SaveChanges(); } LoadThesis(); MessageBox.Show("Save Successful"); ClearTextField(); //DialogResult result = MessageBox.Show("Add more author?", " ", MessageBoxButtons.YesNo); //if (result == DialogResult.Yes) //{ // tb_Title.Enabled = false; // tb_Remarks.Enabled = false; // cb_Course.Enabled = false; // cb_PublishedYear.Enabled = false; //} //if (result == DialogResult.No) //{ //} } } } catch (Exception) { //MessageBox.Show("Select Publish Year and Course"); LoadThesis(); MessageBox.Show("Save Successful"); ClearTextField(); } } else { MessageBox.Show("Please filled up all the required fields:"); } }