I have the following stored procedure to insert records into two tables:
I also have the following code on a codebehind page.
I get the error:
I know I need to put the parameter @OrderID, how do I declare the parameter that will be the @@identity
Code:
USE [PhoenixSQL]
GO
/****** Object: StoredProcedure [dbo].[AddOrder] Script Date: 04/01/2011 12:20:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[AddOrder]
(
@TraderID int,
@OrderNumber nvarchar(50),
@InvoiceNumber nvarchar(50),
@InvoiceDate datetime,
@Carriage decimal(18,2),
@OrderID int
)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO dbo.Orders (TraderID, OrderNumber, InvoiceNumber, InvoiceDate, Carriage)
VALUES (@TraderID, @OrderNumber, @InvoiceNumber, @InvoiceDate, @Carriage)
SELECT @OrderID = scope_identity()
INSERT INTO dbo.OrderDetails (OrderID)
VALUES (@OrderID)
END
I also have the following code on a codebehind page.
Code:
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim ConnString As [String] = ConfigurationManager.ConnectionStrings("PhoenixSQLConnectionString").ConnectionString
Dim con As New SqlConnection(ConnString)
Dim cmd As New SqlCommand()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "AddOrder"
cmd.Parameters.Add("@TraderID", SqlDbType.VarChar).Value = HiddenTraderID.Value.Trim()
cmd.Parameters.Add("@OrderNumber", SqlDbType.VarChar).Value = txtOrderNumber.Text.Trim()
cmd.Parameters.Add("@InvoiceNumber", SqlDbType.VarChar).Value = txtInvoiceNumber.Text.Trim()
cmd.Parameters.Add("@InvoiceDate", SqlDbType.DateTime).Value = txtInvoiceDate.Text.Trim()
cmd.Parameters.Add("@Carriage", SqlDbType.Decimal).Value = txtCarriage.Text.Trim()
cmd.Connection = con
Try
con.Open()
cmd.ExecuteScalar()
Catch ex As Exception
Throw ex
Finally
con.Close()
con.Dispose()
End Try
End Sub
I get the error:
Code:
Procedure or function 'AddOrder' expects parameter '@OrderID', which was not supplied.
I know I need to put the parameter @OrderID, how do I declare the parameter that will be the @@identity
Code:
cmd.Parameters.Add("@OrderID", SqlDbType.Int).Value = ??