Recover foreign key when making an insert from c # and sql

0

I have a problem when making a record of entries and exits of equipment, when doing an insert of my table Registro_Eq apparently I am not recovering correctly the data of my foreign key or maybe some step before inserting I am wrong. Someone could help me how to do it please. This is part of the code that I have.

public void registro(String codB)//Metodo Registro E/S
    {

        DateTime fh = DateTime.Now;
        string fech = fh.ToString("yyyy-MM-dd");
        string hr = fh.ToString("hh :mm :ss");
        string hrs = "00:00:00";
      //  string fechS = "";

        con = new System.Data.SqlClient.SqlConnection();
        con.ConnectionString = "Data Source=10.140.2.131;Initial Catalog=SistemaControl;User ID=sa; Password=sebn.TLX.mx.08";
        try
        {
            con.Open();
            //1er paso verificar si el codigo de barras o rfid estan registrados en la tabla Equipo y recuperar id
            SqlCommand cmd = new SqlCommand("SELECT id_Equipo FROM Equipo WHERE codBarras  = @idC OR rfid = @idC", con);
            cmd.Parameters.AddWithValue("idC", codB);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt); // devuelve el numero de filas.   
            if (dt.Rows.Count == 1)//si se encuentran registrados entonces ago la siguiente consulta.
            {
                //  this.Hide();//Cerrar la ventana Log
                MessageBox.Show("Se realizo la consulta:");
                //2do Verificar que ese id_Equipo tenga un registro entrada y salida en "00:00:00"
                SqlCommand cm = new SqlCommand("SELECT id_Reg FROM Registro_Eq WHERE hrE !=null and hrS= hrs ", con);
                // cm.Parameters.AddWithValue("id", codB);
                 SqlDataAdapter sd = new SqlDataAdapter(cm);
                 DataTable dts = new DataTable();
                 sd.Fill(dts);
                 if(dts.Rows.Count == 1)  //
                   {
                  MessageBox.Show("Existe el registro de entrada");
                  modificReg();
                  mostrarReg();

                   }else{
                       SqlCommand com = new SqlCommand("INSERT INTO Registro_Eq(hrE,fechaE,hrS, id_Equipo ) VALUES('" + hr + "','" + fech + "', '" + hrs + "',@id_Equipo", con);
                       com.Parameters.AddWithValue("@id_Equipo", codB);
                       int i = com.ExecuteNonQuery();
                  if(i>0){
                       MessageBox.Show("Registro agregado correctamente");
                       mostrarReg();
                  }
                  else{
                       MessageBox.Show("Error al agregar");
                  } 
                   }

            }
            else
            {
                MessageBox.Show("No se encuentra en la BD");
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
        finally
        {
            con.Close();            
        }

    }

Even in my registry modification method I feel that it is not right, I think I do not recover the id_Reg that I want to modify.

public void modificReg()
    {

        DateTime fh = DateTime.Now;
        string fech = fh.ToString("yyyy-MM-dd");
        string hr = fh.ToString("hh :mm :ss");


        con = new System.Data.SqlClient.SqlConnection();
        con.ConnectionString = "Data Source=10.140.2.131;Initial Catalog=SistemaControl;User ID=sa; Password=sebn.TLX.mx.08";

        try
        {
            con.Open();
            string cadena = "UPDATE Registro_Eq set  hrS='" + hr + "',fechaS='" + fech + "'  WHERE id_Equipo  = @idEquipo";
            SqlCommand comando = new SqlCommand(cadena, con);
            comando.Parameters.AddWithValue("@idEquipo", txt1Codigo.Text);
            int cant;
            cant = comando.ExecuteNonQuery();
            if (cant == 1)
            {
                MessageBox.Show("Se modifico la hr de salida");
                con.Close();
            }
            else
            {
                MessageBox.Show("Error de consulta");
            }
            // }

        }
        catch (Exception ex)
        {
            MessageBox.Show("Error" + ex.ToString());
        }
    }

I had already asked something similar but make some modifications.

    
asked by Daniel M. 18.05.2018 в 18:48
source

0 answers