Question : Vb.net code to use with stored procedure

I was currently using the below code to insert a record into the database. I now need to insert the record and return the Id  of the inserted record.
I was able to change the stored procedure to return the id (NewID) which is in the code box below.

I don't know how to change my vb.net code to get this value. Please help.

This is what I currently have

        Dim queryString As String = "sp_AddRecord"
        Dim connection As New SqlConnection(ConfigurationManager.ConnectionStrings("strConn").ToString())
        Dim command As New SqlCommand(queryString, connection)

        command.CommandType = CommandType.StoredProcedure

        command.Parameters.Add("@Invoice", SqlDbType.VarChar).Value = strInvoice
        command.Parameters.Add("@Company", SqlDbType.VarChar).Value = IIf(txtCustomer.Text = "", " ", txtCustomer.Text)
        command.Parameters.Add("@SerialNumber", SqlDbType.VarChar).Value = IIf(txtSerial.Text = "", " ", txtSerial.Text)
        command.Parameters.Add("@DateAdded", SqlDbType.DateTime).Value = Date.Now

        connection.Open()
        command.ExecuteNonQuery()
        connection.Close()
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
ALTER PROCEDURE [dbo].[sp_AddRecord] 
	(
		@SerialNumber varchar(50)= NULL,
		@Company varchar(50)= NULL,
		@Invoice varchar(50)= NULL,
		@DateAdded datetime
	)
AS
BEGIN
	SET NOCOUNT ON;
	DECLARE @RetID As int
		
	--Get Id from comapny Table
	SELECT @RetID = [ID] FROM [lxp_1z3etnasf_site_aplus_net].[dbo].[Company] WHERE [Name] = @Company
	--Insert Login and link to Company table
	INSERT INTO [lxp_1z3etnasf_site_aplus_net].[dbo].[Certifications]
           ([Serial Number]
           ,[Invoice Authorization]
           ,[DateAdded]
           ,[CompanyID])
	VALUES
           (@SerialNumber
           ,@Invoice
           ,@DateAdded
           ,@RetID)
           
          SELECT @@identity AS newID
END

Answer : Vb.net code to use with stored procedure

Change the following line:

command.ExecuteNonQuery()

To

Dim RetID as Integer = Convert.ToInt32(command.ExecuteScalar())
Random Solutions  
 
programming4us programming4us