I have a login button doing the validation of users, but I need this code to check on the table that has a "function id" like 0 and 1, where 1 = show delete button and 0 = hide delete. So, I need the login button to check the function id
and return one value for me and change the visibility of the button. Thanks!
private void btnLogin_Click(object sender, EventArgs e)
{
//chamo o método já testando seu retorno
//preenchendo os parâmetros necessários
//se retorna true
if (ValidaUsuario(txtUsuario.Text, txtSenha.Text))
{
//retorno o diálogo ok, abre o form1
this.DialogResult = DialogResult.OK;
}
else
{
//Senão retorno o dialogo cancel,message de login invalido
MessageBox.Show("login e senha invalido");
}
}
//metodo para validar com 2 string
//parametros usuario e senha
private bool ValidaUsuario(string usuario, string senha)
{
//variável que será testada para
//informar o retorno
int retorno = -1;
////instância para conectar
MySqlConnection conn = new MySqlConnection("server=localhost;user id=root;database=appouschool;Allow Zero Datetime=true");
//comando sql que dá um count
//na tabela se existirem usuario e senha
//com os dados informados
string comando = "SELECT COUNT(*) FROM funcionarios WHERE Usuario=@Usuario AND Senha=@Senha";
//instância do comando
MySqlCommand cmd = new MySqlCommand(comando, conn);
//preenchimento dos parâmetros
cmd.Parameters.AddWithValue("@Usuario", usuario);
cmd.Parameters.AddWithValue("@Senha", senha);
//abro conexão
conn.Open();
//retorno recebe o resultado do execute
retorno = Convert.ToInt32(cmd.ExecuteScalar());
//fecho conexão
conn.Close();
//retorno true se retorno for maior que zero
return retorno > 0;
}