Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » Ajax » General » Downloads
 
Add your own
alternative version
Go to top

HTTP Push from SQL Server — Comet SQL

, 27 Nov 2012
This article provides an example solution for presenting data in "real-time" from Microsoft SQL Server in an HTML browser. The article presents how to implement Comet functionality in ASP.NET and how to connect Comet with Query Notification from SQL Server.
// Copyright (c) 2010 Daniel Wojciechowski 
// E-mail: at gmail.com daniel.wojciechowski

using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI;

namespace HttpPushFromMsSql
{
    public partial class _Default : Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack) // "First" page load
            {
                // 1. Generate javascript code

                // 1.1 Generate RefreshData function (JavaScript)
                RegisterFunctionToPostBack("RefreshData", UpdatePanel1);
                
                // 1.2 Write content of LongPolling.js
                string script = File.ReadAllText(Server.MapPath("Script/LongPolling.js"));
                // This script is sticked in html in case when you will need
                // to dynamically change this script (for example script.Replace(...,...))
                // Otherwise you can just include this script using ScriptManager.
                //script = script.Replace("TEMPLATE('lastRecIdValue')", GetLastRecId().ToString());
                Page.ClientScript.RegisterStartupScript(typeof (Page), "LongPoller", script, true);
                // look at http://syedgakbar.wordpress.com/2008/03/01/registerstartupscript-in-ajax-pages/
                // to see how to use ScriptManager1 to register script when it is dynamically hanged on AJAX requests

                // 2. Refresh data in GridView
                RefreshData();
            } 

            if(ScriptManager1.IsInAsyncPostBack) // AJAX partial update
            {
                RefreshData();
            }
        }

        private void RefreshData()
        {
            DateTime refreshStart = DateTime.Now;
            DebugLabel.Text = "Refresh requested: " +
                Tools.ToTimeStringWithMilisecond(DateTime.Now);

            int lastRecId;
            DateTime lastRecTime;
            List<string> data = MessageDal.GetMessageData(out lastRecId, out lastRecTime);
            DebugLabel.Text +=
                "<br>Time measured: <b>" +
                (refreshStart - lastRecTime).TotalSeconds + "s</b>. " +
                "<br>Time measured starting at SQL INSERT, through query " +
                "notification of ASP.NET, Comet notification of browser " + 
                "and finishing at refresh call (by browser) on the server side.";

            Session["LastRecId"] = lastRecId;
            GridView1.DataSource = data;
            GridView1.DataBind();

            //DebugLabel.Text += "<br>Refresh end: " + Tools.ToTimeStringWithMilisecond(DateTime.Now);
            if (!IsPostBack)
                DebugLabel.Text = "";
            if(data.Count==0)
                DebugLabel.Text = "No data";
        }

        // I decided to generate javascript Refresh() function, but you can
        // write it by yourself and include in "LongPolling.js"
        //
        // Thanks to:
        // http://geekswithblogs.net/mnf/articles/102574.aspx
        // http://www.xefteri.com/articles/show.cfm?id=18 How postback works in ASP.NET
        // and thanks to Dave Ward hint for calling __doPostback("UpdatePanel1","") ,
        public bool RegisterFunctionToPostBack(string sFunctionName, Control ctrl)
        { 
            //call the postback function with the right ID __doPostBack('" + UniqueIDWithDollars(ctrl) + @"','');
            string js = "    function " + sFunctionName + @"()
                    {
                    " + ctrl.Page.ClientScript.GetPostBackEventReference(ctrl, "") + @"
                    }";
            ctrl.Page.ClientScript.RegisterStartupScript(this.GetType(), sFunctionName, js, true);
            return true;
        }

        
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

CoperNick
Software Developer
Poland Poland
I graduated from the Jagiellonian University. After defence of master thesis (link to presentation) I have started professional programming in C and C++ using OpenGL. After that, my Company gave me a chance to switch to .Net framework and C# and I have betrayed my passion for computer graphics. Now I'm C#, SQL and sometimes ASP.NET programmer.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 27 Nov 2012
Article Copyright 2010 by CoperNick
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid