{"id":43,"date":"2011-10-19T11:44:00","date_gmt":"2011-10-19T11:44:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2011\/10\/19\/notify-database-changes-to-asp-net-pages\/"},"modified":"2011-10-19T11:44:00","modified_gmt":"2011-10-19T11:44:00","slug":"notify-database-changes-to-asp-net-pages","status":"publish","type":"post","link":"http:\/\/panahy.nl\/index.php\/2011\/10\/19\/notify-database-changes-to-asp-net-pages\/","title":{"rendered":"Notify Database changes to ASP.NET pages"},"content":{"rendered":"<p>In the old times there were two ways of getting notifications from the database into the <a href=\"http:\/\/asp.net\/\">ASP.NET<\/a> pages.<\/p>\n<div>\n<ol>\n<li>writing to a file (simply create or delete an empty file) in a specific location where a CacheDependency can listen to and callBack a method on change.\n<\/li>\n<li>implement an HttpHandler that gets the notification with a parameter of what is changed, and let the database send an http request to the IIS server on a trigger.<\/li>\n<\/ol>\n<div>I am sure there could be worse methods but even these two have a lot of complications in a secured architecture with high performance requirements, like we have these days.<\/div>\n<\/p><\/div>\n<div><\/div>\n<div>Fortunately, since SQL-Servet 2000, there is a new component implemented into SQL-server called <b>Service Broker<\/b>\u00a0that act on changes in tables that are marked to be notified on change. Internally they write to a specific table mentioning about the change.<\/div>\n<div>In order to configure a database for Service Broker you can call a statement like this:<\/div>\n<div><\/div>\n<div>\n<div>Use Northwind<\/div>\n<div> ALTER DATABASE Northwind SET ENABLE_BROKER<\/div>\n<\/div>\n<div><\/div>\n<div>For more information about Service Broker see the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms171562(v=SQL.90).aspx\">MSDN<\/a>.<\/div>\n<div><\/div>\n<div>Next, you need to set the <a href=\"http:\/\/asp.net\/\">ASP.NET<\/a> to recieve the notification via dependencies. To do so , you need to add the command object to the dependency:<\/div>\n<div><\/div>\n<div>\n<div> \/\/ Create the dependency.<\/div>\n<div>SqlCacheDependency empDependency = new SqlCacheDependency(cmd);<\/div>\n<div>\/\/ Add a cache item that will be invalidated if one of its records changes<\/div>\n<div>\/\/ (or a new record is added in the same range).<\/div>\n<\/p><\/div>\n<div>Cache.Insert(&quot;Employees&quot;, ds, empDependency);<\/div>\n<div><\/div>\n<div>To start the notification on application service you need to add the following to the Global.ascx.cs in Application_Start<\/div>\n<div><\/div>\n<div>SqlDependency.Start(connectionString);<\/div>\n<div><\/div>\n<div>It is a good practice to stop the notifications on Application_End<\/div>\n<div><\/div>\n<div>SqlDependency.Stop(connectionString);<\/div>\n<div><\/div>\n<div>Once this is implemented, you can get the notifications through the OnChange event of the SqlDependency class.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In the old times there were two ways of getting notifications from the database into the ASP.NET pages. writing to a file (simply create or delete an empty file) in a specific location where a CacheDependency can listen to and callBack a method on change. implement an HttpHandler that gets the notification with a parameter &hellip; <a href=\"http:\/\/panahy.nl\/index.php\/2011\/10\/19\/notify-database-changes-to-asp-net-pages\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Notify Database changes to ASP.NET pages&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":0,"uagb_excerpt":"In the old times there were two ways of getting notifications from the database into the ASP.NET pages. writing to a file (simply create or delete an empty file) in a specific location where a CacheDependency can listen to and callBack a method on change. implement an HttpHandler that gets the notification with a parameter&hellip;","_links":{"self":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/43"}],"collection":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":0,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"wp:attachment":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}