Error updating in windows form

0

could help me with this error when wanting to update the data

private void btnactualizar_Click(object sender, EventArgs e)
    {
         int resp;
          asignarDatosObjeto();
          resp = CATCAD.Actualizar(CATCRN);
          if (resp == 1)
          {
              UpdateDGV();
          }
          else
          {
              MessageBox.Show("Imposible conectarse a la base de datos: " + CATCAD.error,
                  "Fallo la conexion", MessageBoxButtons.OK, MessageBoxIcon.Error);
              this.Close();
          }            
    }




 public int Actualizar(CatalogoCRN CatalogoObj)
     {

        SqlCommand UpdateCad;
          string comandoSql;
          try
          {



            SqlCommand command = new SqlCommand("SP_ACTUALIZAR", connectionSql);
            command.CommandType = CommandType.StoredProcedure;

            connectionSql.Open();

command.Parameters.AddWithValue("@NUMFACTURA", CatalogoObj.numFactura);
                command.Parameters.AddWithValue("@NAMECLIENTE", CatalogoObj.nameClient);
                command.Parameters.AddWithValue("@NUMPARTE", CatalogoObj.numPart);
                command.Parameters.AddWithValue("@TIPOMATERIAL", CatalogoObj.tipMaterial);
                command.Parameters.AddWithValue("@DESCRIPTIONSPANISH", CatalogoObj.DescriptionESp);
                command.Parameters.AddWithValue("@DESCRIPTIONENGLISH", CatalogoObj.DescriptionUSA);
                command.Parameters.AddWithValue("@CANTIDAD", CatalogoObj.quantity);
                command.Parameters.AddWithValue("@UNITPRICE", CatalogoObj.UnitPrice);
                command.Parameters.AddWithValue("@NUMPALLET", CatalogoObj.numPallet);
                command.Parameters.AddWithValue("@OBSERVATION", CatalogoObj.observations);
                command.Parameters.AddWithValue("@COUNTRY", CatalogoObj.country);
                command.Parameters.AddWithValue("@FECHAINGRESO", CatalogoObj.date);
                command.ExecuteNonQuery();

                connectionSql.Close();
                 return 1;
             }
             catch (Exception ex)
             {
                 error = ex.Message;
                 return 0;

             }
        }

stored procedure is simple

    CREATE PROCEDURE SP_ACTUALIZAR
@FACTURA VARCHAR(100),
@CLIENTE VARCHAR(100), 
@PARTE VARCHAR(100),
@MATERIAL VARCHAR(100),
@DESCRIPCIONSPANISH VARCHAR(100),
@DESCRIPCIONENGLISH VARCHAR(100),
@CANT INT,
@PRICE FLOAT,
@PALLET FLOAT,
@OBSERVACION VARCHAR(100),
@PAIS VARCHAR(50),
@FECHA DATETIME 

AS BEGIN

UPDATE CATALOGO SET NAMECLIENTE = @CLIENTE,

    NUMPARTE = @PARTE,
    TIPOMATERIAL = @MATERIAL,
    DESCRIPTIONSPANISH = @DESCRIPCIONSPANISH,
    DESCRIPTIONENGLISH = @DESCRIPCIONENGLISH,
    CANTIDAD = @CANT,
    UNITPRICE = @PRICE,
    NUMPALLET = @PALLET,
    OBSERVATION = @OBSERVACION,
    COUNTRY = @PAIS,
    FECHAINGRESO = @FECHA

    WHERE  NUMFACTURA  = @FACTURA
    END
    GO

Storage test in sql

before executing the procedure

after executing the procedure

    
asked by Daniel 20.09.2017 в 00:24
source

3 answers

1

Notice that when creating your parameters you are not naming any with @FACTURA. Can you show the code of that procedure? You must create the parameters like this:

command.Parameters.AddWithValue("@FACTURA", Catalog.numFactura);

the first parameter of this method AddWithValue is the name of the parameter in your stored procedure, not the name of your property of the class

    
answered by 20.09.2017 / 18:45
source
0

well the error marks a syntax error, it verifies the data types that the stored procedure receives ... you send all the fields with text type so I see ... you put them all in single quotes

  

Call SP_ACTUALIZAR ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{ 7} ',' {8} ',' {9} ',' {10} ',' {11} ')



UPDATED
-------------------------------------------------- ----------
Try running the procedure like this:

SqlCommand command = new SqlCommand("SP_ACTUALIZAR", conn);
command.CommandType = CommandType.StoredProcedure;

command.Parameters.AddWithValue("numFactura", numFactura);
command.Parameters.AddWithValue("nameClient", nameClient);
command.Parameters.AddWithValue("numPart", numPart);
command.Parameters.AddWithValue("tipMaterial", tipMaterial);
command.Parameters.AddWithValue("DescriptionESp", DescriptionESp);
...
command.ExecuteNonQuery();
    
answered by 20.09.2017 в 00:43
0

try putting it this way:

SqlCommand command = new SqlCommand("EXEC SP_ACTUALIZAR @CLIENTE, @PARTE,@MATERIAL, @DESCRIPCIONSPANISH, 'DESCRO',@DESCRIPCIONENGLISH, @CANT,@PRICE,@PALLET,@OBSERVACION,@PAIS,@FECHA", conn);

command.CommandType = CommandType.StoredProcedure;

command.Parameters.AddWithValue("@FACTURA", numFactura);
command.Parameters.AddWithValue("@CLIENTE", nameClient);
command.Parameters.AddWithValue("@PARTE", numPart);
command.Parameters.AddWithValue("@MATERIAL", tipMaterial);
etc...

this is an example, I did not put the variables in correct order or if I miss some

    
answered by 21.09.2017 в 18:10