Asked by:
sql dependency

Question
-
User-1169218742 posted
hello everyone,
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
thank you
protected void Page_Load(object sender, EventArgs e) { //Response.Write("<script>alert('" + msg + "')</script>"); SqlClientPermission permission = new SqlClientPermission( PermissionState.Unrestricted); try { permission.Demand(); } catch (System.Exception) { Response.Write("<script>alert('" + "no permission" + "')</script>"); } // string connectionString = "Data Source =.\\SQLEXPRESS; Initial Catalog = DIRECTORYDB; Integrated Security = True"; string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; // SqlDependency.Stop(constr); SqlDependency.Start(constr); using (SqlConnection con = new SqlConnection(constr)) { con.Open(); 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>"); }
Monday, April 18, 2016 11:25 PM
All replies
-
User36583972 posted
Hi matthnn,
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
http://techbrij.com/database-change-notifications-asp-net-signalr-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:
http://forums.asp.net/t/1971731.aspx?How+to+use+SqlDependency+in+asp+net+project
Best Regards,
Yohann Lu
Tuesday, April 19, 2016 5:07 AM -
User-1169218742 posted
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?
thank you!
Tuesday, April 19, 2016 9:29 PM -
User36583972 posted
Hi matthnn,
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:
http://ruchirac.blogspot.sg/2014/10/get-database-change-notification-in.html
Best Regards,
Yohann Lu
Thursday, April 28, 2016 10:42 AM