private void cmb_UserOptions_SelectedIndexChanged(object sender, EventArgs e) { //cmb change for delete users if (cmb_UserOptions.Text == "Delete selected users") { //asks user if they want to delete users DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete the selected user? This will remove all data associated.", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dialogResult == DialogResult.Yes) { int selectedUser = Convert.ToInt32(dgv_Users.SelectedRows[0].Cells[0].Value.ToString()); //if admin, cant delete if (selectedUser == 1) { MessageBox.Show("You cannot delete the Administrator account.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Hand); } else { //deletes user data SqlConnection sqlConnection1 = new SqlConnection(connstr); SqlCommand cmd = new SqlCommand(); Object returnValue; cmd.CommandText = "USE [dbWizard] DELETE FROM dbUserProfile WHERE dbUserID = " + selectedUser + "; USE [dbWizard] DELETE FROM dbUsers WHERE dbUserID = " + selectedUser; cmd.CommandType = CommandType.Text; cmd.Connection = sqlConnection1; sqlConnection1.Open(); returnValue = cmd.ExecuteScalar(); sqlConnection1.Close(); //checks to see if deleted user is currently logged in and removes save Home home = new Home(); if (selectedUser == home.userId) { if (File.Exists("C:\\dbWizard\\credentials.txt")) { //removes saved credentials File.Delete(@"C:\\dbWizard\\credentials.txt"); } } var select = @"USE [dbWizard] SELECT dbUserID, dbUsername, CASE WHEN dbPassword<> '' THEN '***' ELSE 'Error' END AS 'dbPassword',GRPS.dbGroupAlias,USR.dtDateCreated, CASE WHEN USR.intActive = 1 THEN 'Online' ELSE 'Offline' END AS 'Activity' FROM[dbo].[dbUsers] USR INNER JOIN[dbo].[dbUserGroups] GRPS ON USR.intSecurity = GRPS.dbGroupID"; //resets table var c = new SqlConnection(connstr); var dataAdapter = new SqlDataAdapter(select, c); var commandBuilder = new SqlCommandBuilder(dataAdapter); var ds = new DataSet(); dataAdapter.Fill(ds); dgv_Users.ReadOnly = true; dgv_Users.DataSource = ds.Tables[0]; } } else if (dialogResult == DialogResult.No) { } } //Reset passwords (via email, defaults to "letmein") if (cmb_UserOptions.Text == "Reset passwords (via email, defaults to 'letmein')") { DialogResult dialogResult = MessageBox.Show("Are you sure you want to reset the password for the selected user?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dialogResult == DialogResult.Yes) { int selectedUser = Convert.ToInt32(dgv_Users.SelectedRows[0].Cells[0].Value.ToString()); //resets password SqlConnection sqlConnection1 = new SqlConnection(connstr); SqlCommand cmd = new SqlCommand(); Object returnValue; cmd.CommandText = "USE [dbWizard] UPDATE dbUsers SET dbPassword = '******' WHERE dbUserID = " + selectedUser; cmd.CommandType = CommandType.Text; cmd.Connection = sqlConnection1; sqlConnection1.Open(); returnValue = cmd.ExecuteScalar(); sqlConnection1.Close(); //lets user know password has been reset MessageBox.Show("Password has been reset for: " + dgv_Users.SelectedRows[0].Cells[1].Value.ToString(), "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information); } } //Reset passwords (via email, defaults to "letmein") if (cmb_UserOptions.Text == "Delete Security Group") { DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete the selected security group?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dialogResult == DialogResult.Yes) { int selectedGroup = Convert.ToInt32(dgv_SecurityGroups.SelectedRows[0].Cells[0].Value.ToString()); if (selectedGroup == 1) { MessageBox.Show("Sorry, you cannot delete the original admin group.", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Stop); } else { //resets password SqlConnection sqlConnection1 = new SqlConnection(connstr); SqlCommand cmd = new SqlCommand(); Object returnValue; cmd.CommandText = "USE [dbWizard] SELECT COUNT(*) FROM dbUsers WHERE intSecurity = " + selectedGroup; cmd.CommandType = CommandType.Text; cmd.Connection = sqlConnection1; sqlConnection1.Open(); returnValue = cmd.ExecuteScalar(); sqlConnection1.Close(); if (Convert.ToInt32(returnValue.ToString()) > 0) { MessageBox.Show("You cannot delete a group with users already within it, there are " + returnValue.ToString() + " users within this group.", "Stop!", MessageBoxButtons.OK, MessageBoxIcon.Stop); } else { cmd.CommandText = "USE [dbWizard] DELETE FROM dbUserGroups WHERE dbGroupID = " + selectedGroup; cmd.CommandType = CommandType.Text; cmd.Connection = sqlConnection1; sqlConnection1.Open(); returnValue = cmd.ExecuteScalar(); sqlConnection1.Close(); //lets user know password has been reset MessageBox.Show("Group: " + dgv_Users.SelectedRows[0].Cells[1].Value.ToString() + " has been deleted.", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information); //sets user groups table var select = @"USE [dbWizard] SELECT dbGroupId, dbGroupAlias AS 'Group Name', dbGroupRights AS 'Rights (none/read/write/all)',dtDateCreated AS 'Date Created' FROM[dbo].[dbUserGroups]"; var c = new SqlConnection(connstr); var dataAdapter = new SqlDataAdapter(select, c); var commandBuilder = new SqlCommandBuilder(dataAdapter); var ds = new DataSet(); dataAdapter.Fill(ds); dgv_SecurityGroups.ReadOnly = true; dgv_SecurityGroups.DataSource = ds.Tables[0]; } } } } }