Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Saving Records & Caputring Return Value (Enterprise Library)

0.00/5 (No votes)
27 Feb 2010 1  
Stored Procedure should look like thisCREATE PROCEDURE [dbo].[Purchases_Insert]    (@SupplierID     bigint,     @ReferenceNo     NVARCHAR(20),    

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Stored Procedure should look like this

CREATE PROCEDURE [dbo].[Purchases_Insert]
    (@SupplierID     bigint,
     @ReferenceNo     NVARCHAR(20),
     @PurchaseDate     DATETIME,
     @CurrentPurchaseID bigint output)

AS

INSERT INTO [tblPurchases]
     ([SupplierID],
      [PurchaseNo],
      [ReferenceNo],
      [PurchaseDate])
 
VALUES
    (@SupplierID,
     @PurchaseNo,
     @ReferenceNo,
     @PurchaseDate)

SET @CurrentPurchaseID=SCOPE_IDENTITY()



Data Access Layer should look like this.

 Public Sub InsertPurchase()

        Try
           Dim DB As Database = DatabaseFactory.CreateDatabase(CWB_DBCONNECTION_STRING)
            Dim DBC As DbCommand = db.GetStoredProcCommand(PURCHASES_INSERT)
            db.AddInParameter(DBC, "@SupplierID", DbType.Int64, Me.SupplierID)
            db.AddInParameter(DBC, "@ReferenceNo", DbType.String, Me.RefBillNo)
            db.AddInParameter(DBC, "@PurchaseDate", DbType.DateTime, Me.PurchaseDate)
            db.AddOutParameter(DBC,"@CurrentPurchaseID", DbType.Int64, 0)       

            db.ExecuteNonQuery(DBC)

            Me.CurrentPurchaseID = Convert.ToInt64(db.GetParameterValue(DBC, "@CurrentPurchaseID").ToString())
        Catch ex As Exception

            Throw
        End Try

    End Sub

 

CurrentPurchaseID  has new value of identity column.

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here