ExecuteNonQuery() does not return the affected rows in C# and SQL Server database

Viewed 531

I'm using SQL Server 2008 and Visual Studio 2010. The issue happen when using ExecuteNonQuery() command. In most cases, it returns the real number of rows affected, but when I tried to delete a group of records, it returns 0.

When I opened the table in SQL Server Management Studio, I see 33 records were deleted.

This is my code:

sqlQuery = "delete from  priv_  where user_id=" + txt_id.Text;
cmd = new SqlCommand(sqlQuery, con);
MessageBox.Show("rows affected = " + cmd.ExecuteNonQuery());

The message box show

rows affected = 0

When I use the same query in SQL Server Management Studio, it shows

33 rows affected

I have change the ONCOUNT to off, but nothing change.

1 Answers
Related