// Check if the user details are correct or not, and the update the user in the database, and update the DgvUsers, too private void BtnUpdateUser_Click(object sender, EventArgs e) { // Check for empty places if (string.IsNullOrEmpty(TxtName.Text) || string.IsNullOrEmpty(TxtSurname.Text) || string.IsNullOrEmpty(TxtUsername.Text) || string.IsNullOrEmpty(TxtPassword.Text) || (!RbtnIsBoss.Checked && !RbtnNotBoss.Checked)) { MessageBox.Show("Boş yer buraxmayın!"); return; } // Do not allow the username to be repeated if (db.Users.Any(u => u.Username == TxtUsername.Text && u.Id != clickedId)) { MessageBox.Show("İstifadəçi adı artıq mövcuddur!"); TxtUsername.ResetText(); return; } // Do not allow spaces in usernames... if (TxtUsername.Text.Contains(' ')) { MessageBox.Show("İstifadəçi adında boşluq istifadə etməyin!"); return; } User user = db.Users.Find(clickedId); user.Name = TxtName.Text; user.Surname = TxtSurname.Text; user.Username = TxtUsername.Text; user.Password = TxtPassword.Text; user.IsBoss = RbtnIsBoss.Checked ? true : false; user.Phone = TxtPhone.Text; db.SaveChanges(); if (clickedId == EnteredUser.Id) { MessageBox.Show("Yenidən giriş edin..."); foreach (Form form in Application.OpenForms) { form.Hide(); } Login login = new Login(); login.Show(); return; } // Update the data grid view DgvUsers.Rows[clickedRow].Cells[1].Value = user.Name; DgvUsers.Rows[clickedRow].Cells[2].Value = user.Surname; DgvUsers.Rows[clickedRow].Cells[3].Value = user.Username; DgvUsers.Rows[clickedRow].Cells[4].Value = user.Password; DgvUsers.Rows[clickedRow].Cells[5].Value = user.IsBoss ? "Bəli" : "Xeyr"; DgvUsers.Rows[clickedRow].Cells[6].Value = user.Phone; // Finished with Dgv MessageBox.Show("İstifadəçi yeniləndi..."); Reset(); }
// Reset Text Boxes and radio buttons. Set Id and row index 0 and -1 private void Reset() { TxtName.ResetText(); TxtSurname.ResetText(); TxtUsername.ResetText(); TxtPassword.ResetText(); TxtPhone.ResetText(); RbtnIsBoss.Checked = false; RbtnNotBoss.Checked = false; BtnDeleteUser.Visible = false; BtnUpdateUser.Visible = false; clickedId = 0; clickedRow = -1; }
// Check if the user details are correct or not, and the add the user to database, and bring it to the DgvUsers private void BtnAddUser_Click(object sender, EventArgs e) { // Check for empty places if (string.IsNullOrEmpty(TxtName.Text) || string.IsNullOrEmpty(TxtSurname.Text) || string.IsNullOrEmpty(TxtUsername.Text) || string.IsNullOrEmpty(TxtPassword.Text) || string.IsNullOrEmpty(TxtPhone.Text) || (!RbtnIsBoss.Checked && !RbtnNotBoss.Checked)) { MessageBox.Show("Boş yer buraxmayın!"); return; } // Do not allow the username to be repeated if (db.Users.Any(u => u.Username == TxtUsername.Text)) { MessageBox.Show("İstifadəçi adı artıq mövcuddur!"); TxtUsername.ResetText(); return; } // Do not allow spaces in usernames... if (TxtUsername.Text.Contains(' ')) { MessageBox.Show("İstifadəçi adında boşluq istifadə etməyin!"); return; } User user = new User { Name = TxtName.Text, Surname = TxtSurname.Text, Username = TxtUsername.Text, Password = TxtPassword.Text, IsBoss = RbtnIsBoss.Checked ? true : false, Phone = TxtPhone.Text }; db.Users.Add(user); db.SaveChanges(); // Add new user to DgvUsers DgvUsers.Rows.Add(user.Id, user.Name, user.Surname, user.Username, user.Password, user.IsBoss ? "Bəli" : "Xeyr", user.Phone); MessageBox.Show("İstifadəçi əlavə olundu!"); Reset(); }
private void ResetTextboxes() { TxtUsername.ResetText(); TxtPassword.ResetText(); }