G
Guest
Hi All,
Thank you in advance. I am trying to insert into two tables but I am
getting the following error: "You cannot add or change a record because
a related record is required in table..." I am not sure why this is
happening since I am using transactions. Below is the code I am using.
Dim con as OleDbConnection
Dim cmd as OleDbCommand
Dim tran as OleDbTranscation
Dim id as string
con = new OleDbConnection(connectionstring)
con.open
tran = con.BeginTransaction
cmd = new OleDbCommand
cmd.Connection = con
cmd.Transaction = tran
cmd.CommandText = "insert into table1 (id,name) values (1,'test name')"
cmd.ExecuteNonQuery()
cmd.CommandText = "select Max(id) from table1"
id = cmd.ExecuteScalar()
cmd.CommandText = "insert into table2 (id,name) values " & id &
",'another test name')"
cmd.ExecuteNonQuery()
Do you have any suggestions?
Thanks again!
Note: I am using an MS Access database
Thank you in advance. I am trying to insert into two tables but I am
getting the following error: "You cannot add or change a record because
a related record is required in table..." I am not sure why this is
happening since I am using transactions. Below is the code I am using.
Dim con as OleDbConnection
Dim cmd as OleDbCommand
Dim tran as OleDbTranscation
Dim id as string
con = new OleDbConnection(connectionstring)
con.open
tran = con.BeginTransaction
cmd = new OleDbCommand
cmd.Connection = con
cmd.Transaction = tran
cmd.CommandText = "insert into table1 (id,name) values (1,'test name')"
cmd.ExecuteNonQuery()
cmd.CommandText = "select Max(id) from table1"
id = cmd.ExecuteScalar()
cmd.CommandText = "insert into table2 (id,name) values " & id &
",'another test name')"
cmd.ExecuteNonQuery()
Do you have any suggestions?
Thanks again!
Note: I am using an MS Access database