Error with login screen in C #

0

I'm trying to make a login screen with the following attributes

  • If Login and password are the same as the typed data
    • enter another if
      • If the ADM (database) attribute is equal to "s" you should open the Main menu if not the meniPrincipalII.

Form Code:

private void btnLogin_Click(object sender, EventArgs e)
{
    claUsuario objUsuario = new claUsuario();

    try
    {

        objUsuario.Login = txtUsuario.Text;
        objUsuario.Senha = txtSenha.Text;

        DataTable tbldados = objUsuario.LOGAR();

        var login = tbldados.Rows[0]["Login"].ToString();
        var senha = tbldados.Rows[0]["Senha"].ToString();


        if (objUsuario.Login == login && objUsuario.Senha == senha)
        {
            this.Visible = false;

            DataTable tbladm = objUsuario.Adiministrador();
            var adm = tbladm.Rows[0]["adm"].ToString();
            objUsuario.Adm = "s";

            if (objUsuario.Adm == adm)
            {
                menuPrincipal janela = new menuPrincipal();

                janela.Show();
            }
            else
            {
                menuPrincipalII janela = new menuPrincipalII();

                janela.Show();
            }
        }
    }
    catch (IndexOutOfRangeException)
    {
        MessageBox.Show("Usuário ou senha invalida!");
    }
    catch (FormatException)
    {
        MessageBox.Show("É necessário informar o código de usuario");
    }

}

Class Code:

public DataTable LOGAR()
{
    DataTable tblresultado = objDados.ConvertSqlToDataTable("SELECT * FROM tblogin WHERE login=@login and senha=@Senha",
        new MySqlParameter("@login", Login),
        new MySqlParameter("@Senha", Senha));

    return tblresultado;
}

public DataTable Adiministrador()
{
    DataTable tblresultado = objDados.ConvertSqlToDataTable("SELECT * FROM tblogin WHERE adm=@Adm",
        new MySqlParameter("@Adm", Adm));

    return tblresultado;
}

Database code:

 create table tblogin(

 cod_user int auto_increment not null,

 nome varchar(100) not null,
 login varchar (50) not null,
 senha varchar (30) not null,
 adm varchar (1) not null,
 constraint pk_cod_user primary key (cod_user)
 );

Thank you very much

    
asked by anonymous 02.05.2018 / 22:35

1 answer

0

You can get the value of the property .adm , which is in the first table.

Try this:

private void btnLogin_Click(object sender, EventArgs e)
{  
    try
    {
        claUsuario objUsuario = new claUsuario();
        objUsuario.Login = txtUsuario.Text;
        objUsuario.Senha = txtSenha.Text;

        DataTable tbldados = objUsuario.LOGAR();

        var login = tbldados.Rows[0]["Login"].ToString();
        var senha = tbldados.Rows[0]["Senha"].ToString();
        var adm = tbldados.Rows[0]["adm"].ToString();


        if (objUsuario.Logi.Equals(login) && objUsuario.Senha.Equals(senha)
        {
            this.Visible = false;

            if (objUsuario.Adm.Equals(adm))
            {
                menuPrincipal janela = new menuPrincipal();
                janela.Show();
            }
            else
            {
                menuPrincipalII janela = new menuPrincipalII();
                janela.Show();
            }
        }
    }
    catch (MysqlException erro)
    {
        MessageBox.Show($"Ocorreu um erro: {erro.message}");     

    }

}

    
03.05.2018 / 16:32