Connect with Expertise | Find Experts, Get Answers, Share Insights

ExecuteNonQuery always 0 :(

 
Join Date: Sep 2007
Posts: 26
#1: Feb 10 '10
Hi guys,

I have a weird problem - I wonder if someone could spare a second for me. I have posted this exact question in the SQL Server forum but was advised to post it here instead.

I am running a DELETE statement inside a T-SQL Stored Procedure (for my C# .NET application). It's as simple as they get.

Expand|Select|Wrap|Line Numbers
  1. DELETE
  2.  
  3. FROM groupPromotionRequests
  4.  
  5. WHERE groupPromotionRequestId = @requestId;
However, when I call ExecuteNonQuery() I always get back 0. I have tried...

Expand|Select|Wrap|Line Numbers
  1. SET NOCOUNT OFF;
...in all sorts of places inside the procedure, yet it still always returns 0. I am certain that one row will be deleted as I have ran the query inside Management Studio and it said that one row was affected, plus I have checked the table itself.

I am very familiar with this setup and have used it all over my application, but for some reason this time it just won't work. I've tried renaming the procedure, debugging the return value from ExecuteNonQuery() and rewriting the procedure but nothing appears to work.

Does anyone have any clues? Thanks for your time.

Chris

 
Join Date: Feb 2010
Location: Italy
Posts: 3
#2: Feb 10 '10

re: ExecuteNonQuery always 0 :(


Why don't you try to run the procedure directly in the code of the form? Doing this you can check step by step what's wrong while deploying.
You can state a simple function like below (of course you need a valid SQL connection-> in my example is named cnSQL):
Expand|Select|Wrap|Line Numbers
  1. private void DeleteFunction()
  2.         {
  3.             string YOURVARIABLE = ""; //WHAT YOU WANT TO DELETE
  4.             string sQuery = "DELETE FROM groupPromotionRequests WHERE groupPromotionRequestId = '" + YOURVARIABLE + "';
  5.             try
  6.             {
  7.                 if (cnSQL.State == ConnectionState.Closed) { cnSQL.Open(); }
  8.                 System.Data.SqlClient.SqlCommand command = cnSQL.CreateCommand();
  9.                 command.CommandText = sQuery;
  10.                 command.ExecuteNonQuery();
  11.                 if (cnSQL.State == ConnectionState.Open) { cnSQL.Close(); }
  12.                 MessageBox.Show("DELETED", "OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
  13.             }
  14.             catch (Exception err)
  15.             {
  16.                 MessageBox.Show(err.Message, "attention!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  17.             }
  18.         }
Hope this will help!
RobyV
 
Join Date: Sep 2007
Posts: 26
#3: Feb 10 '10

re: ExecuteNonQuery always 0 :(


Thanks for that RobyV. However, I prefer to abstract DB functions from the UI. One reason is for reusability, another is for the performance of stored procedures. So this implementation would not work for me.

Thanks again for the time spent answering though!
tlhintoq's Avatar
E
C
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 3,476
#4: Feb 10 '10

re: ExecuteNonQuery always 0 :(


TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
insertAlias's Avatar
E
M
C
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,839
#5: Feb 12 '10

re: ExecuteNonQuery always 0 :(


You could modify your SP to have a return value, and use this line to get it:
Expand|Select|Wrap|Line Numbers
  1. return @@ROWCOUNT
See if that works for you.
Reply