Well, basically this is the problem.
First, I couldn't get it to work (database moved from another computer - owner issues - solved), now I can't get it to stop.
I have a table called birouri and I want to let's say update all the datagridviews of the clients where there's a change in the table.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Permissions;
namespace TicheteC
{
public partial class formBirouri : Form
{
private SqlCommand mCommand;
private string mSqlConnectString;
private int mChangeCount;
#region Permisiuni
private static bool CanRequestNotifications()
{
try
{
SqlClientPermission perm = new SqlClientPermission(PermissionState.Unrestricted);
perm.Demand();
return true;
}
catch
{
return false;
}
}
private void GetData()
{
this.ticheteDataSet.Clear();
mCommand = this.birouriTableAdapter.SelectCommand;
mCommand.Notification = null;
SqlDependency dependency;
dependency = new SqlDependency(mCommand);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
this.birouriTableAdapter.Fill(this.ticheteDataSet.birouri);
InfotoolStripStatusLabel.Text = String.Format("{0} change(s).", mChangeCount);
}
#endregion
public formBirouri()
{
InitializeComponent();
}
private void Birouri_Load(object sender, EventArgs e)
{
if (CanRequestNotifications() == true)
{
mSqlConnectString = this.birouriTableAdapter.Connection.ConnectionString;
SqlDependency.Stop(mSqlConnectString);
SqlDependency.Start(mSqlConnectString);
GetData();
}
}
private void birouriBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.birouriBindingSource.EndEdit();
this.birouriTableAdapter.Update(this.ticheteDataSet.birouri);
}
private void Birouri_FormClosed(object sender, FormClosedEventArgs e)
{
SqlDependency.Stop(mSqlConnectString);
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
ISynchronizeInvoke i = (ISynchronizeInvoke)this;
if (i.InvokeRequired)
{
OnChangeEventHandler tempDelegate = new OnChangeEventHandler(dependency_OnChange);
object[] args = { sender, e };
i.BeginInvoke(tempDelegate, args);
return;
}
SqlDependency dependency = (SqlDependency)sender;
dependency.OnChange -= dependency_OnChange;
++mChangeCount;
GetData();
}
}
}
The problem is that the datagridview keeps on updating. Can anyone see if there's something wrong with the code ?