Saturday, February 25, 2012

Retrieve output parameter from stored procedure

Hi,

I'm having problems retrieving the output parameter from my stored procedure, i'm getting the following error

An SqlParameter with ParameterName '@.slideshowid' is not contained by this SqlParameterCollection

code as follows:

int publicationId = (int) Session["PublicationId"];
string title = txtTitle.Text;
int categoryid = Convert.ToInt32(dlCategory.SelectedItem.Value);
SqlConnection myConnection =new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand =new SqlCommand("sp_be_addSlideshow", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add(new SqlParameter("@.publicationid", SqlDbType.Int));
myCommand.Parameters["@.publicationid"].Value = publicationId;
myCommand.Parameters.Add(new SqlParameter("@.title", SqlDbType.NVarChar));
myCommand.Parameters["@.title"].Value = title;
myCommand.Parameters.Add(new SqlParameter("@.categoryid", SqlDbType.Int));
myCommand.Parameters["@.categoryid"].Value = categoryid;
myConnection.Open();
myCommand.ExecuteNonQuery();
string slideshowId = myCommand.Parameters["@.slideshowid"].Value.ToString();
myConnection.Close();

my stored procedure:

CREATE PROCEDURE sp_be_addSlideshow
(
@.publicationid int,
@.title nvarchar(50),
@.categoryid int,
@.slideshowid int = NULL OUTPUT
)
AS

INSERT INTO
Slideshow(publicationid,title,slideshowcategoryid,deleted)

VALUES
(@.publicationid,@.title,@.categoryid,0)

SELECT @.slideshowid = SCOPE_IDENTITY()
GO

Supergirl_gem,

You need to specify to your command that that parameter is an output param. Try this.

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

Then after the execute, you can set the id

string slideshowId = myCommand.Parameters["@.slideshowid"].Value.ToString();

Hope this helps. If you are still having problems. Please let me know.

Regards.

No comments:

Post a Comment