Get Parameter from Stored Procedure in my code behind

have a stored procedure that inserts new records into my table.  I would like to get the  @PropertyKey assigned to the record and use it in my code behind.  How do I do that?


ALTER PROCEDURE dbo.ManualOrder( 
									@LoanNumber VarChar(20),
									@ClientRefNum VarChar(50),
									@CaseNumber VarChar(50),
									@LegalDesc VarChar(Max),
									@LocTypeID Int,
									@PropTypeID Int,
									@Address VarChar(50),
									@Address2 VarChar(50),
									@City VarChar(50),
									@StateID Char (2),
									@County VarChar(50),
									@Zip Char(5),
									@ReportTypeID int,
									@ClientID int,
									@IntendedUseID int,
									@OccupancyID int,
									@LoanTypeID int,
									@RushKey Int)
AS
	BEGIN
	SET NOCOUNT ON;

	DECLARE @PropertyKey int

	INSERT INTO dbo.Property(PropAddr1,PropAddr2,PropCity,PropZip,PropCounty,ModifiedUser,CreatedUser,PropStateKey,PropTypeKey,LocationTypeKey,LegalDescription)
		SELECT        @Address, @Address2 , @City , @Zip , @County , @UserKey , @UserKey , StateKey , 
		                         @PropTypeID, @LocTypeId ,@LegalDesc
		FROM            State
		WHERE        (StateAbbr = @StateID)

			SELECT @PropertyKey = scope_identity()
END
			

Open in new window

rcowen00Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OCDanCommented:
Amend the procedure to this:
ALTER PROCEDURE dbo.ManualOrder
(
@LoanNumber VarChar(20),
@ClientRefNum VarChar(50),
@CaseNumber VarChar(50),
@LegalDesc VarChar(Max),
@LocTypeID Int,
@PropTypeID Int,
@Address VarChar(50),
@Address2 VarChar(50),
@City VarChar(50),
@StateID Char (2),
@County VarChar(50),
@Zip Char(5),
@ReportTypeID int,
@userkey int,
@ClientID int,
@IntendedUseID int,
@OccupancyID int,
@LoanTypeID int,
@RushKey Int,
@out int OUT
)
AS
      BEGIN
      SET NOCOUNT ON;

      DECLARE @PropertyKey int

      INSERT INTO dbo.Property(PropAddr1,PropAddr2,PropCity,PropZip,PropCounty,ModifiedUser,CreatedUser,PropStateKey,PropTypeKey,LocationTypeKey,LegalDescription)
            SELECT        @Address, @Address2 , @City , @Zip , @County , @UserKey , @UserKey , StateKey ,
                                     @PropTypeID, @LocTypeId ,@LegalDesc
            FROM            State
            WHERE        (StateAbbr = @StateID)

                  SELECT @PropertyKey = scope_identity()
                  set @out = @PropertyKey
                  RETURN @out
END

Then when you call the procedure from your program you just specify another parameter in its execution e.g.

declare @localvariable int
exec ManualOrder @param1,@param2,.................................,@out = @localvariable OUTPUT
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Daniel Van Der WerkenIndependent ConsultantCommented:
I'm not sure, but I think you're asking how to execute a stored procedure from the VB .NET code-behind. Here is an article that can lead you to the correct method:
http://support.microsoft.com/kb/306574

This one might be a bit more helpful for getting return values:
http://www.macronimous.com/resources/calling_stored_procedures_from_ASP.NET_and_VB.NET.asp
0
MikeMCSDCommented:
I have it in C#, but this works :

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            using (var conn = new SqlConnection(connectionString))
            using (var command = new SqlCommand("Your_Stored_Procedure", conn) { CommandType = CommandType.StoredProcedure })
            {
                SqlParameter paramReturnValue = new SqlParameter();
                paramReturnValue.ParameterName = "@RETURN_VALUE";
                paramReturnValue.SqlDbType = SqlDbType.Int;
                paramReturnValue.Direction = ParameterDirection.ReturnValue;
                command.Parameters.Add(paramReturnValue);

                conn.Open();
                command.ExecuteNonQuery();
                conn.Close();

                int returnValue = (int)command.Parameters["@RETURN_VALUE"].Value;
                ...........
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

rcowen00Author Commented:
OCDan - I am getting "Procedure or function 'ManualOrder' expects parameter '@OPK', which was not supplied." in my code behind

Private Sub ManualOrderUpload()
        Dim cnn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("myprovalConnectionString").ConnectionString)
        Try
            Dim cmd As New System.Data.SqlClient.SqlCommand
            cmd.Connection = cnn
            cmd.CommandType = Data.CommandType.StoredProcedure
            cmd.CommandText = "dbo.ManualOrder"
            
            cmd.Parameters.Add("@LoanNumber ", Data.SqlDbType.Char).Value = txtLoanNumber.Text
            cmd.Parameters.Add("@ClientRefNum ", Data.SqlDbType.Char).Value = txtClientRefNum.Text
            cmd.Parameters.Add("@CaseNumber ", Data.SqlDbType.Char).Value = txtCaseNumber.Text
            cmd.Parameters.Add("@LegalDesc ", Data.SqlDbType.Char).Value = txtLegalDesc.Text
            cmd.Parameters.Add("@LocTypeID ", Data.SqlDbType.Int).Value = IIf(txtLocType.SelectedValue = "0", DBNull.Value, Int32.Parse(txtLocType.SelectedItem.Value))
            cmd.Parameters.Add("@PropTypeID ", Data.SqlDbType.Int).Value = Int32.Parse(txtPropertyType.SelectedItem.Value)
            cmd.Parameters.Add("@Address ", Data.SqlDbType.Char).Value = txtAddress.Text
            cmd.Parameters.Add("@Address2 ", Data.SqlDbType.Char).Value = txtAddress2.Text
            cmd.Parameters.Add("@City ", Data.SqlDbType.Char).Value = txtCity.Text
            cmd.Parameters.Add("@StateID ", Data.SqlDbType.Char).Value = StateID.SelectedItem.Value
            cmd.Parameters.Add("@RushKey ", Data.SqlDbType.Char).Value = ddRushType.SelectedItem.Value
            cmd.Parameters.Add("@County ", Data.SqlDbType.Char).Value = txtCounty.Text
            cmd.Parameters.Add("@Zip ", Data.SqlDbType.Char).Value = txtZip.Text
            cmd.Parameters.Add("@ReportTypeID ", Data.SqlDbType.Int).Value = Int32.Parse(ReportTypeID.SelectedItem.Value)
            cmd.Parameters.Add("@ClientID ", Data.SqlDbType.Int).Value = Int32.Parse(ClientID.SelectedItem.Value)
            cmd.Parameters.Add("@IntendedUseID ", Data.SqlDbType.Int).Value = IIf(IntendedUseID.SelectedValue = "0", DBNull.Value, Int32.Parse(IntendedUseID.SelectedItem.Value))
            cmd.Parameters.Add("@OccupancyID ", Data.SqlDbType.Int).Value = IIf(OccupancyID.SelectedValue = "0", DBNull.Value, Int32.Parse(OccupancyID.SelectedItem.Value))
            cmd.Parameters.Add("@LoanTypeID ", Data.SqlDbType.Int).Value = IIf(LoanTypeID.SelectedValue = "0", DBNull.Value, Int32.Parse(LoanTypeID.SelectedItem.Value))
            cmd.Parameters.Add("@BorrFirst ", Data.SqlDbType.Char).Value = IIf(txtBorrFirst.Text = "", DBNull.Value, txtBorrFirst.Text)
            cmd.Parameters.Add("@BorrLast ", Data.SqlDbType.Char).Value = IIf(txtBorrLast.Text = "", DBNull.Value, txtBorrLast.Text)
            cmd.Parameters.Add("@BorrHomeArea", Data.SqlDbType.Char).Value = IIf(txtBorrHomeArea.Text = "", DBNull.Value, txtBorrHomeArea.Text)
            cmd.Parameters.Add("@BorrHomePrefix ", Data.SqlDbType.Char).Value = IIf(txtBorrHomePrefix.Text = "", DBNull.Value, txtBorrHomePrefix.Text)
            cmd.Parameters.Add("@BorrHomeSuffix ", Data.SqlDbType.Char).Value = IIf(txtBorrHomeSuffix.Text = "", DBNull.Value, txtBorrHomeSuffix.Text)
            cmd.Parameters.Add("@BorrWorkArea", Data.SqlDbType.Char).Value = IIf(txtBorrWorkArea.Text = "", DBNull.Value, txtBorrWorkArea.Text)
            cmd.Parameters.Add("@BorrWorkPrefix ", Data.SqlDbType.Char).Value = IIf(txtBorrWorkPrefix.Text = "", DBNull.Value, txtBorrWorkPrefix.Text)
            cmd.Parameters.Add("@BorrWorkSuffix ", Data.SqlDbType.Char).Value = IIf(txtBorrWorkSuffix.Text = "", DBNull.Value, txtBorrWorkSuffix.Text)
            cmd.Parameters.Add("@BorrCellArea", Data.SqlDbType.Char).Value = IIf(txtBorrCellArea.Text = "", DBNull.Value, txtBorrCellArea.Text)
            cmd.Parameters.Add("@BorrCellPrefix ", Data.SqlDbType.Char).Value = IIf(txtBorrCellPrefix.Text = "", DBNull.Value, txtBorrCellPrefix.Text)
            cmd.Parameters.Add("@BorrCellSuffix ", Data.SqlDbType.Char).Value = IIf(txtBorrCellSuffix.Text = "", DBNull.Value, txtBorrCellSuffix.Text)
            cmd.Parameters.Add("@BorrEmail ", Data.SqlDbType.Char).Value = IIf(txtBorrEmail.Text = "", DBNull.Value, txtBorrEmail.Text)
            cmd.Parameters.Add("@ContHomeArea", Data.SqlDbType.Char).Value = IIf(txtContHomeArea.Text = "", DBNull.Value, txtContHomeArea.Text)
            cmd.Parameters.Add("@ContHomePrefix ", Data.SqlDbType.Char).Value = IIf(txtContHomePrefix.Text = "", DBNull.Value, txtContHomePrefix.Text)
            cmd.Parameters.Add("@ContHomeSuffix ", Data.SqlDbType.Char).Value = IIf(txtContHomeSuffix.Text = "", DBNull.Value, txtContHomeSuffix.Text)
            cmd.Parameters.Add("@ContWorkArea", Data.SqlDbType.Char).Value = IIf(txtContWorkArea.Text = "", DBNull.Value, txtContWorkArea.Text)
            cmd.Parameters.Add("@ContWorkPrefix ", Data.SqlDbType.Char).Value = IIf(txtContWorkPrefix.Text = "", DBNull.Value, txtContWorkPrefix.Text)
            cmd.Parameters.Add("@ContWorkSuffix ", Data.SqlDbType.Char).Value = IIf(txtContWorkSuffix.Text = "", DBNull.Value, txtContWorkSuffix.Text)
            cmd.Parameters.Add("@ContCellArea", Data.SqlDbType.Char).Value = IIf(txtContCellArea.Text = "", DBNull.Value, txtContCellArea.Text)
            cmd.Parameters.Add("@ContCellPrefix ", Data.SqlDbType.Char).Value = IIf(txtContCellPrefix.Text = "", DBNull.Value, txtContCellPrefix.Text)
            cmd.Parameters.Add("@ContCellSuffix ", Data.SqlDbType.Char).Value = IIf(txtContCellSuffix.Text = "", DBNull.Value, txtContCellSuffix.Text)
            cmd.Parameters.Add("@ContLast ", Data.SqlDbType.Char).Value = IIf(txtContLast.Text = "", DBNull.Value, txtContLast.Text)
            cmd.Parameters.Add("@ContFirst ", Data.SqlDbType.Char).Value = IIf(txtContFirst.Text = "", DBNull.Value, txtContFirst.Text)
            cmd.Parameters.Add("@ContEmail ", Data.SqlDbType.Char).Value = IIf(txtContEmail.Text = "", DBNull.Value, txtContEmail.Text)
            cmd.Parameters.Add("@ContRelationID ", Data.SqlDbType.Int).Value = IIf(ContRelationID.SelectedValue = "0", DBNull.Value, Int32.Parse(ContRelationID.SelectedItem.Value))
            cmd.Parameters.Add("@LoanAmount ", Data.SqlDbType.Decimal).Value = IIf(intLoanAmount.Text = "", DBNull.Value, intLoanAmount.Text)

            Dim iUserId As New Guid(Membership.GetUser(My.User.Name).ProviderUserKey.ToString())
            cmd.Parameters.Add("@UserKey", Data.SqlDbType.VarChar).Value = iUserId.ToString

            cnn.Open()
            Dim dr As Data.SqlClient.SqlDataReader = cmd.ExecuteReader
            dr.Read()
        Finally
            cnn.Close()
            lblSuccessfullyImport.Visible = True
        End Try
    End Sub

Open in new window

0
OCDanCommented:
I can't see where you have added a variable to accept the parameter return value?
0
rcowen00Author Commented:
I'm not sure of the syntax can you tell me?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.