Yagya Sharma
Yagya Sharma

Reputation: 437

retrieving values of the output parameters of a stored procedure

I have a stored procedure that returns an output. The output parameters definition in the stored procedure is as follows:

@Success int output

I am using following statement to retrieve the values from the output parameters. I am getting the error message

"Specified cast is not valid"

I am using Visual Studio 2012.

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

Can you please let me know what could be the problem? And provide the resolution.

Thanks for the assistance. Yagya

Upvotes: 0

Views: 611

Answers (1)

Michiel van Vaardegem
Michiel van Vaardegem

Reputation: 2035

I created a parameter to get the value from after executing, like this (o_id is the parameter name):

IDbDataParameter o_id = cmd.CreateParameter();
o_id.ParameterName = "?o_id";
o_id.DbType = DbType.Int32;
o_id.Direction = ParameterDirection.Output;
cmd.Parameters.Add(o_id);

// Call the Stored Procedure
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText "= MyStoredProcedure";

db.ExecuteNonQuery(cmd);
int o_idValue =  int.Parse(o_id.Value.ToString());

In this case, db is a database layer, which executes my IDbCommand

Problably you can solve your issue with the int.Parse() (or even better, int.TryParse())

Upvotes: 1

Related Questions