-1

I have looked at all the other questions regarding this and I cant seem to find an answer that fits to my problem.

I have a database filled with measurements and respective datetime for the measurement. I currently use ajax in a asp.net web application to get data from my database(sql server) and make a chart out of that data. The problem is that I have to reload the page in order to see new values that entered the database, meaning that only database values that were present when the page loaded is visible on the web application. I want to poll for new database values every second or so but I cant figure out how to do it in this case.

UPDATED NOW: I have tried to use $.Ajax without any success. I am now trying to return a array 'y' (y values) from a web method and get this from ajax call. See updated code. The problem still lies withing the $.Ajax method in .aspx.

LAST UPDATE: I figured it out after following Josh Mein's step. I had to read documentation and so forth but I learned a lot. If anyone ever encounter a similar problem I will put a comparison below:

.aspx(before):

<head runat="server">
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="Scripts/jQuery-1.6.4.min.js"></script>
<script>
    setInterval(FillGraph, 1000);
function FillGraph() {
    $.ajax({
        type: "POST",
        url: "WebForm1.aspx/getArray",
        data: '{ }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: OnError
    });

    function OnSuccess(response) {
    //Fill the graph here i guess(?)
    //I tried alert(response.d) and it didnt return anything.
    }
</script>

.aspx (After):

function makeChart() {
        $.ajax({
            type: "POST",
            url: "WebForm1.aspx/TestDouble",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                for (var i = 0; i < Object.keys(msg.d).length; i++) {
                    yVals[i] = msg.d[i];
                }
            }
        });

        $.ajax({
            type: "POST",
            url: "WebForm1.aspx/TestDate",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                for (var i = 0; i < Object.keys(msg.d).length; i++) {
                    xVals[i] = msg.d[i];
                }
            }
        });

        var trace1 = {
            x: xVals,
            y: yVals,
            fill: 'tozeroy',
            type: 'scatter'
        };
        var data = [trace1];

        Plotly.newPlot('myDiv', data);
    }

aspx.cs (Before):

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static double[] getArray()
    {
        DataTable dt = new DataTable();
        using (SqlConnection con = new SqlConnection(@"Data Source =localhost\SQLEXPRESS; Initial Catalog = HIDDEN; Integrated Security=True"))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("HIDDEN", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            da.Fill(dt);
            con.Close();
        }


        string[] x = new string[dt.Rows.Count];
        double[] y = new double[dt.Rows.Count];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            y[i] = Convert.ToDouble(dt.Rows[i][0]);
            x[i] = dt.Rows[i][1].ToString();
        }
        return y;
    }
}

aspx.cx (After):

[WebMethod]
    public static string[] TestDate()
    {
        DataTable dt = new DataTable();
        using (SqlConnection con = new SqlConnection(@"Data Source =localhost\SQLEXPRESS; Initial Catalog = WeatherSystem; Integrated Security=True"))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("HIDDEN", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            da.Fill(dt);
            con.Close();
        }
        string[] xArray = new string[dt.Rows.Count];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            xArray[i] = dt.Rows[i][0].ToString();
        }
        return xArray;
    }

    [WebMethod]
    public static double[] TestDouble()
    {
        DataTable dt = new DataTable();
        using (SqlConnection con = new SqlConnection(@"Data Source =localhost\SQLEXPRESS; Initial Catalog = HIDDEN; Integrated Security=True"))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("HIDDEN", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            da.Fill(dt);
            con.Close();
        }
        double[] yArray = new double[dt.Rows.Count];
        //string[] xArray = new string[dt.Rows.Count];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            //xArray[i] = dt.Rows[i][0].ToString();
            yArray[i] = Convert.ToDouble(dt.Rows[i][0]);
        }
        return yArray;
    }

TestDouble() returns an array of double values. TestDate() returns respective DateTimes to those double values (measurements). GetChart() executes 2 ajax calls, one for Date values and one for Double values. The chart is then filled with these values.

12
  • 1
    There are plenty of examples of ajax on SO that should get you headed in the right direction. Just make a WebMethod in your code behind and call it it via $.ajax, $.get, or $.post. When the call returns, use the data to recreate your graph. As it stands right now, your code doesnt even show an attempt at solving the problem. Commented Apr 20, 2018 at 12:51
  • Possible duplicate of ASP.NET jQuery Ajax Calling Code-Behind Method Commented Apr 20, 2018 at 12:54
  • Thanks for the answer. Believe me I have tried $.ajax methods and $.get and so forth. The problem is the content inside those methods, like url, datatype(json?) and so on.. Commented Apr 20, 2018 at 12:54
  • Have you looked up the documentation for $.ajax? jQuerys documentation is really good. Commented Apr 20, 2018 at 12:56
  • If you have tried to use it, update your question with some code showing your attempt. You may be making a simple mistake. Commented Apr 20, 2018 at 13:07

1 Answer 1

0

I solved it, see last update on post for info.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.