How can I inherit a variable that gets data with a SQLServer query?

0

I would like to know how to inherit a variable that contains the ID of a user obtained with the login to make an insertion to the BD with the id of the user that is logged in.

I have tried it in many ways but I have not solved my inheritance problem.

FATHER CLASS:

public class USUARIOS
{
    private string u_;
    private string p_;
    private int id_;

    //public string u { get; set; }
    //public string p { get; set; }
    //public int id { get; set; }

    public string u
    {
        get { return u_; }
        set { u_ = value; }
    }
    public string p
    {
        get { return p_; }
        set { p_ = value; }
    }
    public int id
    {
        get { return id_; }
        set { id_ = value; }
    }
    public USUARIOS()
    {

    }

    public USUARIOS(string no_usu, string pass,ref int i)
    {
        this.u = no_usu;
        this.p = pass;
        this.id = i;
    }

    public void log(string puesto)
    {
        if(puesto=="empleado"){
            MessageBox.Show("Hola Empleado "+u+" "+id);
            SistemaEmpleado pass = new SistemaEmpleado();
            pass.Show();
        }
        else if(puesto=="admin")
        {
            MessageBox.Show("Hola Admin "+u+" "+id);
            SistemaAdmin pass = new SistemaAdmin();
            pass.Show();
        }
        else 
        {
            MessageBox.Show("No se puede acceder");
        }
    }

SON CLASS

public class Empleado : USUARIOS
{
    Conexion x = new Conexion();

    //int i;
    //string name,pass;

    //private string nu;
    //private string ps;
    //private int idd;

    string nu { get; set; }
    string ps { get; set; }
    int idd { get; set; }

    public Empleado(string n, string p,ref int id)
        :base(n,p,ref id)
    {
        //this.nu = n;
        //this.ps = p;
        //this.idd = id;
    }

    public Empleado()
    {

    }
    public Empleado(string u, string p, int id)
    {
        this.nu = u;
        this.ps = p;
        this.idd = id;
    }
    //-------------------------------------------------------------------------------------------------------------------------------------------
    public void add_tck(int id, string equipo, string falla)
    {
        string cadena = "Insert into tickets values ('" + id + "','" + equipo + "','" + falla + "')";
        x.addbd(cadena);
    }
}

BUTTON TO MAKE INSERTION:

    private void iTalk_Button_21_Click(object sender, EventArgs e)
    {
        //string n = "", p="";
        //int i = 0;

        Empleado x = new Empleado();
        USUARIOS u = new USUARIOS();

            DialogResult result = MessageBox.Show("              Desea registrar el reporte?", "Confirmacion", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {

            int id = u.id;

            //string equipo = iTalk_ComboBox1.Text;
            //string falla = iTalk_RichTextBox1.Text;
           //int i = u.id;
            x.add_tck(id,iTalk_ComboBox1.Text, iTalk_RichTextBox1.Text);

            }
            else if (result == DialogResult.No)
            {
                //MessageBox.Show("Bye");
                //Close();
            }
    }

CONNECTION CLASS:

public class Conexion
{

    //public string ServidorSQL { set; get; }
    //public string BD { set; get; }
    //-------------------------------------------------------------------------------------------------------------------------------------------
    public void datos(ref int id, ref string puesto, ref string nombre, ref string pass)
    {
        string cadena = "select id_u, puesto, nombre, pass from usuarios where nombre='" + nombre + "' and pass='" + pass + "'";

        SqlConnection carretera = new SqlConnection();
        carretera.ConnectionString = @"Data Source=ZBASS-PC\ZBASS;Initial Catalog=sys_tck;Integrated Security=True";
        //carretera.ConnectionString = "data source=" + ServidorSQL + "; initial catalog=" + BD + "; integrated security=true";
        SqlCommand P = new SqlCommand();
        SqlDataReader contenedor1;
        try
        {
            carretera.Open();
            P.Connection = carretera;
            P.CommandText = cadena;
            contenedor1 = P.ExecuteReader();
            contenedor1.Read();
            id = contenedor1.GetInt32(0);
            puesto = contenedor1.GetString(1);
            nombre = contenedor1.GetString(2);
            pass = contenedor1.GetString(3);
            carretera.Close();
        }
        catch (Exception t)
        {
            carretera = null;
            MessageBox.Show("Error :" + t.Message);
        }
    }
    //-------------------------------------------------------------------------------------------------------------------------------------------
    public void addbd(string cadena)
    {
        SqlConnection Servicios = new SqlConnection(@"Data Source=PCAllINONE;Initial Catalog=sys_tck;Integrated Security=True");
        SqlCommand DB = new SqlCommand();
        try
        {
            Servicios.Open();
            DB.CommandText = cadena;
            DB.Connection = Servicios;
            DB.ExecuteNonQuery();
            MessageBox.Show("Exito, su reporte sera revisado por un administrador.");
            Servicios.Close();
        }
        catch (Exception t)
        {
            Servicios = null;
            MessageBox.Show("Error: " + t.Message);
        }
    }
    
asked by Adrian Rojas 19.10.2018 в 18:03
source

1 answer

0

The first thing I notice is that you create different instances of user and employee when you do the new with which the inheritance does not apply, it is assumed that the instance must be the same, or you are misunderstanding the inheritance cocept.

With the inheritance you can do something like this

Usuario x = new Emplado();

or assign a type in one variable to another.

If you define the insert operation next to the entity, you should not assign parameters in the method add_tck()

you could define

public void add_tck(string equipo, string falla)
{
    string cadena = "Insert into tickets values ('" + this.id + "','" + equipo + "','" + falla + "')";
    x.addbd(cadena);
}

You're supposed to use

Usuario x = new Emplado();
x.log();

x.add_tck(iTalk_ComboBox1.Text, iTalk_RichTextBox1.Text);

What I do not observe is where you assign the id when authenticating in log()

    
answered by 19.10.2018 в 19:04