コード例 #1
0
        private void pbxBack_Click(object sender, EventArgs e)
        {
            UserAllForm useAll = new UserAllForm(aux);

            useAll.Show();
            this.Close();
        }
コード例 #2
0
        private void pbxSave_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnect = new SqlConnection(connectionString);

            //Save
            if (string.IsNullOrEmpty(lblId.Text))
            {
                try
                {
                    GetData();

                    if (password == confPassword)
                    {
                        UserProfile up = (UserProfile)cmbProfile.SelectedItem;
                        User        u  = new User(name, password, email, up, active);

                        sqlConnect.Open();
                        string sql = "INSERT INTO [USER](NAME, PASSWORD, EMAIL, ACTIVE, FK_USERPROFILE) VALUES (@name, @password,@email, @active, @userprofile)";

                        SqlCommand cmd = new SqlCommand(sql, sqlConnect);

                        cmd.Parameters.Add(new SqlParameter("@name", u.Name));
                        cmd.Parameters.Add(new SqlParameter("@password", UserHelper.Hash(u.Password)));
                        cmd.Parameters.Add(new SqlParameter("@email", u.Email));
                        cmd.Parameters.Add(new SqlParameter("@active", u.Active));
                        cmd.Parameters.Add(new SqlParameter("@userprofile", u.Userprofile.Id));
                        cmd.ExecuteNonQuery();

                        MessageBox.Show("Adicionado com sucesso!");
                        Log.SaveLog(sqlConnect, "Usuário inserido", DateTime.Now, "Inserção");
                        CleanData();
                    }
                    else
                    {
                        MessageBox.Show("Confirmação de senha incorreta!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Erro ao adicionar usuário!" + ex.Message);
                    CleanData();
                }
                finally
                {
                    sqlConnect.Close();
                }
            }
            //Edit
            else
            {
                try
                {
                    GetData();

                    UserProfile up = (UserProfile)cmbProfile.SelectedItem;

                    sqlConnect.Open();
                    string sql = "UPDATE [USER](NAME, PASSWORD, EMAIL, ACTIVE, FK_USERPROFILE) VALUES (@name, @password,@email, @active, @userprofile) WHERE ID = @id";

                    SqlCommand cmd = new SqlCommand(sql, sqlConnect);

                    cmd.Parameters.Add(new SqlParameter("@name", name));
                    cmd.Parameters.Add(new SqlParameter("@password", UserHelper.Hash(password)));
                    cmd.Parameters.Add(new SqlParameter("@email", email));
                    cmd.Parameters.Add(new SqlParameter("@active", active));
                    cmd.Parameters.Add(new SqlParameter("@userprofile", up.Id));
                    cmd.Parameters.Add(new SqlParameter("@id", lblId.Text));
                    cmd.ExecuteNonQuery();

                    MessageBox.Show("Altereções salvas com sucesso!");
                    Log.SaveLog(sqlConnect, "Usuário editado", DateTime.Now, "Edição");
                }
                catch (Exception Ex)
                {
                    MessageBox.Show("Erro ao editar este usuário!" + "\n\n" + Ex.Message);
                    throw;
                }
                finally
                {
                    sqlConnect.Close();

                    UserAllForm useAll = new UserAllForm(aux);
                    useAll.Show();
                    this.Close();
                }
            }
        }