Beispiel #1
0
        private void DeleteUserButton_Click(object sender, EventArgs e)
        {
            SqlDataReader dataReaderbr;
            SqlConnection sqlConnection1 = new SqlConnection(connectionString);
            SqlCommand    cmd            = new SqlCommand();

            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from Users where UserName = \'" + DeleteUsername.Text.Trim() + "\' and Name = \'" + DeleteName.Text.Trim() + "\' and Password = \'" + DeletePassword.Text + "\';";
            cmd.Connection  = sqlConnection1;


            if (!DeleteName.Text.Trim().Equals("") && !DeletePassword.Equals("") && !DeleteUsername.Text.Trim().Equals(""))
            {
                sqlConnection1.Open();
                try
                {
                    dataReaderbr = cmd.ExecuteReader();
                    if (dataReaderbr.HasRows)
                    {
                        if (!DeleteUsername.Text.Trim().Equals(LoginForm.username_data))
                        {
                            SqlConnection sqlConnection2 = new SqlConnection(connectionString);
                            SqlCommand    cmd1           = new SqlCommand();
                            cmd1.CommandType = CommandType.Text;
                            cmd1.CommandText = "delete from dbo.Users where UserName=\'" + DeleteUsername.Text.Trim() + "\';";
                            cmd1.Connection  = sqlConnection2;
                            try
                            {
                                sqlConnection2.Open();
                                cmd1.ExecuteNonQuery();
                                DeleteUsername.Text = "";
                                DeletePassword.Text = "";
                                DeleteName.Text     = "";
                                MessageBox.Show("User Successfully Deleted.");
                                sqlConnection2.Close();
                            }
                            catch (Exception ex)
                            {
                                sqlConnection2.Close();
                                MessageBox.Show("Unable to delete user.");
                            }
                        }
                        else
                        {
                            MessageBox.Show("You cannot delete your own account.");
                        }
                    }
                    else
                    {
                        MessageBox.Show("The Credentials don't match any user\'s credentials.");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("The Credentials don't match any user\'s credentials.");
                }
                sqlConnection1.Close();
            }
            else
            {
                MessageBox.Show("Please fill in all the fields.");
            }
        }