dataset and sql query notification
Hi all;
how to refresh the dataset and reflect the changes on page using sql2005 query notification.
I can get the event of change in database in the code behind.
but i'm not getting a way to reflect the changes in current dataset and display new changes on page.
delegate void UIDelegate();
protected void Page_Load(object sender, EventArgs e)
{
string connstring = "Server=work02025;User ID=sa;pwd=sa;database=Portal";
SqlConnection SQLConnectionObject = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand("SELECT EmpID, FName, " +
"Address FROM dbo.EmployeeMaster", SQLConnectionObject);
SqlDependency depend = new SqlDependency(cmd);
SqlDependency.Start(connstring);
depend.OnChange += new OnChangeEventHandler(MyOnChanged);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
Cache.Insert("MyDataSet", ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
private void MyOnChanged(object sender, SqlNotificationEventArgs e)
{
try
{
//to test place a break point here
//and change any data in the specified table
string msg = "Notified";
UIDelegate uidel = new UIDelegate(RefreshData);
// this..Invoke(uidel, null);
uidel.Invoke();
//Remove the handler as it is used for a single notification.
SqlDependency dependency = (SqlDependency)sender;
dependency.OnChange -= MyOnChanged;
//RefreshData();
}
catch (Exception ex)
{
// Response.Write(ex.Message);
}
}
private void RefreshData()
{
// Since the code is executing on the UI thread,it is safe to update the UI.
Label1.Text = "Database had some changes and are applied in the Grid";
GridView1.DataSource = null;
GridView1.DataBind();
// Reload the dataset that is bound to the grid.
// GetAdvtData();
}
this is the test code.
after first page load, if there is change in database then it goes to "MyOnChanged"
but even if Label1.Text has been changed there, it doesn't appear on page!!!!
any solution?
|