Hi.
I am wondering how I would use the scope identity created in one SP and use it in another SP directly afterwards. I am trying to add a new registration to the database, get the last RegistrationID from that registration and then use it to add selected locations and registrationid in another table.
My SP for Registrations are below:
My stored procedure for Locations:
My codebehind to add the registration is:
Then my code for AddLocation()
Just need to know how I get the last ID from the registration and where to put it in the AddLocation sub
Thanks
I am wondering how I would use the scope identity created in one SP and use it in another SP directly afterwards. I am trying to add a new registration to the database, get the last RegistrationID from that registration and then use it to add selected locations and registrationid in another table.
My SP for Registrations are below:
Code:
USE [KeyStrategy]
GO
/****** Object: StoredProcedure [dbo].[AddNewRegistration] Script Date: 09/22/2011 22:16:50 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[AddNewRegistration]
(
@Title char(10),
@Firstname varchar(50),
@Surname varchar(50),
@Phone varchar(50),
@OtherPhone varchar(50),
@EmailAddress nvarchar(max),
@TravelDistance numeric(4,0),
@MinDayRate decimal(18,2),
@MaxDayRate decimal(18,2)
)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @RegistrationID Int
INSERT INTO dbo.Registrations(Title, Firstname, Surname, Phone, OtherPhone, EmailAddress, TravelDistance, MinimumDayRate, MaximumDayRate )
VALUES (@Title, @Firstname, @Surname, @Phone, @OtherPhone, @EmailAddress, @TravelDistance, @MinDayRate, @MaxDayRate)
SELECT @RegistrationID = scope_identity()
SELECT @RegistrationID As RegistrationID
END
My stored procedure for Locations:
Code:
USE [KeyStrategy]
GO
/****** Object: StoredProcedure [dbo].[AddLocations] Script Date: 09/22/2011 22:17:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[AddLocations]
(
@RegistrationID int,
@Location varchar(50)
)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO dbo.Locations(RegistrationID, Location )
VALUES (@RegistrationID, @Location)
END
My codebehind to add the registration is:
Code:
Protected Sub btnRegister_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRegister.Click
Dim ConnString As [String] = ConfigurationManager.ConnectionStrings("KeyStrategyConnectionString").ConnectionString
Dim con As New SqlConnection(ConnString)
Dim cmd As New SqlCommand()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "AddNewRegistration"
cmd.Parameters.Add("@Title", SqlDbType.VarChar).Value = cboTitle.SelectedItem.Text.Trim()
cmd.Parameters.Add("@Firstname", SqlDbType.VarChar).Value = txtFirstname.Text.Trim()
cmd.Parameters.Add("@Surname", SqlDbType.VarChar).Value = txtSurname.Text.Trim()
cmd.Parameters.Add("@Phone", SqlDbType.VarChar).Value = txtPhone.Text.Trim()
cmd.Parameters.Add("@OtherPhone", SqlDbType.VarChar).Value = txtPhone2.Text.Trim()
cmd.Parameters.Add("@EmailAddress", SqlDbType.NVarChar).Value = txtEmail.Text.Trim()
cmd.Parameters.Add("@TravelDistance", SqlDbType.VarChar).Value = txtMiles.Text.Trim()
cmd.Parameters.Add("@MinDayRate", SqlDbType.Decimal).Value = txtMinDayRate.Text.Trim()
cmd.Parameters.Add("@MaxDayRate", SqlDbType.Decimal).Value = txtMaxDayRate.Text.Trim()
cmd.Connection = con
Try
con.Open()
cmd.ExecuteScalar()
Catch ex As Exception
Throw ex
Finally
con.Close()
con.Dispose()
End Try
AddLocation()
End Sub
Then my code for AddLocation()
Code:
Private Sub AddLocation()
Dim i As Integer
Dim chkbx As CheckBoxList
chkbx = CType(form1.FindControl("chkListLocations"), CheckBoxList)
For i = 0 To chkbx.Items.Count - 1
Dim ConnString As [String] = ConfigurationManager.ConnectionStrings("KeyStrategyConnectionString").ConnectionString
Dim con As New SqlConnection(ConnString)
Dim cmd As New SqlCommand()
Dim ID As Integer
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "AddLocations"
If chkbx.Items(i).Selected Then
cmd.Parameters.AddWithValue("@Location", chkbx.Items(i).Value)
cmd.Connection = con
Try
con.Open()
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
Catch ex As Exception
Throw ex
Finally
con.Close()
con.Dispose()
End Try
End If
Next
End Sub
Just need to know how I get the last ID from the registration and where to put it in the AddLocation sub
Thanks