Why do not you execute the validations that you assign

0
public static int ValidarUsuarios(Constructor_Login add)
    {



        int Admin = 3;
        int Gerente = 1;
        int Empleado = 2;


        int retorno = 0;


        int nevel = 0;
        MySqlCommand actusuario = new MySqlCommand(string.Format("SELECT * FROM usuarios WHERE tipoUsuarios_idtipoUsuarios='" + Admin + "' AND usuario='" + Constructor_Login.usuario + "'"), conexion.obtenerconexion());
        nevel = Convert.ToInt32(actusuario.ExecuteScalar());
        if ( nevel != 3)

        {
            Constructor_Login.nivel = "Administrador";
            MessageBox.Show("Adminnistrador"+Constructor_Login.nivel);

        }
        else
        {

            actusuario = new MySqlCommand(string.Format("SELECT * FROM usuarios WHERE tipoUsuarios_idtipoUsuarios='" + Empleado + "' AND usuario='" + Constructor_Login.usuario + "'"), conexion.obtenerconexion());
            nevel = Convert.ToInt32( actusuario.ExecuteScalar());
            if (Empleado != 2)
            {
                Constructor_Login.nivel = "Empleado";
                MessageBox.Show("Empleado" + Constructor_Login.nivel);
            }
            else
            {

                actusuario = new MySqlCommand(string.Format("SELECT * FROM usuarios WHERE tipoUsuarios_idtipoUsuarios='" + Constructor_Login.nivel + "' AND usuario='" + Gerente + "'"), conexion.obtenerconexion());
                nevel = Convert.ToInt32(actusuario.ExecuteScalar());
                if (Gerente != 1)
                {
                    Constructor_Login.nivel = "Geremte";
                    MessageBox.Show("Gerente"+Constructor_Login.nivel);
                }
                else
                {
                    MessageBox.Show("no se encontro el nivel de usuario", "Error de Sesión", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return retorno;
            }
            return retorno;
        }
        return retorno;
    }
    
asked by Josue Hernandez 19.09.2018 в 02:57
source

1 answer

-2

I detect two things:

  • You should always use parameters, do not concatenate using string.Format() , like this

    string query = "SELECT COUNT(*) FROM usuarios WHERE tipoUsuarios_idtipoUsuarios= ?tipo AND usuario= ?usuario";
    MySqlCommand actusuario = new MySqlCommand(query, conexion.obtenerconexion());
    actusuario.Parameters.AddWithValue("?tipo", Admin);
    actusuario.Parameters.AddWithValue("?usuario",  Constructor_Login.usuario);
    
    nevel = Convert.ToInt32(actusuario.ExecuteScalar());
    

using the AddWithValues() of Parameters to assign the data

  • if you use ExecuteScalar() converting to numeric you have to return a numerical value, so in the example I change it to COUNT(*)
answered by 19.09.2018 в 16:34