Click here to Skip to main content
15,896,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm getting this error whenever I try to insert a sale data into th database, I've been doing some research, but failed to get or to understand an answer.

This is the procedure on the DB:
SQL
CREATE PROC SPInsertar_Venta
	@IDVentas int = NULL OUTPUT,
	@NumeroVentas nvarchar(30),
	@Fecha date,
	@Estado nvarchar(10),
	@IDCliente int
as
	INSERT INTO Ventas VALUES(@NumeroVentas, @Fecha, @Estado, @IDCliente)
	SET @IDVentas = @@IDENTITY
go

CREATE PROC SPInsertar_DetallesVenta
	@Cantidad int,
	@Descripcion nvarchar(256),
	@Precio decimal,
	@Impuestos decimal,
	@Totales money,
	@IDVentas int
as
	INSERT INTO DetallesVenta VALUES(@Cantidad, @Descripcion, @Precio, @Impuestos, @Totales, @IDVentas)
go


This is the code that makes the insertion on the DB:
C#
public string InsertSales(E_Sales _Sales, List<E_SaleDetails> _SaleDetails)
        {
            string answer;

            try
            {
                connection.Open();
                SqlTransaction sqlTransaction = connection.BeginTransaction();

                SqlCommand command = new SqlCommand
                {
                    Connection = connection,
                    Transaction = sqlTransaction,
                    CommandText = "SPInsertar_Venta",
                    CommandType = CommandType.StoredProcedure
                };

                SqlParameter sqlParameterIDVentas = new SqlParameter
                {
                    ParameterName = "@IDVentas",
                    SqlDbType = SqlDbType.Int,
                    Direction = ParameterDirection.Output
                };

                command.Parameters.Add(sqlParameterIDVentas);

                command.Parameters.AddWithValue("@NumeroVentas", _Sales.NumberOfSales);
                command.Parameters.AddWithValue("@Fecha", _Sales.Date);
                command.Parameters.AddWithValue("@Estado", _Sales.State);
                command.Parameters.AddWithValue("@IDCliente", _Sales.IdClient);

                answer = command.ExecuteNonQuery() == 1 ? "OK" : "No se pudo insertar la venta."; //At this point, the sale is alredy inserted

                if (answer.Equals("OK"))
                {
                    _Sales.IdSales = Convert.ToInt32(command.Parameters["IDVentas"].Value); //Here the primary key of that sale is obtained

                    foreach (E_SaleDetails details in _SaleDetails)
                    {
                        details.IdSales = _Sales.IdSales; //Then here that primary key is used as the foreign key in the table "DetallesVenta"
                        answer = d_SaleDetails.InsertSaleDetails(details, ref connection, ref sqlTransaction); //Then proceed to insert the sale detail

                        if (!answer.Equals("OK"))
                        {
                            break;
                        }
                    }
                }

                if (answer.Equals("OK"))
                {
                    sqlTransaction.Commit(); //If everithing is alright, then the transaction is confirmed
                }
                else
                {
                    sqlTransaction.Rollback(); //Or if not, then is reverted/rejected
                }
            }
            catch (Exception ex)
            {
                answer = ex.Message;
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                {
                    connection.Close();
                }
            }

            return answer;
        }


Lastly, here's the controller:
C#
public static string InsertSales(E_Sales _Sales, DataTable _SaleDetails)
        {
            D_Sales objSales = new D_Sales();
            List<E_SaleDetails> saleDetails = new List<E_SaleDetails>();

            foreach (DataRow row in _SaleDetails.Rows)
            {
                E_SaleDetails details = new E_SaleDetails
                {
                    Quantity = Convert.ToInt32(row["Cantidad"].ToString()),
                    Description = row["Descripcion"].ToString(),
                    Price = Convert.ToDecimal(row["Precio"].ToString()),
                    Taxes = Convert.ToDecimal(row["Impuestos"].ToString()),
                    Totals = Convert.ToDecimal(row["Totales"].ToString())
                };

                saleDetails.Add(details);
            }

            return objSales.InsertSales(_Sales, saleDetails);
        }


What I have tried:

I've been excluding some parts of the code while trying to get to the problem itself, but then, as I said, I don't really get what I'm supposed to do now to fix the error. Any help is highly appreciated.
Posted
Updated 10-Jan-22 5:21am
Comments
PIEBALDconsult 10-Jan-22 14:31pm    
Best not include the @ as part of the parameter name, the system will know what you mean.

1 solution

Look at your code:
C#
SqlParameter sqlParameterIDVentas = new SqlParameter
{
    ParameterName = "@IDVentas",
    SqlDbType = SqlDbType.Int,
    Direction = ParameterDirection.Output
};

command.Parameters.Add(sqlParameterIDVentas);
C#
_Sales.IdSales = Convert.ToInt32(command.Parameters["IDVentas"].Value);
So you add a parameter called "@IDVentas" and try to read it back as "IDVentas".

All the characters in a name are important!
 
Share this answer
 
Comments
Héctor Febles 10-Jan-22 11:27am    
Thank you so much! I totally did not noticed that I was missing a character. And was looking for the error for long... xD
OriginalGriff 10-Jan-22 11:34am    
You're welcome!
You're not the only one: I do it all the time - I read the code I meant to write ... :blush:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900