Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Transaction Related Problem

Author  Topic 

SDC
Starting Member

3 Posts

Posted - 2009-12-04 : 06:44:13
I have two tables 1)stu_master 2)stu_details
design are as follows.
stu_master
_____________

s_id int primary key,
s_name varchar(50)


stu_details
_________

s_id int foreign key (with update and delete cascade)
s_qly varchar



I have a code in vb.net 2008 as follows

sub abc()
try

Dim tran As SqlTransaction = Nothing
tran = con.BeginTransaction

cmd = New SqlCommand("insert into stu_master VALUES(1,'ABC')",cmd,tran)

cmd.ExecuteNonQuery()

cmd = New SqlCommand("insert into stu_details values(1,'xyz')",cmd,tran)

cmd.ExecuteNonQuery()

tran.commit
Catch ex As Exception
If Not tran Is Nothing Then
tran.Rollback()
End If
lbl_error.Text = ex.Message

End Try
---------------------------------------
Problem is that It's giving the foreign key reference error.

I don't want to commit until the data in stu_details table is entered.

But Until the transaction is commited the data is not shown in master table , giving the foreign key error.


My question is that , "Is there any way to enter the data in both tables using a transaction if we have foreign key relationship"

Please help.
   

- Advertisement -