The input string does not have the correct format. C # SQL

3

I have an error, as I mention in the good title it turns out that I'm doing a webform in c # and when executing it I get that error Here my code:

public void Ordenes(int ID_Orden)
{
    conectar();
    consulta = "P_SANCHEZ_ORDEN";
    comando = new SqlCommand(consulta, conexion);
    comando.CommandType = CommandType.StoredProcedure;
    comando.Parameters.AddWithValue("@ID_ORDEN",ID_Orden);
    comando.Parameters.Add("@FECHAORDEN", SqlDbType.NVarChar,10).Direction = ParameterDirection.Output;
    comando.Parameters.Add("@SUB", SqlDbType.Money).Direction = ParameterDirection.Output;
    comando.Parameters.Add("@IVA", SqlDbType.Money).Direction = ParameterDirection.Output;
    comando.Parameters.Add("@TOTAL", SqlDbType.Money).Direction= ParameterDirection.Output;
    comando.Connection.Open(); // Abre la conexion
    comando.ExecuteNonQuery(); //Ejecuta 
    FechaOrden = comando.Parameters["@FECHAORDEN"].Value.ToString();
     sub = float.Parse(comando.Parameters["@SUB"].Value.ToString()); <--- En esta línea me marca ese error
    IVA = float.Parse(comando.Parameters["@IVA"].Value.ToString());
    Total = float.Parse(comando.Parameters["@TOTAL"].Value.ToString());
    comando.Connection.Close(); //Cierra
}

Then, according to my teacher, he tells me that it is the SQL in my procedure ... But this one does not mark me any error and when reviewing it, everything works perfectly.

CREATE PROC P_SANCHEZ_ORDEN -- DROP PROC P_SANCHEZ_ORDEN
@ID_ORDEN INT,
@FECHAORDEN NVARCHAR (10) OUTPUT,
@SUB MONEY OUTPUT,
@IVA MONEY OUTPUT,
@TOTAL MONEY OUTPUT
AS
BEGIN
select convert(nvarchar(10),FechaOrden,103) from Ordenes where ID_Orden = @ID_ORDEN
SELECT SUM(TOTALART) FROM V_SANCHEZ_DETALLEORDEN WHERE ID_Orden = @ID_ORDEN
SELECT SUM(TOTALART) *.16 FROM V_SANCHEZ_DETALLEORDEN WHERE ID_Orden = @ID_ORDEN
SELECT SUM(TOTALART)*1.16 FROM V_SANCHEZ_DETALLEORDEN WHERE ID_Orden = @ID_ORDEN
END

Can you help me identify why this error occurs please?

    
asked by Alonso Sánchez 01.12.2017 в 09:04
source

2 answers

3

As your teacher says, the problem is in the stored procedure. You are declaring some output parameters, but you are never assigning them value. You must modify the SELECT to assign the result to your parameters:

select @FECHAORDEN = convert(nvarchar(10),FechaOrden,103) from Ordenes where ID_Orden = @ID_ORDEN
SELECT @SUB= SUM(TOTALART) FROM V_SANCHEZ_DETALLEORDEN WHERE ID_Orden = @ID_ORDEN
SELECT @IVA = SUM(TOTALART) *.16 FROM V_SANCHEZ_DETALLEORDEN WHERE ID_Orden = @ID_ORDEN
SELECT @TOTAL = SUM(TOTALART)*1.16 FROM V_SANCHEZ_DETALLEORDEN WHERE ID_Orden = @ID_ORDEN
    
answered by 01.12.2017 / 09:27
source
1

Good Alonso,

The problem you have is that you are trying to cast the type Money SQL with a Float , with which that conversion is not possible.

You should use the type Decimal to cast the Money :

decimal sub;
decimal IVA;
decimal Total;

sub = Convert.ToDecimal(comando.Parameters["@SUB"].Value.ToString());
IVA = Convert.ToDecimal(comando.Parameters["@IVA"].Value.ToString());
Total = Convert.ToDecimal(comando.Parameters["@TOTAL"].Value.ToString());
    
answered by 01.12.2017 в 09:17