private void DisplayDataButton_Click(object sender, System.EventArgs e) { Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); var people = databaseContext.People .OrderBy(current => current.Age) .ToList() ; peopleDataGridView.DataSource = people; } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } finally { if (databaseContext != null) { databaseContext.Dispose(); //databaseContext = null; } } }
/// <summary> /// second Overloading Of SearchContact Method With any Parameter /// </summary> /// <param name="FetchReason"></param> public void SearchContact(string FetchReason) { Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); System.Collections.Generic.List <Models.Contact> contacts; switch (FetchReason) { case "firstname": if (firstNameTextBox.Text == string.Empty) { SearchContact(); return; } contacts = databaseContext.Contacts .Where(Current => Current.FirstName.Contains(firstNameTextBox.Text.ToString())) .OrderBy(current => current.FirstName) .ToList(); dataGridView.DataSource = contacts; break; case "lastname": if (lastNameTextBox.Text == string.Empty) { SearchContact(); return; } contacts = databaseContext.Contacts .Where(Current => Current.LastName.Contains(lastNameTextBox.Text.ToString())) .OrderBy(current => current.LastName) .ToList(); dataGridView.DataSource = contacts; break; default: System.Windows.Forms.MessageBox.Show("Some Error Accured"); break; } } catch (System.Exception) { System.Windows.Forms.MessageBox.Show("Some Error Accured"); } finally { if (databaseContext != null) { databaseContext.Dispose(); databaseContext = null; } } }
private void ChangePasswordForm_Load(object sender, EventArgs e) { Models.DatabaseContext oDataBaseContext = null; try { oDataBaseContext = new Models.DatabaseContext(); Models.User oUser = oDataBaseContext.Users .Where(current => current.Id == Infrastructure.Utility.AuthenticatedUser.Id) .FirstOrDefault(); if (oUser == null) { Application.Exit(); } } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); } finally { if (oDataBaseContext != null) { oDataBaseContext.Dispose(); oDataBaseContext = null; } } }
private void MainForm_Load(object sender, System.EventArgs e) { Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); var people = databaseContext.People .ToList() ; peopleListBox.ValueMember = nameof(Models.Person.Id); peopleListBox.DisplayMember = nameof(Models.Person.DisplayFullName); peopleListBox.DataSource = people; } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } finally { if (databaseContext != null) { databaseContext.Dispose(); //databaseContext = null; } } }
private void SaveButton_Click(object sender, System.EventArgs e) { Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); Models.User currentUser = databaseContext.Users.Where(current => current.Id == Infrastructure.Utility.AuthenticatedUser.Id).FirstOrDefault(); if (currentUser == null) { System.Windows.Forms.Application.Exit(); } currentUser.FullName = fullNameLable.Text; currentUser.Description = descriptionTextBox.Text; databaseContext.SaveChanges(); Infrastructure.Utility.AuthenticatedUser = currentUser; ((MainForm)this.MdiParent).UpdateWelcomeToolstripStatusLabel(); System.Windows.Forms.MessageBox.Show("your profile is updated successfully...."); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show("Eroor:" + ex.Message); } finally { if (databaseContext != null) { databaseContext.Dispose(); databaseContext = null; } } }
public void SearchContact() { Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); System.Collections.Generic.List <Models.Contact> contacts; contacts = databaseContext.Contacts.ToList(); gridControl.DataSource = contacts; } catch (System.Exception ex) { DevExpress.XtraEditors.XtraMessageBox.Show(ex.ToString()); } finally { if (databaseContext != null) { databaseContext.Dispose(); databaseContext = null; } } }
private void Form1_Load(object sender, EventArgs e) { Models.DatabaseContext oDatabaseContext = null; try { oDatabaseContext = new Models.DatabaseContext(); Models.Person oPeson = new Models.Person(); oPeson.Id = 5; oPeson.Age = 32; oPeson.FullName = "hamed"; oPeson.IsSupervisor = true; oDatabaseContext.People.Add(oPeson); oDatabaseContext.SaveChanges(); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } finally { if (oDatabaseContext != null) { oDatabaseContext.Dispose(); oDatabaseContext = null; } } }
private void UpdateProfileForm_Load(object sender, System.EventArgs e) { Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); Models.User user = databaseContext.Users .Where(x => x.Id == Infrastructure.Utility.AuthenticatedUser.Id) .FirstOrDefault(); if (user == null) { System.Windows.Forms.MessageBox.Show("خطایی رخ داده است با مدیر سیستم تماس بگیرید"); //System.Windows.Forms.Application.Exit(); } fullnameTextBox.Text = user.FullName; descriptionTextBox.Text = user.Description; } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show("خطایی رخ داده است با مدیر سیستم تماس بگیرید" + Environment.NewLine + ex.Message); } finally { if (databaseContext != null) { databaseContext.Dispose(); } } }
private void MainForm_Load(object sender, System.EventArgs e) { Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); var users = databaseContext.Users .ToList() ; } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } finally { if (databaseContext != null) { databaseContext.Dispose(); //databaseContext = null; } } }
private void Solution2Button_Click(object sender, System.EventArgs e) { Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); // Without [virtual] for [States] Property Models.Country country = databaseContext.Countries .Include(current => current.States) .FirstOrDefault(); int stateCount = country.States.Count; } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } finally { if (databaseContext != null) { databaseContext.Dispose(); //databaseContext = null; } } }
private void ChangePasswordButton_Click(object sender, EventArgs e) { Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); Models.User currentUser = databaseContext.Users. Where(current => current.Id == Infrastructure.Utility.AuthenticatedUser.Id).FirstOrDefault(); if (currentUser == null) { System.Windows.Forms.Application.Exit(); } if (string.Compare(currentUser.Password, oldPasswordTextBox.Text, ignoreCase: false) != 0) { System.Windows.Forms.MessageBox.Show("your old Password is not correct"); oldPasswordTextBox.Focus(); return; } currentUser.Password = newPasswordTextBox.Text; databaseContext.SaveChanges(); System.Windows.Forms.MessageBox.Show("your password changed successfully"); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } finally { if (databaseContext != null) { databaseContext.Dispose(); databaseContext = null; } } }
private void MainForm_Load(object sender, System.EventArgs e) { Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); Models.Country newCountry = new Models.Country { Name = "Iran", }; databaseContext.Countries.Add(newCountry); databaseContext.SaveChanges(); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } finally { if (databaseContext != null) { databaseContext.Dispose(); //databaseContext = null; } } }
/// <summary> /// First Overloading Of SearchContact Method Without any Parameter /// </summary> public void SearchContact() { Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); System.Collections.Generic.List <Models.Contact> contacts; contacts = databaseContext.Contacts.ToList(); dataGridView.DataSource = contacts; } catch (System.Exception) { System.Windows.Forms.MessageBox.Show("Some Error Accured"); } finally { if (databaseContext != null) { databaseContext.Dispose(); databaseContext = null; } } }
private void ShowAllBooksButton_Click(object sender, System.EventArgs e) { try { databaseContext = new Models.DatabaseContext(); this.CurrentBooks = databaseContext.Books .ToList(); DisplayBooks(CurrentBooks); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show($"Unexpected Error:{ex.Message}", caption: "ERROR", buttons: System.Windows.Forms.MessageBoxButtons.OK, icon: System.Windows.Forms.MessageBoxIcon.Error); } finally { if (databaseContext != null) { databaseContext.Dispose(); databaseContext = null; } } }
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { Models.DatabaseContext oDatabaseContext = null; try { oDatabaseContext = new Models.DatabaseContext(); //Find Row of Selected Cell and Find Guid Of This Row (Guid Column is Number 4 Column. That is Hidden) //By these info We Can Find Record in Database And Call its Info into Our Fields //*********************************************************************************** int index = e.RowIndex; string cellIndex = dataGridView.Rows[index].Cells[4].Value.ToString(); Guid guid = new Guid(cellIndex); var contacts = oDatabaseContext.People.Where(current => current.Id == guid).FirstOrDefault(); NameTextbox.Text = contacts.Name; FamilyTextbox.Text = contacts.Family; PhoneTextbox.Text = contacts.Phone; EmailTextbox.Text = contacts.Email; } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (oDatabaseContext != null) { oDatabaseContext.Dispose(); oDatabaseContext = null; } } }
private void UpdateProfileForm_Load(object sender, System.EventArgs e) { Models.DatabaseContext oDatabaseContext = null; try { oDatabaseContext = new Models.DatabaseContext(); Models.User oUser = oDatabaseContext.Users .Where(current => current.Id == Infrastructure.Utility.AuthenticatedUser.Id) .FirstOrDefault(); if (oUser == null) { System.Windows.Forms.Application.Exit(); } fullNameTextBox.Text = oUser.FullName; descriptionTextBox.Text = oUser.Description; } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show("Error: " + ex.Message); } finally { if (oDatabaseContext != null) { oDatabaseContext.Dispose(); oDatabaseContext = null; } } }
private void MainForm_Load(object sender, System.EventArgs e) { Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); bool hasAnyUser = databaseContext .Users .Any(); if (hasAnyUser) { generateBtton.Enabled = false; } } catch (System.Exception ex) { System.Windows.Forms.MessageBox .Show($"Error + { System.Environment.NewLine } { ex.Message }"); } finally { if (databaseContext != null) { databaseContext.Dispose(); } } }
private void CreateANewCountryButton_Click(object sender, System.EventArgs e) { Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); Models.Country country = new Models.Country(); country.Name = "Iran"; databaseContext.Countries.Add(country); databaseContext.SaveChanges(); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } finally { if (databaseContext != null) { databaseContext.Dispose(); //databaseContext = null; } } }
private void displayBooks(string name) { try { databaseContext = new Models.DatabaseContext(); var books = databaseContext.Books .Where(current => current.BookName.ToLower().Contains(name.ToLower())) .ToList(); displayBookListbox.DataSource = books; displayBookListbox.DisplayMember = nameof(Models.Book.ListDisplayName); displayBookListbox.ValueMember = nameof(Models.Book.BookName); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show($"Unexpected Error:{ex.Message}", caption: "ERROR", buttons: System.Windows.Forms.MessageBoxButtons.OK, icon: System.Windows.Forms.MessageBoxIcon.Error); } finally { if (databaseContext != null) { databaseContext.Dispose(); } } }
//پیدا کردن سطر مربوط به دکتر انتخاب شده در روز مشخص شده public void CheckVisitTime() { Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); Models.Reservations foundedDoctor = databaseContext.Reservation .Where(current => string.Compare(current.doctorId, doctor, true) == 0 && string.Compare(current.DateVisit, date, true) == 0) .FirstOrDefault(); if (foundedDoctor == null) { SetFirstTime(time8TextBox, time8checkBox); SetFirstTime(time9TextBox, time9checkBox); SetFirstTime(time10TextBox, time10checkBox); SetFirstTime(time11TextBox, time11checkBox); SetFirstTime(time12TextBox, time12checkBox); SetFirstTime(time13TextBox, time13checkBox); SetFirstTime(time14TextBox, time14checkBox); SetFirstTime(time15TextBox, time15checkBox); SetFirstTime(time16TextBox, time16checkBox); SetFirstTime(time17TextBox, time17checkBox); SetFirstTime(time18TextBox, time18checkBox); SetFirstTime(time19TextBox, time19checkBox); SetFirstTime(time20TextBox, time20checkBox); SetFirstTime(time21TextBox, time21checkBox); VisitDateDoctor.DateVisit = date; return; } VisitDateDoctor = foundedDoctor; VisitDateDoctor.DateVisit = date; CheckTime(foundedDoctor); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); return; } finally { if (databaseContext != null) { databaseContext.Dispose(); databaseContext = null; } } }
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (databaseContext != null) { databaseContext.Dispose(); databaseContext = null; } }
private void UserListForm_FormClosed(object sender, FormClosedEventArgs e) { if (_myDatabaseContext != null) { _myDatabaseContext.Dispose(); _myDatabaseContext = null; } }
private void CreateCountryStatesButton_Click(object sender, System.EventArgs e) { Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); Models.Country country = databaseContext.Countries .FirstOrDefault(); // Solution (1) Models.State state1 = new Models.State(); state1.Name = "State (1)"; state1.CountryId = country.Id; databaseContext.States.Add(state1); // /Solution (1) // Solution (2) Models.State state2 = new Models.State(); state2.Name = "State (2)"; state2.Country = country; databaseContext.States.Add(state2); // /Solution (2) // Solution (3) Models.State state3 = new Models.State(); state3.Name = "State (3)"; country.States.Add(state3); // /Solution (3) databaseContext.SaveChanges(); System.Guid id1 = state1.CountryId; System.Guid id2 = state2.CountryId; System.Guid id3 = state3.CountryId; } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } finally { if (databaseContext != null) { databaseContext.Dispose(); //databaseContext = null; } } }
private void DeleteUserButton_Click(object sender, System.EventArgs e) { if (usersListBox.SelectedItems.Count == 0) { System.Windows.Forms.MessageBox.Show("You did not select any users for deleting!"); return; } Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); foreach (var selectedItem in usersListBox.SelectedItems) { Models.User selectedUser = selectedItem as Models.User; if (selectedUser != null) { Models.User foundedUser = databaseContext.Users .Where(current => current.Id == selectedUser.Id) .FirstOrDefault(); if (foundedUser != null) { if (foundedUser.IsAdmin == false) { if (string.Compare(foundedUser.Username, Infrastructure.Utility.AuthenticatedUser.Username, ignoreCase: true) != 0) { databaseContext.Users.Remove(foundedUser); databaseContext.SaveChanges(); } } } } } Search(); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } finally { if (databaseContext != null) { databaseContext.Dispose(); databaseContext = null; } } }
private void SaveButton_Click(object sender, System.EventArgs e) { Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); Models.User user = databaseContext.Users .Where(current => current.Id == SelectedUser.Id) .FirstOrDefault(); if (user == null) { System.Windows.Forms.MessageBox.Show("Please contact Support team", caption: "Error", buttons: System.Windows.Forms.MessageBoxButtons.OK, icon: System.Windows.Forms.MessageBoxIcon.Error ); System.Windows.Forms.Application.Exit(); } user.IsAdmin = isAdminCheckBox.Checked; user.IsActive = isActiveCheckBox.Checked; user.FullName = fullNameTextBox.Text; user.Description = descriptionTextBox.Text; databaseContext.SaveChanges(); System.Windows.Forms.MessageBox.Show("user Profile updated successfully!", caption: "", buttons: System.Windows.Forms.MessageBoxButtons.OK, icon: System.Windows.Forms.MessageBoxIcon.Asterisk ); Infrastructure.Utility.AuthenticatedUser = databaseContext.Users .Where(current => string.Compare(current.Username, Infrastructure.Utility.AuthenticatedUser.Username, true) == 0) .FirstOrDefault(); Infrastructure.Utility.MainForm.ResetForm(); Close(); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.ToString()); } finally { if (databaseContext != null) { databaseContext.Dispose(); databaseContext = null; } } }
private void RegisterForm_Load(object sender, System.EventArgs e) { int newUserNumber; #region first open if (Infrastructure.Utility.EndUserId == string.Empty || Infrastructure.Utility.EndUserId == null) { Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); System.Collections.Generic.List <Models.User> endUser = null; endUser = databaseContext.Users .OrderBy(current => current.Id) .ToList(); newUserNumber = endUser.Count(); Infrastructure.Utility.EndUserId = newUserNumber.ToString(); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); return; } finally { if (databaseContext != null) { databaseContext.Dispose(); databaseContext = null; } } } #endregion /first open #region next open else { newUserNumber = System.Convert.ToInt32(Infrastructure.Utility.EndUserId); } #endregion /next open newUserNumber++; userCodeTextBox.Text = newUserNumber.ToString(); TextBoxBackColor = usernameTextBox.BackColor; }
private void CreateButton_Click(object sender, System.EventArgs e) { if (string.IsNullOrWhiteSpace(titleTextBox.Text)) { System.Windows.Forms.MessageBox.Show("Title is required!"); return; } if (string.IsNullOrWhiteSpace(authorTextBox.Text)) { System.Windows.Forms.MessageBox.Show("Author is required!"); return; } if (string.IsNullOrWhiteSpace(publishYearTextBox.Text)) { System.Windows.Forms.MessageBox.Show("Publish year is required!"); return; } Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); Models.Resource resource = new Models.Resource { Title = titleTextBox.Text, Author = authorTextBox.Text, Translator = translatorTextBox.Text, Description = descriptionTextBox.Text, Type = (Models.Enums.ResourceType)typeComboBox.SelectedValue, PublishYear = System.Convert.ToInt32(publishYearTextBox.Text), }; databaseContext.Resources.Add(resource); databaseContext.SaveChanges(); System.Windows.Forms.MessageBox .Show("Resource created successfully..."); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show($"Error: { ex.Message }"); } finally { if (databaseContext != null) { databaseContext.Dispose(); //databaseContext = null; } } }
private void Search() { Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); // ************************************************** fullNameTextBox.Text = fullNameTextBox.Text.Trim(); while (fullNameTextBox.Text.Contains(" ")) { fullNameTextBox.Text = fullNameTextBox.Text.Replace(" ", " "); } // ************************************************** System.Collections.Generic.List <Models.User> users = null; if (fullNameTextBox.Text == string.Empty) { users = databaseContext.Users .OrderBy(current => current.FullName) .ToList() ; } else { users = databaseContext.Users .Where(current => current.FullName.Contains(fullNameTextBox.Text)) .OrderBy(current => current.FullName) .ToList() ; } // ************************************************** myDataGridView.DataSource = users; // ************************************************** } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show($"Error: { ex.Message }"); } finally { if (databaseContext != null) { databaseContext.Dispose(); databaseContext = null; } } }
private void MainForm_FormClosing (object sender, System.Windows.Forms.FormClosingEventArgs e) { if (_myDatabaseContext != null) { _myDatabaseContext.Dispose(); _myDatabaseContext = null; } }
private void SaveButton_Click(object sender, System.EventArgs e) { Models.DatabaseContext databaseContext = null; try { databaseContext = new Models.DatabaseContext(); var currentUser = databaseContext.Users .Where(current => current.Id == Infrastructure.Utility.AuthenticatedUser.Id) .FirstOrDefault(); // ************************************************** if (currentUser == null) { System.Windows.Forms.Application.Exit(); } if (currentUser.IsActive == false) { System.Windows.Forms.Application.Exit(); } // ************************************************** currentUser.FullName = fullNameTextBox.Text; currentUser.Description = descriptionTextBox.Text; databaseContext.SaveChanges(); // ************************************************** Infrastructure.Utility.AuthenticatedUser = currentUser; Infrastructure.Utility.MainForm.ResetForm(); // ************************************************** System.Windows.Forms.MessageBox .Show("Your profile updated successfully..."); // استفاده کنیم Close فرم به طور اتوماتیک بسته شود، میتوانیم از دستور MessageBox اگر بخواهیم بعد از UpdateProfileForm در داخل //Close(); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show($"Error: { ex.Message }"); } finally { if (databaseContext != null) { databaseContext.Dispose(); databaseContext = null; } } }