Click here to Skip to main content
15,888,037 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I am inserting some values into databse. I am using visual C# and sql server.Here when I am inserting values I am getting -1 at execute nonquery() and database is not updated. I checked every value by debugging. but Failed to find error in which line it is coming.So how can I find that

What I have tried:

I googled but failed to get suitable solution.
Posted
Updated 17-Oct-17 7:27am
v2
Comments
pkfox 17-Oct-17 10:40am    
Your code please :-)
A. Orozco 17-Oct-17 10:43am    
You beat me to it!
ZurdoDev 17-Oct-17 10:46am    
How can you find out? You'll have to debug it. You can use Sql Profiler to see the exact sql that was sent and then figure it out from there.
vijay_bale 17-Oct-17 10:57am    
I already debugged.It is showing values also. but not storing in database.
ZurdoDev 17-Oct-17 11:01am    
Then you have to figure out why it is not in db. As I said, run sql profiler and see the exact sql that is being run.

I'm not sure what you want us to do. We can't see what you are doing so there is no way for us to know what you have done wrong.

You will only get a value other than -1 for a delete, update or insert statement that commits a value. If a rollback occurs, you'll get -1 so it could well be that the database is rolling back the value. As you haven't even supplied the most minimum of information, I'm going to guess it's something like a rollback that's causing you not to see any data. I will revise my opinion once we actually see some code.
 
Share this answer
 
To add some details, if you're using a stored procedure then setting the NOCOUNT option on causes this effect. However, it does not explain why no data is found so as pointed out you either add no data or it is rolled back.

Also note that the rollback is not necessarily done on the server side


To test different variations for stored procedure behaviour consider the following scenario

Test table
SQL
CREATE TABLE NoCountTable (
   col1 int
);
GO


Test procedures
SQL
CREATE PROCEDURE NoCountProcedure AS 
BEGIN
  SET NOCOUNT ON;
  INSERT INTO NoCountTable VALUES (1);
END;
GO

CREATE PROCEDURE CountProcedure AS 
BEGIN
  SET NOCOUNT OFF;
  INSERT INTO NoCountTable VALUES (1);
END;
GO

CREATE PROCEDURE RollbackProcedure AS 
BEGIN
  SET NOCOUNT ON;
  BEGIN TRANSACTION
     INSERT INTO NoCountTable VALUES (1);
  ROLLBACK;
END;
GO

CREATE PROCEDURE ExceptionProcedure AS 
BEGIN
  SET NOCOUNT ON;
  BEGIN TRANSACTION
     INSERT INTO NoCountTable VALUES (1/0);
  ROLLBACK;
END;
GO


Calling program
C#
int result;

using (SqlConnection connection = new SqlConnection()) {
   connection.ConnectionString = "server=.\\<instance_name_here>;integrated security=true";
   using (SqlCommand command = new SqlCommand()) {
      command.Connection = connection;
      try {
         connection.Open();

         command.CommandText = "NoCountProcedure";
         result = command.ExecuteNonQuery();
         Debug.WriteLine(string.Format("{0} returned {1}", 
                         command.CommandText,  result));

         command.CommandText = "CountProcedure";
         result = command.ExecuteNonQuery();
         Debug.WriteLine(string.Format("{0} returned {1}", 
                         command.CommandText, result));

         command.CommandText = "RollbackProcedure";
         result = command.ExecuteNonQuery();
         Debug.WriteLine(string.Format("{0} returned {1}", 
                         command.CommandText, result));

         command.CommandText = "ExceptionProcedure";
         result = command.ExecuteNonQuery();
         Debug.WriteLine(string.Format("{0} returned {1}", 
                         command.CommandText, result));
      } catch (System.Exception exception) {
         Debug.WriteLine(string.Format("Exception occurred {0}: {1}", 
                         exception.Source, exception.Message));
      }
   }
}


The output from the program is
NoCountProcedure returned -1
CountProcedure returned 1
RollbackProcedure returned -1
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
Exception occurred .Net SqlClient Data Provider: Divide by zero error encountered.
The statement has been terminated.


But as pointed out by @Pete-OHanlon, this is merely explaining different possibilities since you haven't posted any code.
 
Share this answer
 
How to Find It: No Code Submitted method:

1 - create the query you think you wish to execute and see if it works when run directly in SQL (however you do this).

If that successfully inserts the record:
2 - if that works, paste it into your code so that the know working query is executed via Executenonquery(). If it doesn't work, problem is in the external parts (such as a rollback or whatever).

If that successfully inserts the record:
3 - trap (possibly a print to screen?) the query you are generating with your code and see what's wrong with it. Perhaps use it in (1), above.

You have now debugged your query. Determined if it is because the SQL is no good or the rest of your handling is no good.

If the latter, check your conditionals (maybe by commenting out!) and see where things go wrong. Or better yet, use debugger.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900