Wrox Programmer Forums
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 30th, 2008, 08:08 AM
Authorized User
Join Date: Oct 2004
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default SqlDependency OnChange Event

I am using SqlDependency in web application(asp.net), here when i m chenging something in database I am getting the notification in dependency_OnChange(object sender, SqlNotificationEventArgs e)event …but when I am trying to do some work according to that notification…..,it is not happening…..here in the following code I am trying to display a simple string in label… and then trying to populate the grid with changed values from the database automatically, without externally refreshing the page.....for this purpose i m using Ajax update panel control and calling its update() method in OnChange event,but this is not working………..Please help me to solve this problem...

using System.Data.SqlClient;

public partial class Notification : System.Web.UI.Page
    private DataSet myDataSet = null;
    private SqlConnection connection;
    private SqlCommand command = null;

    protected void Page_Load(object sender, EventArgs e)
        string ssql = "select Id,Name from dbo.Customer ";

        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrin gs["ASPNETDemoConnectionString"].ConnectionString.ToString());
        if (command == null)
            command = new SqlCommand(ssql, connection);

        if (myDataSet == null)
            myDataSet = new DataSet();

     private bool EnoughPermission()
        SqlClientPermission perm = new SqlClientPermission(System.Security.Permissions.Pe rmissionState.Unrestricted);
            return true;
        catch (System.Exception)
            return false;
    SqlDependency dependency;
    private void GetAdvtData()
        command.Notification = null;
        dependency = new SqlDependency(command);
        Label1.Text = System.DateTime.Now.ToString();
        dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
            adapter.Fill(myDataSet, "dbo.Customer");
            GridView1.DataSource = myDataSet;
            GridView1.DataMember = "dbo.Customer";

    private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        catch (Exception)
        SqlDependency dependency = (SqlDependency)sender;
        dependency.OnChange -= dependency_OnChange;
    private void RefreshData()
        Label1.Text = "Database had some changes and are applied in the Grid";



Old July 30th, 2008, 03:01 PM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

So are you saying that you expect an event triggered by a SQL dependency to "push" HTML back to a client page when the event fires? It doesn't work this way.

Doing anything against page control with a dependency event is essentially pointless. Unless the dependency happens to fire precisely between the point at which you "wire up" to the dependency event and the end of the event handling portion of the page life cycle, you'll never affect the rendered page output.

The only way you are going to be able to get updates to the page is to poll it from the browser.


Similar Threads
Thread Thread Starter Forum Replies Last Post
onChange event mcinar HTML Code Clinic 3 May 24th, 2007 08:46 PM
onChange Event mcinar Javascript 11 May 11th, 2007 09:38 AM
how to use onchange event of dropdownlist check_007 ASP.NET 2.0 Professional 2 February 16th, 2007 02:57 AM
Event:OnChange catchrohith Classic ASP Basics 0 November 3rd, 2006 12:52 AM
OnChange Event Doesn't Execute JLJones13 Javascript How-To 1 August 22nd, 2003 02:15 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.