0

We are developing a real-time web application in Asp.Net 4.5 that has a few charts.

We also have a SQL Server 2008 with a table that feeds the charts.

The charts should be refreshed if any record inserted has X minutes is found. Where X are the minutes that have passed after a record was created. For example, if X = 20 min, we would check if any record is 20 minutes old in this way getdate() - table.CreationDate = 20 minutes.

We already tried with SqlDependency but it has some limitations like no computed columns are allowed, so we cannot do something like

    SELECT id, 
      CASE WHEN DATEDIFF(mi, table.CreationDate, getdate()) = 20 THEN 1
      ELSE 0
    END Send
    FROM table

How can we make this done?

Jamo
  • 494
  • 5
  • 24

1 Answers1

0

I am assuming you are having a web page and not a desktop application.

You can use jQuery to fetch latest chart infor every x seconds/minutes. If there is a change in database you can return a new chart or you can return null.

In jquery check if the json value is not null set the chart image.

C# Code:

public FileContentResult GetYourChartHere()
        {
            return objCommonFunction.GetYourChartHere();
        }

jQuery Code:

function GetMyChart() {
    $.ajax({
        type: 'GET',
        url: Path,
        cache: false,
        success: function (obj) {

            //adding a datetime to the querystring allows you to override ajax and browser caching.
            var url = Your Page UrL Pointing To'GetYourChartHere' Method + "?t=" + new Date().getTime().toString();

            // set the image path for charts
            $('#YourImageAttribute').attr('src', url);           

        },
        error: function (obj) {
            alert('Something went wrong');
        }
    });
};
MCoder
  • 113
  • 5
  • Yes, it is a web application (I just updated the question). What I am looking for is only refresh the front-end when a record in DB has been there by X minutes, With the jQuery approach we would be updating front-end every X minutes what I don't want. – Jamo Jan 24 '18 at 16:02
  • Agree, but in ajax call you only need to take an action (set the image URL) in case new changes are available. If there are no changes you can return null and skip setting URL in jQuery code. It will surely call the function every X minutes but it will not impact the performance a lot. – MCoder Jan 24 '18 at 19:51
  • Yeah, that is fine but we are looking for a way to avoid generating requests to the server if the data hasn't changed. – Jamo Jan 25 '18 at 01:19