0

I have written this query in c# , query syntax is OK, query is also receiving parameter values as I have checked using breakpoints and also same query is working in SQL server management studio, but in visual studio it does not gives any error but also does not delete item from table.

private void deleteItem(int itemId, int saleId)
{
    SqlConnection conn = new SqlConnection(connString);

    SqlCommand deleteItem = new SqlCommand(
      "Delete FROM items_in_sales WHERE sale_id=@sale_id AND item_id=@item_id", 
       conn);

    deleteItem.Parameters.AddWithValue("@sale_id", itemId);
    deleteItem.Parameters.AddWithValue("@item_id", saleId);

    try
    {
        conn.Open();
        deleteItem.ExecuteNonQuery();
        conn.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

please help.

3
  • 1
    check values of itemId and saleId, does in db exists record with that sale_id and item_id ? Commented Mar 29, 2018 at 9:40
  • 3
    The ExecuteNonQuery method returns an integer that tells you how many rows have been deleted by your query. Get that return and display it somewhere or use the debugger to see the result. Tell us what value you get. Commented Mar 29, 2018 at 9:41
  • 1
    I also suggest using a using block. But that seems unrelated. Commented Mar 29, 2018 at 9:42

1 Answer 1

4

Fix your parameters they are wrong way around

deleteItem.Parameters.AddWithValue("@sale_id", itemId);
deleteItem.Parameters.AddWithValue("@item_id", saleId);

should be

deleteItem.Parameters.AddWithValue("@sale_id", saleId);
deleteItem.Parameters.AddWithValue("@item_id", itemId);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.