validate login (login) c #

2

I have a login form (user, password, permission) that has a combobox, which allows you to choose what type of user to start the session, depending on the type of user selected, validates the data from 2 different tables, but it gives me error:

the final code of the enter button is as follows:

        private void btnIngresar_Click(object sender, EventArgs e)
    {

        //RECIBMOS EL VALOR QUE VIENE DESDE EL COMBOBOX
        if (permisoCombo == "Funcionario")
        {
            DataTable datos = CapaNegocios.negocioLoginProf.ingresar(txtUsuario.Text, txtClave.Text);

            //verificamos la existencia del usuario
            if (datos.Rows.Count == 0)
            {
                MessageBox.Show("NO EXISTE NINGUN USUARIO CON ESOS DATOS", "Centro Medico Chilhue", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                frmPrincipal frm = new frmPrincipal();
                frm.idRegistro = datos.Rows[0][0].ToString();
                frm.rut = datos.Rows[0][1].ToString();
                frm.nombre = datos.Rows[0][2].ToString();
                frm.apePat = datos.Rows[0][3].ToString();
                frm.usuario = datos.Rows[0][5].ToString();
                frm.permiso = datos.Rows[0][7].ToString();

                frm.Show();
                this.Hide();
            }
        }else if(permisoCombo == "Profesional")
        {
            DataTable datos = CapaNegocios.negocioLoginProf.ingresar(txtUsuario.Text, txtClave.Text);

            //verificamos la existencia del usuario
            if (datos.Rows.Count == 0)
            {
                MessageBox.Show("NO EXISTE NINGUN USUARIO CON ESOS DATOS", "Centro Medico Chilhue", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                frmPrincipal frm = new frmPrincipal();
                frm.idRegistro = datos.Rows[0][0].ToString();
                frm.rut = datos.Rows[0][1].ToString();
                frm.nombre = datos.Rows[0][2].ToString();
                frm.apePat = datos.Rows[0][3].ToString();
                frm.usuario = datos.Rows[0][5].ToString();
                frm.permiso = datos.Rows[0][7].ToString();

                frm.Show();
                this.Hide();
            }

        }


    }

and in the SelectIndexChange event of the combobox (to see what type of user it was selected) I have:

            permisoCombo = Convert.ToString(this.cmbRoles.SelectedItem);

Greetings to all and thanks

    
asked by Nicolas Ezequiel Almonacid 02.08.2018 в 19:38
source

3 answers

0

Check that in the Enter method of the Business Layer you are initializing the variable to return with new DataTable () or failing in the Data Access layer, so that you have at least an empty DataTable. Since you are referring to a null object. By having your DataTable empty you can use your conditional in the way you indicate:

if (datos.Rows.Count == 0)
{

}

    
answered by 03.08.2018 / 15:10
source
1

You can call the constructor of the datatable when you declare the variable, so you avoid a nullexception. On the other hand I recommend you not validate the same data 2 or more times, that's what the switch is for. Now, you make a validation according to the permission, but do not see that it changes between one and the other (that is, they execute the same lines of code). Check this code a bit more simplified:

    private void btnIngresar_Click(object sender, EventArgs e)
    {
        DataTable datos = new DataTable();
        //RECIBMOS EL VALOR QUE VIENE DESDE EL COMBOBOX
        switch (permisoCombo)
        {
            case "Funcionario":
                datos = CapaNegocios.negocioLoginProf.ingresar(txtUsuario.Text, txtClave.Text);
                if (datos.Rows.Count == 0)
                    MessageBox.Show("NO EXISTE NINGUN USUARIO CON ESOS DATOS", "Centro Medico Chilhue", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    SetearFormulario(datos);
                break;

            case "Profesional":
                datos = CapaNegocios.negocioLoginProf.ingresar(txtUsuario.Text, txtClave.Text);
                if (datos.Rows.Count == 0)
                    MessageBox.Show("NO EXISTE NINGUN USUARIO CON ESOS DATOS", "Centro Medico Chilhue", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    SetearFormulario(datos);
                break;
        }
    }

    private void SetearFormulario(DataTable datos)
    {
        frmPrincipal frm = new frmPrincipal();
        frm.idRegistro = datos.Rows[0][0].ToString();
        frm.rut = datos.Rows[0][1].ToString();
        frm.nombre = datos.Rows[0][2].ToString();
        frm.apePat = datos.Rows[0][3].ToString();
        frm.usuario = datos.Rows[0][5].ToString();
        frm.permiso = datos.Rows[0][7].ToString();
        frm.Show();
        this.Hide();
    }

The term this is often redundant, check your code to see which ones you can delete.

I hope you serve and review the filter permissions.

    
answered by 02.08.2018 в 20:04
0

You are saying that datos is null .

You should check it out.

 //verificamos la existencia del usuario
        if (datos!= null)
        {

That is to say

CapaNegocios.negocioLoginProf.ingresar(

You are returning a null.

    
answered by 02.08.2018 в 19:41