Fill Labels with values from a Mysql Database table

Asked

Viewed 22 times

-1

C# and Mysql

I’m trying to get the table values Employees of the current user logged into the program and displayed in the Windows of my form, however the values are not being returned, my guess is that the function dataReader.Read() is not returning anything.

My code:

private void FormUsuario_Load(object sender, EventArgs e)
        {
            LimparCampos();
            PreencherCampos();
        }

        private void PreencherCampos()
        {
            Form1 f = new Form1();
            Conexao objCon = new Conexao();
            string query = "select * from funcionarios WHERE Nome = '" + f.username + "' AND Senha = '" + f.password + "'";
            MySqlCommand cmd = new MySqlCommand(query, objCon.Conectar());
            try
            {
                objCon.Open();
                MySqlDataReader dataReader;
                dataReader = cmd.ExecuteReader();
                while (dataReader.Read()) 
                {
                    lbl_nome.Text += dataReader.GetString("Nome");
                    lbl_senha.Text += dataReader.GetInt32("Senha");
                    lbl_cargo.Text += dataReader.GetString("Cargo");
                    lbl_cpf.Text += dataReader.GetString("CPF");
                    lbl_telefone.Text += dataReader.GetString("Telefone");
                    lbl_endereco.Text += dataReader.GetString("Endereco");
                    MessageBox.Show("Dados atualizados com sucesso");
                }
                
            }
            catch (MySqlException er)
            {
                MessageBox.Show("Erro de conexão: " + er);
            }
            
        }

        private void LimparCampos()
        {
            lbl_nome.Text = string.Empty;
            lbl_senha.Text = string.Empty;
            lbl_cargo.Text = string.Empty;
            lbl_cpf.Text = string.Empty;
            lbl_telefone.Text = string.Empty;
            lbl_endereco.Text = string.Empty;
        }

1 answer

0

Use debug and put breakpoints in your variables to see if first select is being filled in correctly

Browser other questions tagged

You are not signed in. Login or sign up in order to post.