Hello Good morning I hope and you can help me please I need your help, I am new in programming and the truth still costs me to program. I have a project where I make entry and exit records through a scanner using the barcode. My problem is that when I already have my output hr, I want to finish that record and create a new one. What it does is to be modifying my output hr every time I read that bar code and it never ends. I do not know where part of my code is wrong or what is missing.
My method to register is done in the following way.
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();
SqlCommand cmd = new SqlCommand("SELECT id_codBarras FROM Equipo WHERE id_codBarras = @idC OR rfid = @idC", con);
// SqlCommand cmd1 = new SqlCommand("SELECT rfid FROM Equipo WHERE 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)
{
// this.Hide();//Cerrar la ventana Log
MessageBox.Show("Se realizo la consulta:");
SqlCommand cm = new SqlCommand("SELECT id_codBarras FROM Registro_Eq WHERE id_codBarras = @id", 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_codBarras ) VALUES('" + hr + "','" + fech + "', '" + hrs + "', @cod)", con);
com.Parameters.AddWithValue("@cod", 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 en la BD");
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
con.Close();
}
}
My Modify method is as follows.
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_codBarras = '" + txt1Codigo.Text + "'";
SqlCommand comando = new SqlCommand(cadena, con);
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());
}
}
To show I use this method.
public void mostrarReg()
{
try
{
SqlCommand cmd = new SqlCommand("SELECT id_Reg as 'ID',hrE as 'Hr de Entrada' ,fechaE as 'Fecha de Entrada' ,hrS as 'Hr de Salida',fechaS as 'Fecha de Salida', deptoNum as 'Depto y No. Equipo', id_numControl as 'No. Control' FROM Registro_Eq INNER JOIN Equipo ON Registro_Eq.id_codBarras = Equipo.id_codBarras ", con);
SqlDataAdapter adaptador = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adaptador.Fill(dt);
TabReg.DataSource = dt;
}
catch (Exception e)
{
MessageBox.Show("No se pudo cargar los datos en la Tabla" + e.ToString());
}
}