473,395 Members | 1,624 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,395 software developers and data experts.

Stored procedure with ExecuteNonQuery not returning value

Hi all:

I m trying to get the return value of a stored procedure with

....
DbCommand command = connection.CreateCommand();
command.CommandText = "Exec some_SP";
command.CommandType = CommandType.StoredProcedure;

int temp = command.Parameters.Add(new OdbcParameter("@ReturnValue",
OdbcType.Int, 15));
DbParameter parameter = command.Parameters[temp];
parameter.Direction = ParameterDirection.Output;

command.ExecuteNonQuery();

return command.Parameters["@ReturnValue"].Value.ToString()
....

however the @ReturnValue is _always_ 0 (and i know it shouldn't be)
what s the problem?
is it the way I set up the parameter?

Dec 5 '07 #1
8 14340

firstly it should be this:
command.CommandText = "some_SP";

secondly, the parameter direction should be ParameterDirection.ReturnValue,
not output.

Try this, and if it still doesn't work, could you post your SP, or at least
the SQLe you use to run it, with results showing resultvalue can in fact no
be 0?

"roundcrisis" <ro*********@gmail.comwrote in message
news:e7**********************************@d61g2000 hsa.googlegroups.com...
Hi all:

I m trying to get the return value of a stored procedure with

...
DbCommand command = connection.CreateCommand();
command.CommandText = "Exec some_SP";
command.CommandType = CommandType.StoredProcedure;

int temp = command.Parameters.Add(new OdbcParameter("@ReturnValue",
OdbcType.Int, 15));
DbParameter parameter = command.Parameters[temp];
parameter.Direction = ParameterDirection.Output;

command.ExecuteNonQuery();

return command.Parameters["@ReturnValue"].Value.ToString()
...

however the @ReturnValue is _always_ 0 (and i know it shouldn't be)
what s the problem?
is it the way I set up the parameter?
Dec 5 '07 #2
On Dec 5, 4:38 pm, "Dan Bass" <danb...@REMOVEblueCAPSbottle.com>
wrote:
firstly it should be this:
command.CommandText = "some_SP";

secondly, the parameter direction should be ParameterDirection.ReturnValue,
not output.

Try this, and if it still doesn't work, could you post your SP, or at least
the SQLe you use to run it, with results showing resultvalue can in fact no
be 0?

"roundcrisis" <roundcri...@gmail.comwrote in message

news:e7**********************************@d61g2000 hsa.googlegroups.com...
Hi all:
I m trying to get the return value of a stored procedure with
...
DbCommand command = connection.CreateCommand();
command.CommandText = "Exec some_SP";
command.CommandType = CommandType.StoredProcedure;
int temp = command.Parameters.Add(new OdbcParameter("@ReturnValue",
OdbcType.Int, 15));
DbParameter parameter = command.Parameters[temp];
parameter.Direction = ParameterDirection.Output;
command.ExecuteNonQuery();
return command.Parameters["@ReturnValue"].Value.ToString()
...
however the @ReturnValue is _always_ 0 (and i know it shouldn't be)
what s the problem?
is it the way I set up the parameter?- Hide quoted text -

- Show quoted text -
Thanks for your answer
I have tried those changes however it doesn't seem to solve the
problem
I cant show you the code for my SP because I dont use one, the user
can chose from a set of existing working stored procedures
running the above code doesn't throw any exception or anything and i
can see it running in the profiler with "exec " included
I tested without exec and runs as well without exceptions wich I find
very strange

Also, I wonder why it was the only way to create a parameter using an
odbcparameter

Cheers
Dec 5 '07 #3

If you're connecting to SQL Server you may as well use the SqlClient for
your connection / command / parameters.

If you can't see the SP, how do you know that it doesn't return non-zero
values? I'd run the sql exec from Management Studio to confirm the results.

If your command type is Stored proc then you shouldn't have the exec...

"roundcrisis" <ro*********@gmail.comwrote in message
news:23**********************************@w40g2000 hsb.googlegroups.com...
On Dec 5, 4:38 pm, "Dan Bass" <danb...@REMOVEblueCAPSbottle.com>
wrote:
>firstly it should be this:
command.CommandText = "some_SP";

secondly, the parameter direction should be
ParameterDirection.ReturnValue,
not output.

Try this, and if it still doesn't work, could you post your SP, or at
least
the SQLe you use to run it, with results showing resultvalue can in fact
no
be 0?

"roundcrisis" <roundcri...@gmail.comwrote in message

news:e7**********************************@d61g200 0hsa.googlegroups.com...
Hi all:
I m trying to get the return value of a stored procedure with
...
DbCommand command = connection.CreateCommand();
command.CommandText = "Exec some_SP";
command.CommandType = CommandType.StoredProcedure;
int temp = command.Parameters.Add(new OdbcParameter("@ReturnValue",
OdbcType.Int, 15));
DbParameter parameter = command.Parameters[temp];
parameter.Direction = ParameterDirection.Output;
command.ExecuteNonQuery();
return command.Parameters["@ReturnValue"].Value.ToString()
...
however the @ReturnValue is _always_ 0 (and i know it shouldn't be)
what s the problem?
is it the way I set up the parameter?- Hide quoted text -

- Show quoted text -

Thanks for your answer
I have tried those changes however it doesn't seem to solve the
problem
I cant show you the code for my SP because I dont use one, the user
can chose from a set of existing working stored procedures
running the above code doesn't throw any exception or anything and i
can see it running in the profiler with "exec " included
I tested without exec and runs as well without exceptions wich I find
very strange

Also, I wonder why it was the only way to create a parameter using an
odbcparameter

Cheers
Dec 5 '07 #4
"roundcrisis" <ro*********@gmail.comwrote in message
news:23**********************************@w40g2000 hsb.googlegroups.com...
I have tried those changes however it doesn't seem to solve the
problem
[...]
running the above code doesn't throw any exception or anything
Your sample does not show any Openning or Closing of the Connection, but
I assume that it is somewhere in your code. Verify that you are reading the
return parameter *before* closing the connection. If you read it after doing
the Close(), it doesn't throw any exception or anything, it just returns
zero all the time.

Dec 5 '07 #5
On Dec 5, 6:30 pm, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.orgwrote:
"roundcrisis" <roundcri...@gmail.comwrote in message

news:23**********************************@w40g2000 hsb.googlegroups.com...
I have tried those changes however it doesn't seem to solve the
problem
[...]
running the above code doesn't throw any exception or anything

Your sample does not show any Openning or Closing of the Connection, but
I assume that it is somewhere in your code. Verify that you are reading the
return parameter *before* closing the connection. If you read it after doing
the Close(), it doesn't throw any exception or anything, it just returns
zero all the time.
Hi alberto: I m absolutely certain that the connection is open at the
time of trying to read the parameter value
actually at run time just after I run the executenonquery i can see
that the parameter value is empty

I cant actually change the type of client at the moment
Dec 5 '07 #6
On Dec 5, 6:43 pm, roundcrisis <roundcri...@gmail.comwrote:
On Dec 5, 6:30 pm, "Alberto Poblacion" <earthling-

quitaestoparacontes...@poblacion.orgwrote:
"roundcrisis" <roundcri...@gmail.comwrote in message
news:23**********************************@w40g2000 hsb.googlegroups.com...
I have tried those changes however it doesn't seem to solve the
problem
[...]
running the above code doesn't throw any exception or anything
Your sample does not show any Openning or Closing of the Connection, but
I assume that it is somewhere in your code. Verify that you are reading the
return parameter *before* closing the connection. If you read it after doing
the Close(), it doesn't throw any exception or anything, it just returns
zero all the time.

Hi alberto: I m absolutely certain that the connection is open at the
time of trying to read the parameter value
actually at run time just after I run the executenonquery i can see
that the parameter value is empty

I cant actually change the type of client at the moment
rite so i tried this

IDbCommand command = connection.CreateCommand();
IDbDataParameter parameter = command.CreateParameter();
parameter.ParameterName = "ReturnValue";
parameter.Direction =
ParameterDirection.ReturnValue;
parameter.DbType = DbType.Int32;
command.Parameters.Add(parameter);

command.CommandText = storedProcedureName;
command.CommandType = CommandType.StoredProcedure;

command.ExecuteNonQuery();
insertValue =
((IDbDataParameter)command.Parameters[0]).Value;

and still no return value and the sp i m runing (as a test ) is a sp
that simply returns a value
???
Dec 6 '07 #7

and still no return value and the sp i m runing (as a test ) is a sp
that simply returns a value
???
Right, please provide the SQL for the stored procedure.
Is it a SQL Server database that you'll always be connecting to?
Dec 6 '07 #8
On Dec 6, 5:15 pm, "Dan Bass" <danb...@REMOVEblueCAPSbottle.com>
wrote:
and still no return value and the sp i m runing (as a test ) is a sp
that simply returns a value
???

Right, please provide the SQL for the stored procedure.
Is it a SQL Server database that you'll always be connecting to?
ok, the problem lied on the fact that i was using an odbc connection
so the command text has to be

{ ? = CALL sp_Name }

what a nightmare anyway there you go
Dec 7 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael | last post by:
This one's really got me. I have a VB.NET (version 1.1.4322) project that provides an easy way to execute stored procedures on a generic level. When I run the code on computer A (running SQL...
0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
2
by: Mike P | last post by:
I have a SQL Server stored procedure that is returning a 0 if the execution was OK and 1 if not. e.g. : commit trans return 0 on_error: rollback tran
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
6
by: David Lozzi | last post by:
Here is the proc: CREATE PROCEDURE . @CID as int, @Netname as nvarchar(25), @Return as int OUTPUT AS IF EXISTS (SELECT DISTINCT netname FROM computers WHERE CompanyID = @CID AND...
1
by: j090757 | last post by:
Returning parm data to vb.net from AS400 stored procedure This example loads a textbox which is used by javascript for error handling. First create the stored procedure on the AS400: CREATE...
4
by: scparker | last post by:
Hello, We have a stored procedure that does a basic insert of values. I am then able to retrieve the ID number created for this new record. We are currently using ASP.NET 2.0 and use N-Tier...
9
by: fniles | last post by:
I am using VB.NET 2003 and SQL2000 database. I have a stored procedure called "INSERT_INTO_MYTABLE" that accepts 1 parameter (varchar(10)) and returns the identity column value from that table....
2
by: jed | last post by:
I have created this example in sqlexpress ALTER PROCEDURE . @annualtax FLOAT AS BEGIN SELECT begin1,end1,deductedamount,pecentageextra FROM tax
3
by: bogdan | last post by:
Hi, I have a stored procedure that returns a single value. Example: SELECT @RowCount = COUNT(*) FROM t WHERE RETURN @RowCount I created a data set, table adapter, and adapter's method...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.