title: SqlDependency.OnChange not firing in WinForm?
body: <p>I used <a href="https://msdn.microsoft.com/en-us/library/62xk7953(v=vs.110).aspx" rel="nofollow">Detecting Changes with SqlDependency</a> as example for the code that I'm writing. I've also looked at other links with similar code, but none of them work. </p>
  
  <p>Essentially, I simply want to change <code>label1.Text</code> when a change has been made to table <code>[ErrorLog]</code>. For some reason, <code>OnDependencyChange</code> is not firing.</p>
  
  <p>I've enabled <code>Service Broker</code> in the database: </p>
  
  <pre><code>ALTER DATABASE TestDB 
  SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE
  </code></pre>
  
  <p>Now, here's my complete code. It's very short:</p>
  
  <pre><code>public partial class Form1 : Form
  {
      private string GetConnectionString()
      {
          return @"Data Source=USER-PC\SQLEXPRESS;Initial Catalog=TestDB;Persist Security Info=True;User ID=TestUser;Password=12345;";
      }
  
      SqlConnection connection;
  
      public Form1()
      {
          InitializeComponent();
  
          connection = new SqlConnection(GetConnectionString());
          connection.Open();
  
          SqlDependency.Start(GetConnectionString());
          i = 0;
      }
  
      int i;
  
      void OnDependencyChange(object sender, SqlNotificationEventArgs e)
      {
          i++;
          label1.Text = "Changed: " + i.ToString();
          // Handle the event (for example, invalidate this cache entry).
      }
  
      void SomeMethod()
      {
          // Assume connection is an open SqlConnection.
          // Create a new SqlCommand object.
          using (SqlCommand command = 
              new SqlCommand("SELECT [ErrorLog].[ID],[ErrorLog].[Project],[ErrorLog].[Form],[ErrorLog].[Message],[ErrorLog].[Exception],[ErrorLog].[InsertDate] " + 
              "FROM [dbo].[ErrorLog]", connection))
          {
              // Create a dependency and associate it with the SqlCommand.
              SqlDependency dependency = new SqlDependency(command);
  
              // Maintain the reference in a class member.
              // Subscribe to the SqlDependency event.
              dependency.OnChange += new OnChangeEventHandler(OnDependencyChange);
  
              // Execute the command.
              using (SqlDataReader reader = command.ExecuteReader())
              {
                  // Process the DataReader.
              }
          }
      }
  }
  </code></pre>
  
  <p>I checked if service broker is enabled and it is; the following returns 1: </p>
  
  <pre><code>SELECT is_broker_enabled 
  FROM sys.databases 
  WHERE name = 'TestDB';
  </code></pre>
  
  <p>Any help is appreciated.</p>
  
  <p>Thanks.</p>