Sann Lin

Sann Lin

  • NA
  • 9
  • 4.4k

Insert two tables in a time

Aug 28 2015 12:37 AM
I want to Insert 2 tables (which all have the same field) is Tbl_Branch & Tbl_BranchHistory at the same time.
But when the insert data is Tbl_Branch has 1 row and Tbl_BranchHistory has 3 rows that 1 row has data and 2 rows are blank. Please see pictures
(Tbl_Branch)
 
Tbl_BranchHistory
 
 
The following​ is my code:
// Insert code
cmd = New SqlCommand("Branch", cnn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@name", txtBranchName.Text)
cmd.Parameters.AddWithValue("@location", txtLocation.Text)
cmd.Parameters.AddWithValue("@note", txtDescription.Text)
cmd.Parameters.AddWithValue("@inputby", txtUser.Text)
cmd.Parameters.AddWithValue("@inputdate", userDate)
cmd.ExecuteNonQuery()
 
// Stored Procedure
insert into Tbl_Branch(BranchName,Location,[Description], input_By, input_Date)
VALUES(@name,@location,@note,@inputby,@inputdate)
declare @lastrow int
select top 1 @lastrow = BranchId from Tbl_Branch
order by BranchId desc

DECLARE @insert INT
Select @insert = @@ROWCOUNT
if @insert>0

insert into Tbl_BranchHistory(BranchId,BranchName,Location,[description],status,update_By,update_Date)
VALUES(@lastrow,@name,@location,@note,@status,@updateby,@updatedate)
 

Answers (2)