简体   繁体   中英

Error in retrieving the value of the output parameter of the stored procedure

I have a stored procedure defined as follows to check the existence of the duplicate Email:

 USE [SQL2008_850994_onebizness]
 GO
 /****** Object:  StoredProcedure [dbo].[EmailExists]    Script Date: 04/07/2013 15:14:22 ******/
 SET ANSI_NULLS ON
 GO
 SET QUOTED_IDENTIFIER ON
 GO

 ALTER PROCEDURE [dbo].[EmailExists]
 @EmailID Nvarchar(50),
 @Success int output, 
 @msg varchar(50) output  

 AS
 BEGIN
 IF EXISTS(SELECT 1 FROM dbo.Membership WHERE EmailId = @emailID) 
     OR EXISTS(SELECT 1 FROM dbo.Allocation where ResourceEmail=@emailID)
 begin
 set @Success=6
 set @msg='Duplicate Email found. Please try again.'      
 --Insert the records in the database
 end
 END

I am retrieving the value of the success in a C# code as follows:

 int returnVal = int.Parse(myCOmmand.Parameters["@Success"].ToString());

I am getting the error message "Input values are not in the correct format".

Can someone please let me know what is causing the error?

Have you set the Parameters as below

myCommand.Parameters.Add(new SqlParameter("@Success", SqlDbType.Int));
myCommand.Parameters["@Success"].Direction = ParameterDirection.Output;

Then retrieve as

int returnVal = (int)myCommand.Parameters["@Success"].Value;

You need to add the below code before you write the int returnVal

myCOmmand.Parameters.Add("@Success", SqlDbType.Int).Direction = ParameterDirection.Output;

And then:

int returnVal = Convert.ToInt32(myCOmmand.Parameters["@Success"].Value);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM