Last post Apr 28, 2016 10:42 AM by Yohann Lu
Apr 18, 2016 11:25 PM|matthnn|LINK
so ive got an application the displays a sql table and i want changes to the table to show up whenever they are made without having to refresh the webpage
i thought i could use sql dependency for this and rebind the gridview whenever there is a onchangeevent. buuut the event is never firing. here is my code, can anyone tell me what im doing wrong
protected void Page_Load(object sender, EventArgs e)
//Response.Write("<script>alert('" + msg + "')</script>");
SqlClientPermission permission =
Response.Write("<script>alert('" + "no permission" + "')</script>");
// string connectionString = "Data Source =.\\SQLEXPRESS; Initial Catalog = DIRECTORYDB; Integrated Security = True";
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
using (SqlCommand command = new SqlCommand("SELECT First_Name FROM [dbo].[Table]", con))
command.Notification = null;
// Create a dependency and associate it with the SqlCommand.
SqlDependency dependency = new SqlDependency(command);
// Maintain the refence 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.
void OnDependencyChange(object sender, SqlNotificationEventArgs e)
// Handle the event (for example, invalidate this cache entry).
Response.Write("<script>alert('" + "test" + "')</script>");
Apr 19, 2016 05:07 AM|Yohann Lu|LINK
From your description, I suggest you can refer the following tutorials to implement your needs.
1: Database Change Notifications in ASP.NET using SignalR and SqlDependency
2: Auto Refresh GridView using SqlCacheDependency and Toolkit’s Timer.
The following post has the same question with you, you can refer it.
How to use SqlDependency in asp.net project:
Apr 19, 2016 09:29 PM|matthnn|LINK
it looks to me that ive done exactly what your links are saying to do but its still not working.
anyone see any problems that im not seeing?
Apr 28, 2016 10:42 AM|Yohann Lu|LINK
but its still not working.
I think you can check your database configuration is correct. for example, you should enable Service Broker for the database.
The following tutorial is a way to implement your needs, you can refer it.
Get database change notification in asp.net using SignalR: