Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm using mvc, i have a dashboard for which i have used charthelper and bootstrap admin chart. Now i want to update the data on database change. For which i'm trying to use signal R.

Before

I used repository to get data from database. So had services folder which had methods for it.

Now.

I'm not sure exactly how to do it.
But what i have done so far is created a hub class,
returning

public static void Send()
       {
           IHubContext context = GlobalHost.ConnectionManager.GetHubContext<DashboardHub>();
           context.Clients.All.updateOnDashboard();
       }


and on view

<script>
    $(function () {
        // Declare a proxy to reference the hub.
        var chat = $.connection.dashboardHub;
        $.connection.hub.logging = true;

        chat.client.foo = function () { };

        //debugger;
        // Create a function that the hub can call to broadcast messages.
        chat.client.updateOnDashboard = function () {
            getAllDashboardUpdates()
        };
        $.connection.hub.start().done(function () {
            getAllDashboardUpdates();

            console.log('Now connected, connection ID=' + $.connection.hub.id);
        })
            .fail(function () { console.log('Could not connect'); });;

        //$.connection.hub.stop();
    });

    function getAllDashboardUpdates() {
        $.ajax({
            url: '/Dasdhboard/Index',
            contentType: 'application/html ; charset:utf-8',
            type: 'GET',
            dataType: 'html'
        }).success(function (result) {
            //$("#refTable").html(result);
        }).error(function () {
        });
    }
</script>


controller method
public ActionResult Index(int? page)
        {
            IEnumerable<test> newlist = null;

            newlist = GetAlltest();
            var data = dashboardService.GetDashboardData(page, User);
            if (newlist != null)
            {
                return View(data);
            }
            return View(data);
        }


to search for dependency

C#
public IEnumerable<test> GetAlltest()
       {

           var messages = new List<test>();
           using (var connection = new SqlConnection(_connString))
           {
               connection.Open();
               using (var command = new SqlCommand(@"SELECT [id],[testid] FROM [dbo].[test]", connection))
               {
                   command.Notification = null;
                   SqlDependency.Start(_connString);
                   var dependency = new SqlDependency(command);
                   dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);

                   if (connection.State == ConnectionState.Closed)
                       connection.Open();

                   var reader = command.ExecuteReader();

                   while (reader.Read())
                   {
                       messages.Add(item: new test { id = (int)reader["id"] });
                   }
               }

           }
           return messages;
       }

       private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
       {
           if (e.Type == SqlNotificationType.Change)
           {
               DashboardHub.Send();
           }
       }



Even after doing it my view is not refreshed. I'm sure code is redundant. CAn someone show me a better way to do it. Or where i'm going wrong.

This is just one method. I have 2 charts too.
Posted
Updated 24-Nov-15 23:52pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900