Ejemplo n.º 1
0
        private void btn_salvar_Click(object sender, EventArgs e)
        {
            if (txt_username.Text == "" || txt_senha.Text == "")
            {
                MessageBox.Show("Digite o nome de usuário e a senha");
                return;
            }
            if (txt_senha.Text != txt_senha2.Text)
            {
                MessageBox.Show("As senhas não coincidem!");
                return;
            }

            string    queryVerificar = String.Format(@"
                            SELECT 
                                T_USERNAME,
                                N_IDFUNCIONARIO
                            FROM 
                                tb_usuarios
            ");
            DataTable dt             = new DataTable();

            dt = Banco.dql(queryVerificar);
            string username      = "";
            int    idFuncionario = 0;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                username      = dt.Rows[i].Field <string>("T_USERNAME");
                idFuncionario = int.Parse(dt.Rows[i].Field <Int64>("N_IDFUNCIONARIO").ToString());
                if (username == txt_username.Text)
                {
                    MessageBox.Show("Usuário Já existe");
                    return;
                }
                if (idFuncionario == int.Parse(cb_funcionario.SelectedValue.ToString()))
                {
                    MessageBox.Show("O funcionário já possuí conta de usuário");
                    return;
                }
            }

            string queryInsert = String.Format(@"
                     INSERT INTO 
                            tb_usuarios(T_USERNAME,T_SENHA,N_NIVEL,N_IDFUNCIONARIO)
                     VALUES (
                                '{0}',
                                '{1}',
                                 {2},
                                 {3}
                            )
                
            ", txt_username.Text, txt_senha.Text, cb_nivel.SelectedItem, cb_funcionario.SelectedValue);

            Banco.dml(queryInsert);
            MessageBox.Show("Usuário Inserido!");
            cb_funcionario.Enabled = false;
            cb_nivel.Enabled       = false;
            txt_username.Enabled   = false;
            txt_senha.Enabled      = false;
            txt_senha2.Enabled     = false;
            btn_salvar.Enabled     = false;
            btn_novo.Enabled       = true;
            btn_gerir.Enabled      = true;
        }
        private void btn_salvar_Click(object sender, EventArgs e)
        {
            if (pb_funcionario.Image == Properties.Resources.Usuario)
            {
                if (MessageBox.Show("Sem imagem selecionada, deseja continuar?", "Continuar ?", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }
            }
            if (destinoCompleto != "")
            {
                if (File.Exists(imagemApagar))
                {
                    File.Delete(imagemApagar);
                }
                System.IO.File.Copy(origemCompleta, destinoCompleto, true);

                if (File.Exists(destinoCompleto))
                {
                    pb_funcionario.ImageLocation = destinoCompleto;
                }
                else
                {
                    if (MessageBox.Show("Erro ao localizar a foto", "ERRO!", MessageBoxButtons.YesNo) == DialogResult.No)
                    {
                        return;
                    }
                }
            }

            query = String.Format(@"
                    UPDATE 
                        tb_funcionarios
                    SET 
                                T_NOMEFUNCIONARIO = '{0}',
                                T_PAI = '{1}',
                                T_MAE = '{2}',
                                T_BI = '{3}',
                                T_NATURAL = '{4}',
                                T_TELEFONE = '{5}', 
                                T_EMAIL = '{6}' , 
                                T_BAIRRO = '{7}', 
                                T_MUNICIPIO = '{8}',
                                T_FOTO = '{9}'
                    WHERE
                                N_IDFUNCIONARIO = '{10}'
            ", txt_nome.Text, txt_pai.Text, txt_mae.Text, txt_bi.Text, txt_naturalidade.Text, txt_telefone.Text, txt_email.Text, txt_bairro.Text, txt_municipio.Text, destinoCompleto, txt_id.Text);
            Banco.dml(query);
            MessageBox.Show("Dados Actualizados!");
            query = String.Format(@"
                        SELECT 
                                N_IDFUNCIONARIO as 'Id',
                                T_NOMEFUNCIONARIO as 'Nome',
                                T_TELEFONE as 'Telefone', 
                                T_EMAIL as 'Email', 
                                T_BAIRRO as 'Bairro', 
                                T_MUNICIPIO as 'Municipio' 
                        FROM
                                tb_funcionarios
            ");
            dgv_funcionarios.DataSource = Banco.dql(query);
        }