Hi,
Please try below code.Change code as per your table structure(i.e. parent-child relationship),as we know we need to delete child table record first and then parent table record.
C#
try
{
using (NWindDataContext dc = new NWindDataContext())
{
var q =
(from c in dc.GetTable<Customer>()
where c.CustomerID == "AAAAA"
select c).Single<Customer>();
foreach (Order ord in q.Orders)
{
dc.GetTable<Order>().DeleteOnSubmit(ord);
foreach (Order_Detail od in ord.Order_Details)
{
dc.GetTable<Order_Detail>().DeleteOnSubmit(od);
}
}
dc.GetTable<Customer>().DeleteOnSubmit(q);
dc.SubmitChanges();
}
UpdateDataGrid();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
VB
Try
Using dc As New NWindDataContext()
Dim q = (From c In dc.GetTable(Of Customer)() Where c.CustomerID = "AAAAA"c).[Single](Of Customer)()
For Each ord As Order In q.Orders
dc.GetTable(Of Order)().DeleteOnSubmit(ord)
For Each od As Order_Detail In ord.Order_Details
dc.GetTable(Of Order_Detail)().DeleteOnSubmit(od)
Next
Next
dc.GetTable(Of Customer)().DeleteOnSubmit(q)
dc.SubmitChanges()
End Using
UpdateDataGrid()
Catch ex As Exception
Response.Write(ex.Message)
End Try
I hope this will help you out.