0

I'm trying to update a PartialView when a register is updated in DataBase. I'm using SqlDependency and the event rise properly when the change happens.

SqlDependency.OnChange += new OnChangeEventHandler(sqlDependency_OnChange);

Method:

Private void sqlDependency_OnChange(object sender, SqlNotificationEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Ok");

        UpdateView();

    }

And the method that is called:

 public ActionResult UpdateView()
    {
        try
        {
            //Blablabla...
            //Business logic...
            //Blablabla...
            model = 2;

            return PartialView(model);
        }
        catch (Exception ex)
        {
            return PartialView(0);
        }
    }

I receive every time a change is made the Lines in the OutputWindow, the method UpdateView() exits with no errors, but the PartialView is not render any more.

The View:

@model int
<h4>You have @Model points.</h4>

What I'm doing wrong? Is it because I'm requesting the Render from a different thread? Can I Update a View from server side when an event occurs? Thanks for your help!

svick
  • 236,525
  • 50
  • 385
  • 514
Mario Levrero
  • 3,345
  • 4
  • 26
  • 57
  • Web requests typically fall into a simple pattern that looks like this: request > process (this is your asp.net code) > send response to client. Once the output is sent to the client, it's End of Story. Now, what happens if a db record is updated AFTER the client request/response is complete? How do you plan to send that updated view back to the client? Think about this issues and try to understand the basics behind how http/web pages/asp.net/php or whatever else delivers html to clients before you worry about multi-threading. – bkdc Sep 03 '13 at 12:15
  • I'm quite sure that the problem is that the `ActionResult` returned from `UpdateView()` doesn't go anywhere: you just ignore it. (I don't know enough about MVC to help you fix that, if that's even possible.) – svick Sep 03 '13 at 12:15
  • or take the shortcut and check SignalR [link](http://www.codeguru.com/csharp/.net/sending-notifications-using-asp.net-signalr.htm) – bkdc Sep 03 '13 at 12:16
  • @svick , thanks for your answer and edit. I agree that I'm ignoring it, but how could I manage it if the output has been already sent to the client? – Mario Levrero Sep 03 '13 at 14:24
  • @bkdc , thanks for your answer and link. I understand what you say. Because of that, how could I manage data from a background thread if data has been already sent to the client? If it's not possible, what sense have multithreading in web environment? – Mario Levrero Sep 03 '13 at 14:28
  • 1
    multithreading & web: ask your PHP friends :) Jokes aside, there can be many uses for it: SignalR is just one example. Being able to run things in the background instead of only when there's a request from a client can give you a lot of freedom to do interesting (and useful) stuff. The "problem", in your case, is sending back to the client that interesting stuff you've done in the background; there are multiple approaches to solving this issue: have your client poll for changes using Ajax, SignalR (again) or simply have the background task update whatever is needed for the next client request – bkdc Sep 04 '13 at 14:41

0 Answers0