none
How to Detect state change in database table and add a schedule function to run at a specific time? RRS feed

  • Question

  • Let’s say I have a website where users can create questionnaires that get stored in the database for their users to answers the questions. My idea was to send an email notification to their users when the questionnaire has been changed for example a new question being added to the questionnaire.

    However, to avoid send email every two minutes in case the user keeps on editing the questionnaire, I have created a temporary table which will keep a record on when the questionnaire has been edited.  Now the challenge that I am facing is that I would like the email notification to be sent an hour after the last questionnaire has been edited.

    I hope this made sense.

             [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Edit(SurveyVM vm)
            {
                if (ModelState.IsValid)
                {
                    try
                    {
                        bool readyToPublish = false;
                        if (vm.Survey.Publish)
                        {
                            readyToPublish = surveyLogic.CheckIfReadyToPublish(vm.Survey);
                            if (readyToPublish)
                            {
                               // User currentUser = userLogic.GetBy(x => x.UserID == vm.User.UserID);
                                Survey survey = surveyLogic.GetBy(x => x.SurveyName == vm.Survey.SurveyName && x.CustomerID == vm.Survey.CustomerID && x.SurveyID != vm.Survey.SurveyID).SingleOrDefault();
                                if (survey == null)
                                {
                                    surveyLogic.Update(vm.Survey);
    
    
                                   surveyLogic.SuveyUpdate(vm.Survey);
                                }
    
                    }
                    catch (Exception e) 
                    {
                        vm.CategoryOptions = CategoryOptionsHelper.BuildCategoryOptions(vm.Survey.CustomerID, false);
                        TempData["Failure"] = "Edit Failed. Please Review Your Selections and Try Again.";
                        return View(vm);
                    }
    
    }
    }
    
    
          public void SuveyUpdate(Survey survey)
            {
                using (ManageLoggedSurveyUpdate updateLogic = new ManageLoggedSurveyUpdate(ref base.Uow))
                using (ManageSurvey surveyLogic = new ManageSurvey(ref base.Uow))
                using (ManageLoggedSurveyUpdate loggedSurveyUpdate = new ManageLoggedSurveyUpdate(ref base.Uow))
                {
                    Survey surveys = surveyLogic.GetBy(x => x.SurveyID == survey.SurveyID && x.Publish == survey.Publish).SingleOrDefault();
    
                    if (surveys == null)
                    {
                        LoggedSurveyUpdate newupdte = new LoggedSurveyUpdate()
                        {
                            SurveyID = survey.SurveyID,
                            Active = true,
                            SurveyEdited = DateTime.Now,
                            UserID = (int)System.Web.HttpContext.Current.Session["UserID"],
    
    
    
                        };
                        loggedSurveyUpdate.Insert(newupdte);
                    }
                    else {
    
                        LoggedSurveyUpdate newupdte = new LoggedSurveyUpdate()
                        {
    
                            SurveyID = survey.SurveyID,
                            Active = true,
                            SurveyEdited = DateTime.Now,
                            UserID = (int)System.Web.HttpContext.Current.Session["UserID"],
                           
    
    
    
                        };
    
                        loggedSurveyUpdate.Update(newupdte);
    
                       
                    }
    
                  
                    
                }
            }
    
    
    

    Friday, June 23, 2017 10:37 AM

All replies

  • Let’s say I have a website where users can create questionnaires that get stored in the database for their users to answers the questions. My idea was to send an email notification to their users when the questionnaire has been changed for example a new question being added to the questionnaire.

    However, to avoid send email every two minutes in case the user keeps on editing the questionnaire, I have created a temporary table which will keep a record on when the questionnaire has been edited.  Now the challenge that I am facing is that I would like the email notification to be sent an hour after the last questionnaire has been edited.

             [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Edit(SurveyVM vm)
            {
                if (ModelState.IsValid)
                {
                    try
                    {
                        bool readyToPublish = false;
                        if (vm.Survey.Publish)
                        {
                            readyToPublish = surveyLogic.CheckIfReadyToPublish(vm.Survey);
                            if (readyToPublish)
                            {
                               // User currentUser = userLogic.GetBy(x => x.UserID == vm.User.UserID);
                                Survey survey = surveyLogic.GetBy(x => x.SurveyName == vm.Survey.SurveyName && x.CustomerID == vm.Survey.CustomerID && x.SurveyID != vm.Survey.SurveyID).SingleOrDefault();
                                if (survey == null)
                                {
                                    surveyLogic.Update(vm.Survey);
    
    
                                   surveyLogic.SuveyUpdate(vm.Survey);
                                }
    
                    }
                    catch (Exception e) 
                    {
                        vm.CategoryOptions = CategoryOptionsHelper.BuildCategoryOptions(vm.Survey.CustomerID, false);
                        TempData["Failure"] = "Edit Failed. Please Review Your Selections and Try Again.";
                        return View(vm);
                    }
    
    }
    }
    
    
    
          public void SuveyUpdate(Survey survey)
            {
                using (ManageLoggedSurveyUpdate updateLogic = new ManageLoggedSurveyUpdate(ref base.Uow))
                using (ManageSurvey surveyLogic = new ManageSurvey(ref base.Uow))
                using (ManageLoggedSurveyUpdate loggedSurveyUpdate = new ManageLoggedSurveyUpdate(ref base.Uow))
                {
                    Survey surveys = surveyLogic.GetBy(x => x.SurveyID == survey.SurveyID && x.Publish == survey.Publish).SingleOrDefault();
    
                    if (surveys == null)
                    {
                        LoggedSurveyUpdate newupdte = new LoggedSurveyUpdate()
                        {
                            SurveyID = survey.SurveyID,
                            Active = true,
                            SurveyEdited = DateTime.Now,
                            UserID = (int)System.Web.HttpContext.Current.Session["UserID"],
    
    
    
                        };
                        loggedSurveyUpdate.Insert(newupdte);
                    }
                    else {
    
                        LoggedSurveyUpdate newupdte = new LoggedSurveyUpdate()
                        {
    
                            SurveyID = survey.SurveyID,
                            Active = true,
                            SurveyEdited = DateTime.Now,
                            UserID = (int)System.Web.HttpContext.Current.Session["UserID"],
                           
    
    
    
                        };
    
                        loggedSurveyUpdate.Update(newupdte);
    
                       
                    }
    
                  
                    
                }
            }
    
    
    


    • Edited by Cedric02 Friday, June 23, 2017 10:21 AM
    • Merged by 宝宝徐 Monday, June 26, 2017 2:57 AM the same
    Friday, June 23, 2017 10:18 AM
  •  

    Now the challenge that I am facing is that I would like the email notification to be sent an hour after the last questionnaire has been edited.

    You would need a Windows service that constantly runs on a machine to make the check and send the email.

    Friday, June 23, 2017 11:46 AM
  • Hi Cedric02,

    Maybe you could create a button like . When the questions be answered or something happen for the website, open could click the button like upload, save and so on, after this, the whole steps will be finished. And then send the mail.

    If you need future help, you could post in ASP.NET forum.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 28, 2017 6:25 AM