سلام و وقت بخیر

میخوام با استفاده از SQL Dependency ، یه گرید ویو رو در صورتی که دیتا جدید ارسال شد یا ویرایش شد ، ریفرش کنم،

از این لـــــیـــــنــــک که مربوط به ماکروسافت هست استفاده کردم ، ولی جواب نگرفتم.

قسمتی از کد های بنده به این صورت هستند .

در قسمت تابع سازنده و بعد از InitializeComponent() این کد نوشتم :

SqlDependency.Start(db.CreateConnectionString(), "ItemChangeQueue");


یه متد FillGrid() درست کردم که کد های اصلی و پر کردن GridView انجام میده

private void FillGrid()
{
if (!DoesUserHavePermission())
return;

using (SqlConnection cn = new SqlConnection(db.CreateConnectionString()))
{
using (SqlCommand cmd = cn.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "dbo.LoadGrid";
cmd.Notification = null;
SqlDependency dep = new SqlDependency(cmd);
dep.OnChange += new OnChangeEventHandler(OnDependencyChange);
if (cn.State == ConnectionState.Closed)
cn.Open();
// Get the messages
//dt.Load(cmd.ExecuteReader(CommandBehavior.CloseCon nection));
using (SqlDataReader reader = cmd.ExecuteReader())
{
DataTable dtGrid = new DataTable();
dtGrid.Load(reader);
gridControl1.Invoke(new Action(() => gridControl1.DataSource = dtGrid));
gridControl1.Invoke(new Action(() => gridControl1.ForceInitialize()));
gridControl1.Invoke(new Action(() => gridView1.BestFitColumns()));
}
}
}
}


یه متد دیگه هم برای زمانی که دیتا تغییر میکنه


private void OnDependencyChange(object sender, SqlNotificationEventArgs e)
{
// Handle the event (for example, invalidate this cache entry).
DataTable Grid = DBExecuteCommand.DbInstance.MainGrid();
if (Grid != null)
{
gridControl1.Invoke(new Action(() => gridControl1.DataSource = Grid));
gridControl1.Invoke(new Action(() => gridControl1.ForceInitialize()));
gridControl1.Invoke(new Action(() => gridView1.BestFitColumns()));
}
}


خطایی که دریافت میکنم اینه
System.InvalidOperationException: 'When using SqlDependency without providing an options value, SqlDependency.Start() must be called prior to execution of a command added to the SqlDependency instance.'
جالب اینجا است که وقتی ItemChangeQueue رو از متد SQL Dependency .start بر میدارم درست میشه ، ولی با تغییر کوری مثل Update ، و .... ، گرید ریفرش نمیشه

ممنون میشم راهنمایی بفرمایید