Why does ExecuteNonQuery () give me the following error?

0

Good will put my code here and have if someone can tell me what error I have here because I can not see it

public List<EntidadProducto> listarProductosCD(SqlConnection conexion, int ID, String nombre, decimal precio)
    {
        List<EntidadProducto> listaProductos = new List<EntidadProducto>(); 
        CapaDProducto ocdProducto = new CapaDProducto();

        using (conexion)
        {
            String consultaSqlUpdate = "UPDATE PRODUCTS SET ProductName=@ProductName, UnitPrice=@UnitPrice WHERE=" + ID;
            SqlCommand comando = new SqlCommand(consultaSqlUpdate, conexion);
            comando.Parameters.AddWithValue("@ProductName", nombre);
            comando.Parameters.AddWithValue("@UnitPrice", precio);

            int filaAfectados = comando.ExecuteNonQuery();

            String consultaSql = "SELECT * FROM PRODUCTS";

            SqlCommand comando2 = new SqlCommand(consultaSql, conexion);

            SqlDataReader lectura = comando2.ExecuteReader();

            while (lectura.Read())
            {
                EntidadProducto oentProducto = new EntidadProducto();
                oentProducto.idProducto = lectura.GetInt32(lectura.GetOrdinal("ProductID"));
                oentProducto.Nombre = lectura.GetString(lectura.GetOrdinal("ProductName"));
                oentProducto.Precio = lectura.GetDecimal(lectura.GetOrdinal("UnitPrice"));
                listaProductos.Add(oentProducto);
            }

            lectura.Close();
        }

        return listaProductos;
    }

In the line int filaAfetados I get the following:

Am I storing the result in an integer variable because it gives me syntax error in the "="? Saying that I am new to ASP.net maybe there is something earlier than what I am doing wrong, since I am executing the action query and it does not modify the data either, maybe the SQL statement is wrong. I hope you can help me. Thank you.

    
asked by Ronald Sanchez 23.10.2017 в 00:32
source

3 answers

2

Good Ronald,

Your error is because you have a syntax error in the SQL query:

String consultaSqlUpdate = "UPDATE PRODUCTS SET ProductName=@ProductName, 
UnitPrice=@UnitPrice WHERE=" + ID;

If you notice, in the WHERE you are not passing it that field has to be equal to ID , then it gives you syntax error near '=' since it can not recognize which field you should compare with ID :

You should have something like this:

String consultaSqlUpdate = "UPDATE PRODUCTS SET ProductName=@ProductName, 
UnitPrice=@UnitPrice WHERE ProductID=" + ID;
    
answered by 23.10.2017 / 08:45
source
1

In the String variable that you define the update query is wrongly made

String consultaSqlUpdate = "UPDATE PRODUCTS SET ProductName=@ProductName, UnitPrice=@UnitPrice WHERE ProductID=" + ID;

Remember the basics for performing an update in an sql query.

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
    
answered by 23.10.2017 в 19:07
0

My brother that value is because you are not defining it if it is stored procedure or query. I miss this code:

comando.CommandType = CommandType.Text;


public List<EntidadProducto> listarProductosCD(SqlConnection conexion, int ID, String nombre, decimal precio)
    {
        List<EntidadProducto> listaProductos = new List<EntidadProducto>(); 
        CapaDProducto ocdProducto = new CapaDProducto();

        using (conexion)
        {
            String consultaSqlUpdate = "UPDATE PRODUCTS SET ProductName=@ProductName, UnitPrice=@UnitPrice WHERE=" + ID;
            SqlCommand comando = new SqlCommand(consultaSqlUpdate, conexion);
 comando.CommandType = CommandType.Text;            
comando.Parameters.AddWithValue("@ProductName", nombre);
            comando.Parameters.AddWithValue("@UnitPrice", precio);

            int filaAfectados = comando.ExecuteNonQuery();

            String consultaSql = "SELECT * FROM PRODUCTS";

            SqlCommand comando2 = new SqlCommand(consultaSql, conexion);

            SqlDataReader lectura = comando2.ExecuteReader();

            while (lectura.Read())
            {
                EntidadProducto oentProducto = new EntidadProducto();
                oentProducto.idProducto = lectura.GetInt32(lectura.GetOrdinal("ProductID"));
                oentProducto.Nombre = lectura.GetString(lectura.GetOrdinal("ProductName"));
                oentProducto.Precio = lectura.GetDecimal(lectura.GetOrdinal("UnitPrice"));
                listaProductos.Add(oentProducto);
            }

            lectura.Close();
        }

        return listaProductos;
    }

I'll attach an example:

public void Usp_MostrarIncidentes(Int32 opt,String username,Int32 IdAtencionTarea)
        {
            SqlDataReader reader;

            SqlConnection cn = new SqlConnection(ObtenerCadenaConexion());
            cn.Open();
            SqlCommand cmd = new SqlCommand("Usp_MostrarIncidentes", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@opt", SqlDbType.Int).Value = opt;
            cmd.Parameters.Add("@username", SqlDbType.NVarChar, 250).Value = username;
            cmd.Parameters.Add("@idatenciontarea", SqlDbType.NVarChar, 250).Value = IdAtencionTarea;

            var students = new List<ListaIncidencias>();
            reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                var student = new ListaIncidencias
                {
                    Minutos = Convert.ToInt32(reader[0].ToString()),
                    Id = Convert.ToInt32(reader[1].ToString()),
                    Fecha = Convert.ToString(reader[2].ToString()),
                    TipoIncidencia = reader[3].ToString(),
                    Sub_TipoIncidencia = reader[4].ToString(),
                    Estado = reader[5].ToString(),
                    Ver = reader[6].ToString()

                };
                students.Add(student);
            }
            var js = new JavaScriptSerializer();
            Context.Response.Write(js.Serialize(students));

        }
    
answered by 23.10.2017 в 01:56