Delete page problem

May 9, 2008 at 1:17 AM
Just did a fresh download of the project and ran it for the first time. Everything came up and was working fine until i tried to delete a page. Got the following error.

The DELETE statement conflicted with the REFERENCE constraint "FKWidgetInstancePage". The conflict occurred in database "Dashboard", table "dbo.WidgetInstance", column 'PageId'.
The statement has been terminated.

I guess this has to do with the recent delete page functionality that was added.
May 9, 2008 at 9:36 PM
Yes, every widget on the page has to be deleted before the page can be deleted. This should be an easy fix in the code.
May 9, 2008 at 9:51 PM
Edited May 9, 2008 at 9:59 PM
Add the following code in the db.SubmitChanges of the DeletePageActivity.cs just after the var UserSettings section(Should be line 52):
var WidgetInstances = (from widgetInstance in db.WidgetInstances
where widgetInstance.PageId == PageId
orderby widgetInstance.ColumnNo, widgetInstance.OrderNo
select widgetInstance)
.ToList();

db.WidgetInstances.DeleteAllOnSubmit(WidgetInstances);
db.SubmitChanges();
May 10, 2008 at 3:10 AM
Yes that worked. Thanks.

Here's the full code if anyone else needs:

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                var candidatePage = (from page in db.Pages
                                     where page.UserId == UserGuid && page.ID != PageId
                                     orderby page.ID ascending
                                     select page).First();

                var tUserSettings = db.UserSettings.Single(userSetting => userSetting.UserId == this.UserGuid);
                var WidgetInstances = (from widgetInstance in db.WidgetInstances
                                       where widgetInstance.PageId == PageId
                                       orderby widgetInstance.ColumnNo, widgetInstance.OrderNo
                                       select widgetInstance).ToList();

                db.WidgetInstances.DeleteAllOnSubmit(WidgetInstances);
                db.SubmitChanges();

                tUserSettings.CurrentPageId = candidatePage.ID;
                db.SubmitChanges();

                var pageToDelete = db.Pages.Single(p => p.ID == PageId);
                db.Pages.DeleteOnSubmit(pageToDelete);
                db.SubmitChanges();
            }
         
            return ActivityExecutionStatus.Closed;
        }
May 10, 2008 at 4:37 AM
Yes that worked. Thanks.

Here's the full code if anyone else needs:

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                var candidatePage = (from page in db.Pages
                                     where page.UserId == UserGuid && page.ID != PageId
                                     orderby page.ID ascending
                                     select page).First();

                var tUserSettings = db.UserSettings.Single(userSetting => userSetting.UserId == this.UserGuid);
                var WidgetInstances = (from widgetInstance in db.WidgetInstances
                                       where widgetInstance.PageId == PageId
                                       orderby widgetInstance.ColumnNo, widgetInstance.OrderNo
                                       select widgetInstance).ToList();

                db.WidgetInstances.DeleteAllOnSubmit(WidgetInstances);
                db.SubmitChanges();

                tUserSettings.CurrentPageId = candidatePage.ID;
                db.SubmitChanges();

                var pageToDelete = db.Pages.Single(p => p.ID == PageId);
                db.Pages.DeleteOnSubmit(pageToDelete);
                db.SubmitChanges();
            }
         
            return ActivityExecutionStatus.Closed;
        }