With the following code to add to a table
- public void AbrirCaja(decimal monto_apertura)
- {
- Debug.WriteLine("AbrirCaja con monto_apertura " + monto_apertura);
- using (var connection = GetConnection()) // Ensure GetConnection() returns the correct type of connection
- {
- connection.Open();
- using (var transaction = connection.BeginTransaction())
- {
- try
- {
- var command = connection.CreateCommand();
- command.Connection = connection;
- command.Transaction = transaction;
- Debug.WriteLine("AbrirCaja");
- command.CommandText = @"INSERT INTO CAJAS
- (FECHA_HORA_APERTURA, FECHA_HORA_CIERRE, MONTO_APERTURA,
- MONTO_CIERRE, ID_CAJERO, NOMBRE_CAJERO, CERRADA)
- VALUES
- (@FECHA_HORA_APERTURA, @FECHA_HORA_CIERRE, @MONTO_APERTURA,
- @MONTO_CIERRE, @ID_CAJERO, @NOMBRE_CAJERO, @CERRADA);";
- // Add parameters to the command
- command.Parameters.Add("@FECHA_HORA_APERTURA", DateTime.Now);
- command.Parameters.Add("@FECHA_HORA_CIERRE", string.Empty);
- command.Parameters.Add("@MONTO_APERTURA", monto_apertura);
- command.Parameters.Add("@MONTO_CIERRE", 0m);
- command.Parameters.Add("@ID_CAJERO", 0); // Replace placeholder with actual ID
- command.Parameters.Add("@NOMBRE_CAJERO", "POR DEFINIR"); // Replace placeholder with actual name
- command.Parameters.Add("@CERRADA", false);
- // Execute the command
- int rowsAffected = command.ExecuteNonQuery();
- Debug.WriteLine($"Rows affected: {rowsAffected}");
- // Commit the transaction
- transaction.Commit();
- Debug.WriteLine("Transaction committed successfully.");
- }
- catch (Exception ex)
- {
- // Rollback the transaction if there is an error
- transaction.Rollback();
- Debug.WriteLine($"Exception occurred: {ex.Message}");
- }
- }
- }
- }
Any help is appreciated.
Thanks in advance !