Not getting the return value from Stored Procedure.

Enclosed is the stored procedure:

setANSI_NULLSON

setQUOTED_IDENTIFIERON

GO

ALTERPROCEDURE [dbo].[InsertModule]

(

@Modulenvarchar(MAX)

)

AS

Begin

SetNOCOUNTON

ifexists(Select 1From ModulesWhere Module=@Module)

Begin

return-1

End

ELSE

Begin

INSERTINTO [dbo].[Modules]([Module])VALUES(@Module)

RETURN 0

End

END

This is the calling program:

privatevoid SubmitButton_Click(object sender,EventArgs e)

{

sqlConnection1.Open();

if (Module.Length > 0)

{

SqlCommand cmdModule =newSqlCommand("InsertModule",sqlConnection1);

cmdModule.CommandType =CommandType.StoredProcedure;

SqlParameter parInput = cmdModule.Parameters.Add("@Module",SqlDbType.NVarChar);

parInput.Direction =ParameterDirection.Input;

parInput.Value = Module;

cmdModule.Parameters.Add("@RetVal",SqlDbType.Int,1);

cmdModule.Parameters["@RetVal"].Direction =ParameterDirection.ReturnValue;

cmdModule.ExecuteNonQuery();

int code = (int)cmdModule.Parameters["@RetVal"].Value;

if (code == 0)

{

label8.Text ="Module Created Successfully";

}

else

{

System.Windows.Forms.MessageBox.Show("Create new module Not Successful!");

}

}

else

{

label8.Text ="Module can't be blank";

}

Module ="";

ModuleNameTextBox.Text =" ";

label8.Text =" ";

sqlConnection1.Close();

}

I can insert the data, but I can't show the sucess message for some unknown reason.

Thanks for the help.

[5875 byte] By [HKEC] at [2007-12-25]
# 1
?

The return value is not sent to the client until

the connection is closed -- close the connection right after doing the

ExecuteNonQuery() and you should be all set.


--


Adam Machanic
Pro SQL Server 2005, available now
href="http://www.apress.com/book/bookDisplay.html@bID=457">http://www..apress.com/book/bookDisplay.html?bID=457
--

style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">

< href="mailto:HKEC@discussions.microsoft.com">HKEC@discussions.microsoft..com>

wrote in message href="news_3A8c2abf72-7c26-42de-a07b-c89bf1eceed8_40discussions.microsoft.com">news:8c2abf72-7c26-42de-a07b-c89bf1eceed8@discussions.microsoft.com...

Enclosed is the stored procedure:

set size=2>ANSI_NULLS size=2>ON

set color=#0000ff size=2>QUOTED_IDENTIFIER color=#0000ff size=2>ON

GO

ALTER size=2>PROCEDURE [dbo] size=2>.[InsertModule]

size=2>

(

@Module nvarchar color=#808080 size=2>(MAX color=#808080 size=2>)

)

AS

Begin

Set color=#0000ff size=2>NOCOUNT size=2>ON

if color=#808080 size=2>exists size=2>(Select 1

From Modules

Where Module color=#808080 size=2>=@Module size=2>)

Begin

return color=#808080 size=2>-1

End

ELSE

Begin

INSERT color=#0000ff size=2>INTO [dbo] size=2>.[Modules] size=2>([Module] size=2>) size=2>VALUES size=2>(@Module size=2>)

RETURN 0

End

END

This is the calling program:

private size=2>void SubmitButton_Click( color=#0000ff size=2>object sender,

EventArgs color=#000000> e)

{

sqlConnection1.Open();

if (Module.Length

> 0)

{

SqlCommand cmdModule

= new color=#008080 size=2>SqlCommand( size=2>"InsertModule",sqlConnection1);

cmdModule.CommandType = size=2>CommandType.StoredProcedure;

SqlParameter parInput

= cmdModule.Parameters.Add( size=2>"@Module", size=2>SqlDbType.NVarChar);

parInput.Direction = size=2>ParameterDirection.Input;

parInput.Value = Module;

cmdModule.Parameters.Add( size=2>"@RetVal", size=2>SqlDbType.Int,1);

cmdModule.Parameters[ size=2>"@RetVal"].Direction = size=2>ParameterDirection.ReturnValue;

cmdModule.ExecuteNonQuery();

int code =

(int size=2>)cmdModule.Parameters[ size=2>"@RetVal"].Value;

if (code == 0)

{

label8.Text = "Module Created

Successfully";

}

else

{

System.Windows.Forms. size=2>MessageBox.Show( size=2>"Create new module Not Successful!");

}

}

else

{

label8.Text = "Module can't be

blank";

}

Module = "";

ModuleNameTextBox.Text = " " size=2>;

label8.Text = " "size=2>;

sqlConnection1.Close();

}

I can insert the data, but I can't show the sucess message for some unknown

reason.

Thanks for the help.

MVPUser at 2007-9-3 > top of Msdn Tech,SQL Server,Transact-SQL...
# 2
Thanks for the help, but it doesn't work. It inserted the data and that was it. It did give me error message from the Messagebox if I enter the duplicate.
HKEC at 2007-9-3 > top of Msdn Tech,SQL Server,Transact-SQL...
# 4
I still have not the not getting the return value from a stored procedure. Do you know where I can get some sample code?
HKEC at 2007-9-3 > top of Msdn Tech,SQL Server,Transact-SQL...
# 5
You realize that your SP logic is incorrect right! If the SP gets called concurrently you will get errors depending on which rows are involved and so on. Also you are not performing the appropriate locking or using the correct transaction isolation level. So you may want to correct that.
# 6
Multiusers access is not applicable in this case. I just want to get back the return value correctly. No one seems to have an answer to this problem.
HKEC at 2007-9-3 > top of Msdn Tech,SQL Server,Transact-SQL...
# 7
Why are you assigning a value to your return value ?

//cmdModule.Parameters.Add("@RetVal", SqlDbType.Int,1);
cmdModule.Parameters.Add("@RetVal", SqlDbType.Int);

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

JensK.Suessmeyer at 2007-9-3 > top of Msdn Tech,SQL Server,Transact-SQL...
# 8

You must add a SqlParameter like this to your command object:

SqlParameter returnValue = new SqlParameter("@RETURN_VALUE", SqlDbType.Int);

returnValue.Direction = ParameterDirection.ReturnValue;

After executing the command with e.g. ExecuteNonQuery(); you can get the return value of the stored procedure with

int returnV = (int)returnValue.Value;

Best regards

.netexpert at 2007-9-3 > top of Msdn Tech,SQL Server,Transact-SQL...

SQL Server

Site Classified