Search code examples
c#angularhttpclientput

HttpClient put issue in Angular


I'm developing an Angular app with Web api.

I have created a service (sellerService) in which I can update some data in my database with HttpClient put.

Above works but it update all the data of my table, something like follows;

Before I update my seller:

BEFORE

After I update my seller:

AFTER

My sellerService code:

  updateSeller(user: string, nbsales: number, pVote: number, nVote: number, idUser: number): Observable<any> {
      return this.http.put('http://localhost:50867/api/seller_user/', {
        'username': user,
        'nbSales': nbsales,
        'positiveVote': pVote,
        'negativeVote': nVote,
        'idUser': idUser
      });
    }

My update query (DAO (c#)):

public static readonly string UPDATE = "update " + TABLE_NAME + " set "
            + COLUMN_USERNAME + " =@username"
            + ", " + COLUMN_NB_SALES + "=@nbSales"
            + ", " + COLUMN_POSITIVE_VOTE + "=@positiveVote"
            + ", " + COLUMN_NEGATIVE_VOTE + " =@negativeVote"
            + ", " + COLUMN_ID_USER + "=@idUser";

        //Update a seller_user
        public static bool Update(Seller_user todo)
        {
            bool state = false;
            using (SqlConnection connection = DataBase.GetConnection())
            {
                connection.Open();
                SqlCommand command = new SqlCommand(UPDATE, connection);

                //command.Parameters.AddWithValue("@idSeller", todo.idSeller);
                command.Parameters.AddWithValue("@username", todo.username);
                command.Parameters.AddWithValue("@nbSales", todo.nbSales);
                command.Parameters.AddWithValue("@positiveVote", todo.positiveVote);
                command.Parameters.AddWithValue("@negativeVote", todo.negativeVote);
                command.Parameters.AddWithValue("@idUser", todo.idUser);

                state = command.ExecuteNonQuery() != 0;
            }
            return state;
        }

Thanks in advance ;)


Solution

  • You missed where clause in SQL query. So it will update all records.

    public static readonly string UPDATE = "update " + TABLE_NAME + " set "
                + COLUMN_USERNAME + " =@username"
                + ", " + COLUMN_NB_SALES + "=@nbSales"
                + ", " + COLUMN_POSITIVE_VOTE + "=@positiveVote"
                + ", " + COLUMN_NEGATIVE_VOTE + " =@negativeVote"
                + ", " + COLUMN_ID_USER + "=@idUser"
                + "WHERE "  + COLUMN_ID_USER + "=" + "= @idUser";