0

i am having issues with my method to delete a row, it is not working. I share my code to delete.

Method to delete a row:

    public void DeleteProject(string projectNameDelete)
    {
        Conectarbd.Open();

        string query = "DELETE FROM ProjectsClipBoardManager WHERE projectName = @projectName";
        SqlCommand cmd = new SqlCommand(query, Conectarbd);
        cmd.Parameters.Add("@projectName", SqlDbType.VarChar);
        cmd.Parameters["@projectName"].Value = projectNameDelete;

        int rowsAffected = cmd.ExecuteNonQuery();
        Conectarbd.Close();
        

        if (rowsAffected > 0)
        {
            MessageBox.Show("La línea se ha eliminado exitosamente.");
        }
        else
        {
            MessageBox.Show("No se encontró ningun proyecto con el nombre especificado.");
            Conectarbd.Close();
        }


    }

When i run my program my code goes through the else of DeleteProject method and says "There are not projects with that name". Sorry if my english is not good

Trying to delete a row from SQL server using C#

  • please change cmd.Parameters["@projectName"].Value = projectName Delete; with explain this post https://stackoverflow.com/a/9999792/10193401 – abolfazl sadeghi Jun 23 '23 at 16:45
  • 1. one way to make sure CaSe is not an issue. Delete from dbo.MyTable where UPPER(MyStringColumn) = UPPER('MyValue'). (You'll change your C# code to upper-case your string. 2. Having MessageBox and SqlConnection/SqlCommand code in the same block.. means you are not using the "basic 3 layer" architecture. 3 layer architecture is the MINIMAL standard IMHO. (3). you are double closing your Connection object. (but see "3 layer" comment .. if you had 3 layers, you wouldn't have that big ball of mudd issue. – granadaCoder Jun 23 '23 at 17:54
  • https://www.geeksforgeeks.org/what-is-net-3-tier-architecture/ – granadaCoder Jun 23 '23 at 17:57
  • If `SET NOCOUNT` is `ON` then the result of `rowsAffected` will be `-1` – Charlieface Jun 25 '23 at 10:44

2 Answers2

1

Try debugging step by step to see the value of "cmd" when you execute it. There might really not be an existing entry in your table that matches your "where" clause.

I dont see anything inherently wrong with the code.

You can try replacing your two lines of adding the parameter with this:

command.Parameters.AddWithValue("@projectName", projectNameDelete);

but I do suspect debugging and checking what your "cmd" actually sends to the DB will solve your issue.

0

I have run your code and it has successfully deleted a record.

The value you are trying to delete may not be found in the table.

  • Verify that within your ProjectsClipBoardManager table there is a record that contains this value in the projectName column.

  • Verifies that the string exactly matches the one found in the table, that there are no spaces, signs or different capital letters.

Note: It is recommended to delete one record per primary key because doing this for the projectName column will delete all records matching that value, and when you run it again you will no longer have any records with that value, and no rows will be affected.

kuudev
  • 11
  • 2