?
Solved

Get Parameter from Stored Procedure in my code behind

Posted on 2012-03-31
6
Medium Priority
?
319 Views
Last Modified: 2012-04-01
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

0
Comment
Question by:rcowen00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 9

Accepted Solution

by:
OCDan earned 2000 total points
ID: 37791714
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
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 37791735
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
 
LVL 16

Expert Comment

by:MikeMCSD
ID: 37791811
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:rcowen00
ID: 37791843
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
 
LVL 9

Expert Comment

by:OCDan
ID: 37791859
I can't see where you have added a variable to accept the parameter return value?
0
 

Author Comment

by:rcowen00
ID: 37792126
I'm not sure of the syntax can you tell me?
0

Featured Post

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question