PC Review


Reply
Thread Tools Rate Thread

Cannot get OUTPUT parameter after running Sql stored procedure

 
 
MS
Guest
Posts: n/a
 
      30th Jan 2005
Here's my simple stored procedure:

ALTER PROCEDURE GetMemberIDByEmail
@Email EmailAddress,
@ID int OUTPUT
AS
SELECT @ID = ID FROM tbl_Member WHERE Email=@Email
RETURN


Here's my C# code (using Microsoft Data


public int GetMemberIdByEmail(string email)
{
SqlParameter[] aryParams = new SqlParameter[2];

aryParams[0] = new SqlParameter("@Email", email);
aryParams[1] = new SqlParameter("@ID", SqlDbType.Int);
aryParams[1].Direction = ParameterDirection.Output;

try
{
SqlHelper.ExecuteNonQuery(_Connection, "GetMemberIDByEmail", aryParams);
}
catch (System.Data.SqlClient.SqlException)
{
return 0;
}

if (aryParams[1].Value == null)
{
Debug.WriteLine("Still NULL!"); <-- ALWAYS GET THIS!!
}
else
{
Debug.WriteLine("Got it!");
}

return 0;
}


The value of the output parameter is ALWAYS null! I've spent hours trying to
fix this but don't see what is wrong. The stored procedure works great in
Query Analyzer. Can anyone point out what I'm missing? Thanks!

Can anyone


 
Reply With Quote
 
 
 
 
Jim Hughes
Guest
Posts: n/a
 
      30th Jan 2005
Add a Trace.Writeline(ex.message)

catch (System.Data.SqlClient.SqlException ex)
{
System.Diagnostics.Trace.Writeline(ex.message)
return 0;
}
catch (Exception ex) // catch other errors
{
System.Diagnostics.Trace.Writeline(ex.message)
}


"MS" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Here's my simple stored procedure:
>
> ALTER PROCEDURE GetMemberIDByEmail
> @Email EmailAddress,
> @ID int OUTPUT
> AS
> SELECT @ID = ID FROM tbl_Member WHERE Email=@Email
> RETURN
>
>
> Here's my C# code (using Microsoft Data
>
>
> public int GetMemberIdByEmail(string email)
> {
> SqlParameter[] aryParams = new SqlParameter[2];
>
> aryParams[0] = new SqlParameter("@Email", email);
> aryParams[1] = new SqlParameter("@ID", SqlDbType.Int);
> aryParams[1].Direction = ParameterDirection.Output;
>
> try
> {
> SqlHelper.ExecuteNonQuery(_Connection, "GetMemberIDByEmail",
> aryParams);
> }
> catch (System.Data.SqlClient.SqlException)
> {
> return 0;
> }
>
> if (aryParams[1].Value == null)
> {
> Debug.WriteLine("Still NULL!"); <-- ALWAYS GET THIS!!
> }
> else
> {
> Debug.WriteLine("Got it!");
> }
>
> return 0;
> }
>
>
> The value of the output parameter is ALWAYS null! I've spent hours trying
> to fix this but don't see what is wrong. The stored procedure works great
> in Query Analyzer. Can anyone point out what I'm missing? Thanks!
>
> Can anyone
>



 
Reply With Quote
 
=?Utf-8?B?R2FicmllbGU=?=
Guest
Posts: n/a
 
      30th Jan 2005
I think that this code is simply but ok!

public void method()
{
SqlConnection mainConn = new SqlConnection();
mainConn.ConnectionString = "user id=sa;password=pippo;initial
catalog=myDB;data source=(local)";

if (mainConn.State == ConnectionState.Closed)
mainConn.Open();

try
{
SqlCommand myCmd = new SqlCommand("storedProc",mainConn);
myCmd.CommandType = CommandType.StoredProcedure;
SqlParameter ret = new SqlParameter("@RETURN", SqlDbType.Int);
ret.Direction = ParameterDirection.ReturnValue;
myCmd.Parameters["@parameters"].Value = ValueOfParameters;
myCmd.ExecuteNonQuery();

int var = (int)ret.Value;
}
catch (Exception ex)
{
MessageBox.Show("" + ex.Message);
}
finally
{
mainConn.Close();
}
}

I use this statemet when i must use stored procedure!
I hope that this code is well for you!

Good bye

"MS" wrote:

> Here's my simple stored procedure:
>
> ALTER PROCEDURE GetMemberIDByEmail
> @Email EmailAddress,
> @ID int OUTPUT
> AS
> SELECT @ID = ID FROM tbl_Member WHERE Email=@Email
> RETURN
>
>
> Here's my C# code (using Microsoft Data
>
>
> public int GetMemberIdByEmail(string email)
> {
> SqlParameter[] aryParams = new SqlParameter[2];
>
> aryParams[0] = new SqlParameter("@Email", email);
> aryParams[1] = new SqlParameter("@ID", SqlDbType.Int);
> aryParams[1].Direction = ParameterDirection.Output;
>
> try
> {
> SqlHelper.ExecuteNonQuery(_Connection, "GetMemberIDByEmail", aryParams);
> }
> catch (System.Data.SqlClient.SqlException)
> {
> return 0;
> }
>
> if (aryParams[1].Value == null)
> {
> Debug.WriteLine("Still NULL!"); <-- ALWAYS GET THIS!!
> }
> else
> {
> Debug.WriteLine("Got it!");
> }
>
> return 0;
> }
>
>
> The value of the output parameter is ALWAYS null! I've spent hours trying to
> fix this but don't see what is wrong. The stored procedure works great in
> Query Analyzer. Can anyone point out what I'm missing? Thanks!
>
> Can anyone
>
>
>

 
Reply With Quote
 
Bob Grommes
Guest
Posts: n/a
 
      30th Jan 2005
I don't think you're actually passing the email value in that you think you
are.

I don't have any experience passing parameters when the defined SQL type is
a user defined type. I assume that in C# you would have to use the actual
underlying SQL type. If that were, say, varchar, your code would look like
this:

aryParams[0] = new
SqlParameter("@Email",SqlDbType.Varchar,email.Length).Value = email;

Give that a try.

--Bob

"MS" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Here's my simple stored procedure:
>
> ALTER PROCEDURE GetMemberIDByEmail
> @Email EmailAddress,
> @ID int OUTPUT
> AS
> SELECT @ID = ID FROM tbl_Member WHERE Email=@Email
> RETURN
>
>
> Here's my C# code (using Microsoft Data
>
>
> public int GetMemberIdByEmail(string email)
> {
> SqlParameter[] aryParams = new SqlParameter[2];
>
> aryParams[0] = new SqlParameter("@Email", email);
> aryParams[1] = new SqlParameter("@ID", SqlDbType.Int);
> aryParams[1].Direction = ParameterDirection.Output;
>
> try
> {
> SqlHelper.ExecuteNonQuery(_Connection, "GetMemberIDByEmail",
> aryParams);
> }
> catch (System.Data.SqlClient.SqlException)
> {
> return 0;
> }
>
> if (aryParams[1].Value == null)
> {
> Debug.WriteLine("Still NULL!"); <-- ALWAYS GET THIS!!
> }
> else
> {
> Debug.WriteLine("Got it!");
> }
>
> return 0;
> }
>
>
> The value of the output parameter is ALWAYS null! I've spent hours trying
> to fix this but don't see what is wrong. The stored procedure works great
> in Query Analyzer. Can anyone point out what I'm missing? Thanks!
>
> Can anyone



 
Reply With Quote
 
Marc Scheuner [MVP ADSI]
Guest
Posts: n/a
 
      31st Jan 2005
> public int GetMemberIdByEmail(string email)
> {
> SqlParameter[] aryParams = new SqlParameter[2];
>
> aryParams[0] = new SqlParameter("@Email", email);
> aryParams[1] = new SqlParameter("@ID", SqlDbType.Int);
> aryParams[1].Direction = ParameterDirection.Output;
>
> SqlHelper.ExecuteNonQuery(_Connection, "GetMemberIDByEmail", aryParams);
> if (aryParams[1].Value == null)
> {
> Debug.WriteLine("Still NULL!"); <-- ALWAYS GET THIS!!
> }
> else
> {
> Debug.WriteLine("Got it!");
> }


You're using the wrong overload for ExecuteNonQuery - the one that
uses a "params object[]......" array (overload no. 4), not the one
using the SqlParameter[] array! (that would be overload no. 5).

In this case, the ExecuteNonQuery call does not know what kind of
parameters you've passed in, and thus cannot update them upon exiting.

Use this call instead:

SqlHelper.ExecuteNonQuery(_Connection, CommandType.StoredProcedure,
"GetMemberIDByEmail", aryParams);

*THEN* you should be able to access your output parameter.

Marc

================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stored Procedure with Output parameter Phenom Microsoft ADO .NET 13 9th Nov 2005 09:50 PM
Cannot get OUTPUT parameter after running Sql stored procedure MS Microsoft ASP .NET 5 31st Jan 2005 08:09 AM
Cannot get OUTPUT parameter after running Sql stored procedure MS Microsoft C# .NET 5 31st Jan 2005 08:09 AM
Stored procedure parameter output value Steven Microsoft ASP .NET 5 27th May 2004 04:16 AM
Stored procedure parameter output value Steven Microsoft C# .NET 4 27th May 2004 01:28 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:16 PM.