Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 30th, 2008, 08:08 AM
Authorized User
 
Join Date: Oct 2004
Location: Bangalore, Karnataka, India.
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default SqlDependency OnChange Event

Hi,
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)
    {
        EnoughPermission();
        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();
        GetAdvtData();
    }

     private bool EnoughPermission()
     {
        SqlClientPermission perm = new SqlClientPermission(System.Security.Permissions.Pe rmissionState.Unrestricted);
        try
        {
            perm.Demand();
            return true;
        }
        catch (System.Exception)
        {
            return false;
        }
    }
    SqlDependency dependency;
    private void GetAdvtData()
    {
        myDataSet.Clear();
        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";
            GridView1.DataBind();
        }
    }

    private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
    {
        try
        {
            RefreshData();
            UpdatePanel1.Update();
        }
        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";
        GetAdvtData();
    }

}

Thanks,


  #2 (permalink)  
Old July 30th, 2008, 03:01 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.

-Peter
compiledthoughts.com
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:32 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.