Click here to Skip to main content
13,045,566 members (72,653 online)
Click here to Skip to main content
Articles » Web Development » Ajax » General » Downloads


168 bookmarked
Posted 5 Mar 2010

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 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
                // to see how to use ScriptManager1 to register script when it is dynamically hanged on AJAX requests

                // 2. Refresh data in GridView

            if(ScriptManager1.IsInAsyncPostBack) // AJAX partial update

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

            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;

            //DebugLabel.Text += "<br>Refresh end: " + Tools.ToTimeStringWithMilisecond(DateTime.Now);
            if (!IsPostBack)
                DebugLabel.Text = "";
                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:
        // 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.


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


About the Author

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.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 27 Nov 2012
Article Copyright 2010 by CoperNick
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid