Use more than one constructor in a class?

1

Greetings. I have a class called employee. This class has the 4 basic objectives of a CRUD. I created a constructor that had the basic parameters of the table but not the primary key because it is identity and autoincrements.

It happens that now I need the primary key to delete a value from the table, but when I set the method to create, it asks me to enter the employeeID that is my PK.

     {
class Empleados
{
    SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conexion"].ConnectionString);

    public string Nombre { get; set; }
    public string Apellido { get; set; }
    public string Cedula { get; set; }
    public string Direccion { get; set; }
    public int Empleado_ID { get; set; }

    public Empleados()
    {

    }

    public Empleados(string nombre, string apellido, string cedula, string direccion, int empleadoid)
    {
        this.Nombre = nombre;
        this.Apellido = apellido;
        this.Cedula = cedula;
        this.Direccion = direccion;
        this.Empleado_ID = empleadoid;
    }


    public bool registrar()
    {
        string query = "INSERT INTO EMPLEADOS (NOMBRE, APELLIDO, CEDULA, DIRECCION)";
        query += " VALUES (@NOMBRE, @APELLIDO, @CEDULA, @DIRECCION)";

        try
        {
            cn.Open();
            SqlCommand myCommand = new SqlCommand(query, cn);
            myCommand.Parameters.AddWithValue("@NOMBRE", Nombre);
            myCommand.Parameters.AddWithValue("@APELLIDO", Apellido);
            myCommand.Parameters.AddWithValue("@DIRECCION", Direccion);
            myCommand.Parameters.AddWithValue("@CEDULA", Cedula);
            myCommand.ExecuteNonQuery();
            cn.Close();
            MessageBox.Show("Datos insertados correctamente");
        }

        catch (SqlException p)
        {

            MessageBox.Show(p.Message);
        }
        return true;
    }

    public bool Actualizar()
    {

        string query = ("UPDATE Empleados set [email protected], [email protected], [email protected], [email protected] WHERE [email protected]");

        try
        {
            cn.Open();
            SqlCommand myCommand = new SqlCommand(query, cn);
            myCommand.Parameters.AddWithValue("@NOMBRE", Nombre);
            myCommand.Parameters.AddWithValue("@APELLIDO", Apellido);
            myCommand.Parameters.AddWithValue("@DIRECCION", Direccion);
            myCommand.Parameters.AddWithValue("@CEDULA", Cedula);
            myCommand.Parameters.AddWithValue("@EMPLEADOID", Empleado_ID);
            myCommand.ExecuteNonQuery();
            cn.Close();
            MessageBox.Show("Datos actualizados correctamente");
        }

        catch (SqlException p)
        {

            MessageBox.Show(p.Message);
        }
        return true;
    }

}

    }

This is my view of Create, so you have an idea of how everything is going.

      private void btnguardar_Click(object sender, EventArgs e)
    {

        var Empleados = new Clases.Empleados(txtnombre.Text, txtapellido.Text, txtcedula.Text, txtdireccion.Text);
        Empleados.registrar();
    }
    
asked by Maicol Lenin 11.09.2017 в 23:14
source

3 answers

1

The constructor of your class receives the id by parameters, but if you are not going to send it and do not use it in the rest of the code, simply delete it from the constructor. You can create another constructor that is equal to the one you have but without receiving the id, but I do not see it as necessary. Replace this:

 public Empleados(string nombre, string apellido, string cedula, string direccion, int empleadoid)
{
    this.Nombre = nombre;
    this.Apellido = apellido;
    this.Cedula = cedula;
    this.Direccion = direccion;
    this.Empleado_ID = empleadoid;
}

for this:

 public Empleados(string nombre, string apellido, string cedula, string direccion)
{
    this.Nombre = nombre;
    this.Apellido = apellido;
    this.Cedula = cedula;
    this.Direccion = direccion;
}
    
answered by 11.09.2017 / 23:35
source
1

All the comments that your colleagues have given you are correct, but I would recommend that you separate the data access class from the model. That is, you would have an Employee class:

public class Empleado {
    public string Nombre { get; set; }
    public string Apellido { get; set; }
    public string Cedula { get; set; }
    public string Direccion { get; set; }
    public int Empleado_ID { get; set; }
}

and then a data access class that would be called, for example, EmployeesDAO only with the access methods and you would pass the Employee object to each one:

public class EmpleadosDAO
{
    SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conexion"].ConnectionString);

    public bool Registrar(Empleado empleado) {
    ...
    ...
    ...
    }

    public bool Actualizar(Empleado empleado) {
    ...
    ...
    ...
    }

    public bool Eliminar(int idEmpleado) {
    ...
    ...
    ...
    }
}
    
answered by 12.09.2017 в 16:18
0

Here you have an idea, to eliminate you do not have to call the class to create.

You just have to create a constructor that receives the id to be deleted,

public Empleados(int empleadoid)
    {
        this.Empleado_ID = empleadoid;
    }

and you have to have a method something like that,

public bool eliminar(int id)
    {
        string query = "DELETE FROM EMPLEADOS WHERE ID_EMPLEADO = @ID_USUARIO;

        try
        {
            cn.Open();
            SqlCommand myCommand = new SqlCommand(query, cn);
            myCommand.Parameters.AddWithValue("@ID_USUARIO", id);           
            myCommand.ExecuteNonQuery();
            cn.Close();
            MessageBox.Show("Datos insertados correctamente");
        }

        catch (SqlException p)
        {

            MessageBox.Show(p.Message);
        }
        return true;
    }
    
answered by 11.09.2017 в 23:26