Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6229489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:34:29+00:00 2026-05-24T09:34:29+00:00

After a while of searching the internet, I found this piece of code that

  • 0

After a while of searching the internet, I found this piece of code that triggers upon a LINQ-based change to the Database. It trigger only once and doesn’t mention or show what was changed/deleted/added, or what table was CRUDed.

static class GlobalNotifications
{
    public static event OnChangeEventHandler OnChange;

    public static void InitializeNotifications(string connectString)
    {
        // Initialize notifications
        SqlDependency.Start(connectString);
        // Create and register a new dependency
        SqlDependency dependency = new SqlDependency();
        dependency.OnChange += new OnChangeEventHandler(NotificationCallback);
        System.Runtime.Remoting.Messaging.CallContext.SetData("MS.SqlDependencyCookie", dependency.Id);
    }

    internal static void NotificationCallback(object o, SqlNotificationEventArgs args)
    {
        OnChange.Invoke(o, args);
    }
}

This is how I’m using it:

public partial class Nawa : Form
{
  public Nawa()
  {
    InitializeComponent();
  }

  private void Nawa_Load(object sender, EventArgs e)
  {
    GlobalNotifications.InitializeNotifications("Server=GENISYSSERVER; Trusted_Connection=no;database=Maple_DBv1; user id=sa; password=Wc123Wc123");
    GlobalNotifications.OnChange += new System.Data.SqlClient.OnChangeEventHandler(GlobalNotifications_OnChange);
  }

  void GlobalNotifications_OnChange(object sender, System.Data.SqlClient.SqlNotificationEventArgs e)
  {
    MessageBox.Show("Test");
  }

  private void button1_Click(object sender, EventArgs e)
  {
    using (DataClasses1DataContext dbcontext = new DataClasses1DataContext("Server=GENISYSSERVER; Trusted_Connection=no;database=Maple_DBv1; user id=sa; password=Wc123Wc123")) {
      OrderFood random = dbcontext.OrderFoods.FirstOrDefault(id => id.ID == 10);

      if (random != null) { 
        if (random.MenuID == 4)
          random.MenuID = 1;
        else
          random.MenuID = 4;

        dbcontext.SubmitChanges();
      }
    }
  }
}

Can someone help in this regard? How to get more details of what was changed, type of change, Table(s) changed, and why does it fire only once. Also, how can it understand LINQ changes only? It doesn’t trigger on direct changes etc.

Reference:
Extemporaneous Mumblings

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:34:31+00:00Added an answer on May 24, 2026 at 9:34 am

    A SQLDependency will only fire once, you need to recreate the dependency after it has fired.

    The OnChange event fires once and then gets consumed, so you need to hook up the event again after it fires.

    Dan Miser – SqlDependency

    I don’t have any C# code to hand to demonstrate how to deal with this but I’m sure some VB.NET should work well enough for you to come up with your own solution.

    Private _permissionsDependency As SqlDependency
    
    Private Sub doSubscribe()
        _permissionsDependency = New SqlDependency(cmd.InnerCommand)
        RemoveHandler _permissionsDependency.OnChange, AddressOf User_OnChange
        AddHandler _permissionsDependency.OnChange, AddressOf User_OnChange
    End Sub
    
    Private Sub User_OnChange(ByVal sender As Object, ByVal e As System.Data.SqlClient.SqlNotificationEventArgs)
        If _permissionsDependency IsNot Nothing Then RemoveHandler _permissionsDependency .OnChange, AddressOf User_OnChange
    
        Select Case e.Info
            Case SqlNotificationInfo.Delete
                RaiseEvent UserDeleted(Me)
            Case SqlNotificationInfo.Update
                populateUser()
                RaiseEvent UserUpdated(Me)
            Case Else
        End Select
    End Sub
    

    As you can see you can find out what happened by looking at e.Info, which will allow for you to know what happened (in my example I’m only looking for deletes and updates).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

after searching for a while I found the SQL query that gives me the
Spent a while searching on this and found nothing that related directly. I have
I'm sure this is discussed somewhere, but after searching for a while I can't
###Intro After puzzling over my code for a while, I discovered that exceptions don't
(I have just answered this question after searching for a while, so I am
This seems to be a simple problem, but after a while of searching I
After trying to get this to work for a while and searching around I
After a while using the website, loading contents, etc.. this message shows Fill: SelectCommand.Connection
I found similar questions while searching for a solution for my problem but I
After extensive searching on this matter, I have been unable to find an answer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.