I have created a stored procedure that takes several parameters and ultimately does an INSERT on two tables. The sp returns with an integer indicating which is positive if one or more rows were added.
If I execute the SP by hand using the SQL Server Management Studio Express I get the proper results, the records are added to both tables and the return values are proper. One is an output parameter indicating the Identity value of the main record, the return value simply >0 if OK.
However, when I use C#, build my connection, command and its associated parameters making sure they match the SP then I get a malfunction.
The problem is that when I call ExecuteNonQuery the integer value it returns is -1 even though calling it from Mgmt. Studio gives a >0 result. Even though it returns -1 I can confirm that the records were added to BOTH tables and that the output parameter (The identity) given to me is also correct. However the return value is always -1.
I have no idea what is going wrong, Since I have SQL Express 2005 I do cannot do profiling :(. I really don't see why this goes wrong and I think using ExecuteScalar is not the best choice for this type of action.
ExecuteScalar is used for database calls that return one and only one value. This sounds like what you're doing. Why don't you think that ExecuteScalar is appropriate for this?
|||The return Value from ExecuteNonQuery retuns number of rows effected. It is better to use ExecuteScalar but not must. For execute nonquery commnd, 0th [zero] command parameter is your return value. try to access that. If you dont get then create another parameter to ur sp @.newID OUTPUT, and set that in u r sp @.newID = @.@.IDENTITY. and access that in your CommandObject after executing ExecuteNonQuery();
Are you checking the return value from the ExecuteNonQuery function, or the parameter with type ReturnValue?
result=cmd.ExecuteNonQuery()
or
cmd.Parameters.Add("@.RETURN_VALUE",sqldbtype.Int).ParameterDirection=ReturnValue
cmd.executeNonQuery()
result=cmd.Parameters("@.RETURN_VALUE").Value
?
No comments:
Post a Comment