473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ExecuteNonQuery always 0 :(

26
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
Feb 10 '10 #1
4 4661
RobyV
4
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
Feb 10 '10 #2
cbellew
26
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!
Feb 10 '10 #3
tlhintoq
3,525 Expert 2GB
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.
Feb 10 '10 #4
Curtis Rutland
3,256 Expert 2GB
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.
Feb 12 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mark | last post by:
Hi, I have a sqlCommand object that I use to execute an SQL statement. I thought I could use the ExecuteNonQuery to test for the number of rows returned by the sql command but it does not seem to...
1
by: Mark | last post by:
It appears that you can only retrieve an Output parameter from a SQL Server stored procedure when using the ExecuteNonQuery of the SqlCommand class, and cannot use the ExecuteReader() method. In...
10
by: Mike | last post by:
I know this sounds strange but I am at a loss. I am calling a simple funtion that opens a connection to a SQL Server 2000 database and executes an Insert Statement. private void...
5
by: Paul Aspinall | last post by:
Hi I have a Stored Proc in SQL server, which creates a record key when a record is created. I want to return the value back to my code, once the record has been created. I am using SQLHelper...
1
by: Matthew Louden | last post by:
I just tried to create a simple ASP.NET application to add record to the SQL Server database. However, it has run time error on Line 88: objCommand.ExecuteNonQuery() Any ideas?? Please help!!...
2
by: jdb | last post by:
Hi, I am adding a record to the database using an ExecuteNonQuery, which adds without problem. Now after the record is added I run a method passing in some info as well as the curretnly opened...
3
by: keithb | last post by:
What can I put in a stored procedure to control what gets returned by command.ExecuteNonQuery()? I already tried this: param = comm.CreateParameter(); param.ParameterName = "@Success"; ...
3
by: Manikandan | last post by:
Hi, I'm using executenonquery to check the existence of table in the database. I tried two methods of queries, everything return -1 even though table exists in database Code as below...
8
by: Tony M | last post by:
Shouldn't the ExecuteNonQuery be TRUE if it finds a record and FALSE is not? I get FALSE all the time and I seeded the database with the email address. I just want to check if record exist before...
2
by: TheSteph | last post by:
Hi I have a SQLCommand that do updates on a table. I have a loop where I set the parameters then I call ExecuteNonQuery. For . { . Set the SQLCommand's Params Values;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.