|
-
Jun 7th, 2008, 07:52 AM
#1
Thread Starter
Frenzied Member
Global variable to be inserted as parameter [RESOLVED]
hey,
Im trying to use stored procedures for the first time so please bear with me.
My stored procedure compiles from the SQL Server Studio Manager so it should be fine. Now with this procedure Im trying to insert a record and get its Primary key which is an auto incremented one.
CREATE PROCEDURE dbo.AddNewDelivery
-- Add the parameters for the stored procedure here
@schoolFID int,
@bookFID int,
@qty int,
@deliveryDate smalldatetime,
@userFID int,
@deliveryID int OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT INTO tb_delivery (schoolFID, bookFID, qty, deliveryDate, userFID)
VALUES (@schoolFID, @bookFID, @qty, @deliveryDate, @userFID)
SET @deliveryID = Scope_Identity()
END
My VB.NET error states that it expects @userId as a parameter that is not supplied. I am declaring it global and giving it a value within the function. So what's the mistake?? Here the code for the insert command
Code:
Public userId as integer
--------------------------------------
Private Sub NewDelivery()
Dim query As String = "AddNewdelivery"
Dim deliveryID As Integer
userId = 2
Dim deliveryboolean As Boolean
Try
Using cmd As New SqlCommand(query, MyConn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@schoolFID", cboSchD.SelectedValue)
cmd.Parameters.AddWithValue("@bookFID", cboBookD.SelectedValue)
cmd.Parameters.AddWithValue("@qty", txtQtyD.Text)
cmd.Parameters.AddWithValue("@deliveryDate", dtpDelivery.Value)
cmd.Parameters.AddWithValue("@userID", userId)
MyConn.Open()
cmd.ExecuteNonQuery()
deliveryID = cmd.Parameters("@deliveryID").Value
ShowScopeIdentity.Text = deliveryID.ToString
deliveryboolean = True
End Using
Catch ex As Exception
MessageBox.Show(ex.ToString)
deliveryboolean = False
Finally
If MyConn.State = ConnectionState.Open Then
MyConn.Close()
Me.Dispose()
End If
End Try
End Sub
Is this the right way to get the Scope Identity (PK) into a textbox(a variable later on ) so I can insert in another table.
Last edited by angelica; Jun 7th, 2008 at 02:28 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|