Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I Want handle Delete from Grid view 2 tables that have relationship with another table

when i want to delete row from the first table
an error accrued

The DELETE statement conflicted with the REFERENCE constraint "FK_TryIt_GuestsTable". The conflict occurred in database "Tourist", table "dbo.TryIt", column 'BookingNo'.
The statement has been terminated.


so i want to create message when i want to delete row & this row have another values in rows of another table

(((you must delete all Booking in the Second table that related with this Client )))

first table :
IDBooking (P.K) Type Guest Hotel Address

Second Table
ID(P.K) IdBooking (F.K) Number Night Number Rooms


first table :
IDBooking (P.K)  Type  Guest Hotel Address
1
2
3
4
Second Table
ID(P.K) IdBooking (F.K)  Number Night Number Rooms
1	1
2	1
3	1
4	2
5	2
6	2
7	2
8	3
----------------
if i want to delete the row 4 in the First table i can
but
if i want to delete the row 1 in the First table i Must
delete Rows(1-2-3) in the Second Table

i want to handel this error to dont appear to the client
Posted
Updated 18-Nov-13 5:00am
v2
Comments
kabifarm 18-Nov-13 7:59am    
why not create delete statement that will delete all related information from both tables at once

like delete from table 1 where code = 1
and delete from table where code = 1.1,2.1,3.1

Put this code in catch block:
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Cannot be Deleted because it is used in other Pages!!!');", true);
 
Share this answer
 
Comments
Tom Marvolo Riddle 18-Nov-13 8:04am    
If you don't want to show this to client then get the exception from catch block and assigned it to a label and make it visible false
------------------------------------------------
first table :
------------------------------------------------
IDBooking (P.K) Type Guest Hotel Address
------------------------------------------------
1
2
3
4
------------------------------------------------
Second Table
------------------------------------------------
ID(P.K) IdBooking (F.K) Number Night Number Rooms
------------------------------------------------
1       1
2       1
3       1
4       2
5       2
6       2
7       2
8       3
------------------------------------------------

When you delete the IDBooking(ID = 1) from first table, you should two any of the following.
0) Check the ID exists in the second table, if exists then show alert like you have mentioned in your question.
You must delete all Booking in the Second table that related with this Client.
Sample query to find existence of records
SQL
SELECT * FROM second-table WHERE IdBooking=@ID

Check this article to show alerts
Use & Call RegisterStartUpScript, RegisterClientScript and Client-Side Script[^]

1) Delete the matching record[IDBooking(ID = 1)] from second table, then delete the record from first table. And display message to end user.
Sample query to delete records
SQL
DELETE FROM second-table WHERE IdBooking=@ID

SQL
DELETE FROM first-table WHERE IdBooking=@ID


But it depends on requirement.
 
Share this answer
 
Comments
radals 18-Nov-13 14:10pm    
at first thanks alot
i meant this Solution
1) Delete the matching record[IDBooking(ID = 1)] from second table, then delete the record from first table. And display message to end user.
Sample query to delete records

but how i can handle this case in case of i use wizard Gridview & dont use Code .Cs
How i can check the rows in the SECOND TABLE that related with THE Row in the First Table
thatraja 19-Nov-13 1:53am    
Include Grid related code & UI in your question

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



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