Emmmanuel FIADUFE

Emmmanuel FIADUFE

  • 778
  • 1.1k
  • 55.3k

Delete data from two tables

May 21 2024 1:59 AM

 Hello Team,

I have two tables  and am trying to delete from the two tables but I keep getting this response which says fail to delete data.

public ActionResult DeleteStaff(int StaffId, int NextOfKingId)
        {
            try
            {
                var DeleteNextOfKing = objBasicShoolDBEntities.tblNextOfKings.Where(a => a.NextOfKingId == NextOfKingId).FirstOrDefault<tblNextOfKing>();
                if (DeleteNextOfKing != null)
                {
                    objBasicShoolDBEntities.tblNextOfKings.Remove(DeleteNextOfKing);
                }
                var Staff = objBasicShoolDBEntities.tblStaffs.Where(a => a.StaffId == StaffId && a.NextOfKingId == NextOfKingId).FirstOrDefault<tblStaff>();
                objBasicShoolDBEntities.tblStaffs.Remove(Staff);
                objBasicShoolDBEntities.SaveChanges();
                return Json(true, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return Json(false, JsonRequestBehavior.AllowGet);
            }
        }

 

function DeleteStaff(StaffId, NextOfKingId) {
        swal({
            title: "Do you want to delete this staff data?",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: 'Yes Delete',
            cancelButtonText: 'No pls cancel',
            confirmButtonClass: 'btn btn-success',
            cancelButtonClass: 'btn btn-danger',
            type: "warning",
            closeOnConfirm: false,
            closeOnCancel: false
        }).then(
        function (isConfirm) {
            if (isConfirm.value) {
                $.get("/Home/deleteStaff", { StaffId: StaffId, NextOfKingId: NextOfKingId }, function (res) {
                    if (res) {
                        dataTable.ajax.reload();
                        swal('Confirm', 'Staff deleted successfully', 'success');
                    }

                    // dataTable.ajax.reload();
                }).fail(function () {
                    swal('Error', 'Failed to delete staff!', 'erro');
                });
            }
            else {
                swal('Cancelled', '', 'error');
            }

        });
    }


Answers (6)