newbie question :Get return from SQL server

  • Thread starter Thread starter Danny Ni
  • Start date Start date
D

Danny Ni

Hi,

I used SqlCommand ExecuteNonQuery method to execute a stored procedure in
SQL server, the SP returned me a value, how do I read this result in C#?
ExecuteNonQuery method does not seem to return anything.

TIA
 
How is the SP returning the value? In a SELECT, RETURN value or OUTPUT
parameter? Except for the SELECT, you need to setup one or more Parameters
and set the appropriate direction. If a SELECT, you can't use
ExecuteNonQuery. I explain how to do all of this in my book on ADO.NET for
C# Programmers.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
It's a select. Actually this SP inserts a record into a table and return
identity by using: select @@identity.
 
As Bill mentions, if it is a SELECT yow will not get results using
ExecuteNonQuery(). Use ExecuteScalar() or ExecuteReader() instead.
HTH,
Sushil.
 
Danny,

Create a parameter for your SqlCommand with Direction set to
ParameterDirection.ReturnValue. After executing your stored procedure,
close the connection, then the value of your parameter will be available.
There are plenty of examples in the framework documentation on how to create
parameters, but what confuses a lot of new ADO and ADO.Net developers is
that you have to close the connection before the values are available to be
read.

Even in the framework documentation, there are a couple examples that show
that incorrectly, that is implying you can get the return value without
closing the connection.

Dale
 
Back
Top