Click here to Skip to main content
15,910,234 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i cant performed delete query by using below code



VB
adp = New SqlDataAdapter("delete from tbl1 where no=" & TextBox1.Text & " ", cn)
        adp1 = New SqlDataAdapter("delete from tbl2 where no=" & TextBox1.Text & "", cn)

        cmd.ExecuteNonQuery()
        'cmd1.ExecuteNonQuery()
        adp.Fill(ds, "tbl1")
        cmd.ExecuteNonQuery()
        adp1.Fill(ds1, "tbl2")
        MsgBox("Record Deleted....")


error generate

The DELETE statement conflicted with the REFERENCE constraint "FK_tbl2_tbl1". The conflict occurred in database "two", table "dbo.tbl2", column 'no'. The statement has been terminated.
Posted

You need to delete before all references (Records) from Child tables then delete from Master table.

Or you may refer following:

http://www.builderau.com.au/program/sqlserver/soa/SQL-basics-Deleting-from-child-tables/0,339028455,320269121,00.htm[^]

http://www.sqlteam.com/article/performing-a-cascade-delete-in-sql-server-7[^]

Thanks,
Imdadhusen
 
Share this answer
 
v2
Comments
somil030 11-Feb-11 2:48am    
thnx for ur best replying ...
will u explain me why should i delete all reference from child b4 master table
Sunasara Imdadhusen 11-Feb-11 3:06am    
because your child b4 is referring to the parents table!
As said you must delete the child rows first or the other possibility is to change the foreign key constraint to cascade the deletion from master table to child tables. You can use this approach if you want the database to always delete the children when master is deleted. See: Cascading Referential Integrity Constraints[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900