<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>9tutorials - The best collection of tutorials &#187; C#</title>
	<atom:link href="http://9tutorials.com/category/c/feed" rel="self" type="application/rss+xml" />
	<link>http://9tutorials.com</link>
	<description>Photoshop tutorials , Flash tutorials, PHP tutorials and much more</description>
	<lastBuildDate>Sat, 20 Feb 2010 20:24:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ASP.NET Breadcrumbs with C#</title>
		<link>http://9tutorials.com/2007/11/10/aspnet-breadcrumbs-with-c.html</link>
		<comments>http://9tutorials.com/2007/11/10/aspnet-breadcrumbs-with-c.html#comments</comments>
		<pubDate>Sun, 11 Nov 2007 06:44:00 +0000</pubDate>
		<dc:creator>dangtruong</dc:creator>
				<category><![CDATA[ASP]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://9tutorials.com/2007/11/10/aspnet-breadcrumbs-with-c.html</guid>
		<description><![CDATA[By utilizing breadcrumb navigation you allow your viewers to easily trace their path taken to the current location and back track if necessary.
A breadcrumb navigation usually looks something like:
Main Page &#62; Page Design : ASP.NET Breadcrumbs with C#
Within this article you will learn how to create breadcrumb navigation using ASP.NET and C#.
In the above example [...]]]></description>
			<content:encoded><![CDATA[<div class="spost"><p><span id="lblSubTitle" class="v11">By utilizing breadcrumb navigation you allow your viewers to easily trace their path taken to the current location and back track if necessary.</span><span id="more-3535"></span></p>
<p>A breadcrumb navigation usually looks something like:</p>
<p><strong>Main Page &gt; Page Design : ASP.NET Breadcrumbs with C#</strong></p>
<p>Within this article you will learn how to create breadcrumb navigation using ASP.NET and C#.</p>
<p>In the above example we see that the viewer started on the â€™Main Pageâ€™, then moved to the â€˜Page Designâ€™ section, then selected the â€˜ASP.NET Breadcrumbs with C#&#8217; page, where they currently reside. The Main Page and Page Design will be hyperlink so that you can go directly to the above sections.</p>
<p>The first part, â€˜Main Pageâ€™ is where the user is starting; typically something like â€˜Homeâ€™ or â€˜Home Pageâ€™ will be used. However, we will use a variable in our code so that the user may decide what text they would like to use. The next part â€˜Page Designâ€™ is a sub-category that holds the page that the user is currently viewing, â€˜ASP.NET Breadcrumbs with C#â€™. We know that this is the final resting spot because the â€˜:â€™ preceding the page name indicates so. Finally, the â€˜&gt;â€™ is used as a separator for categories, this also is a variable, allowing for a choice of symbol.</p>
<p>[source: c#]</p>
<p>using System;<br />
using System.Web.UI;<br />
using System.Web.UI.WebControls;<br />
using System.ComponentModel;<br />
using System.Text;<br />
// We include the System.Text namespace so we can use the StringBuilder object later.<br />
namespace BreadCrumbs<br />
{<br />
///<br />
/// Summary description for ctrlBreadCrumbs.<br />
///<br />
public class ctrlBreadCrumbs : System.Web.UI.WebControls.WebControl<br />
{<br />
///<br />
/// The 3 variables below, Separator, RootName, directoryNameSpacer can be changed to meet your needs<br />
/// PageTitle is pulled from the CodeBehind of the page<br />
///<br />
public string Separator = &#8221; &gt; &#8220;;<br />
public string RootName = &#8220;Home Page&#8221;;<br />
public char directoryNameSpacer = &#8216;_&#8217;;<br />
private string _PageTitle;</p>
<p>// Above are our variables that we can set:<br />
// Separator is the â€˜&gt;â€™ symbol<br />
// RootName is the homepage anchor text<br />
// directoryNameSpacer is the naming scheme for my directories, for example: search_engine_optimization or website_design (notice the URL in the address bar above)</p>
<p>public string PageTitle<br />
{<br />
get<br />
{<br />
return _PageTitle;<br />
}<br />
set<br />
{<br />
_PageTitle = value;<br />
}<br />
}<br />
///<br />
/// Render this control to the output parameter specified.<br />
///<br />
///</p>
<p>[/source]</p>
<p>The HTML writer to write out to</p>
<p>[source: c#]<br />
protected override void Render(HtmlTextWriter output)<br />
{<br />
StringBuilder sbResult = new StringBuilder();<br />
// sbResult StringBuilder will hold the breadcrumb navigation when done</p>
<p>// get the url root, like www.domain.com<br />
string strDomain = Page.Request.ServerVariables["HTTP_HOST"].ToString();<br />
strDomain.Trim(); // Trim removes leading and trailing whitespace<br />
sbResult.Append ( &#8220;&#8221; + RootName + &#8220;&#8221; + Separator );</p>
<p>// gets dir(s), like subdirectory/subsubdirectory/file.aspx<br />
string scriptName = Page.Request.ServerVariables["SCRIPT_NAME"].ToString();<br />
// find the last &#8216;/&#8217; and Remove the text after it as it&#8217;s the file name<br />
int lastSlash = scriptName.LastIndexOf(&#8216;/&#8217;); // returns the # of chars. from right to /<br />
string pathOnly = scriptName.Remove(lastSlash, (scriptName.Length &#8211; lastSlash));</p>
<p>// create breadcrumb HTML for the directory name(s)<br />
// We Remove the first &#8220;/&#8221; otherwise when you split the string the first item in array is empty<br />
pathOnly = pathOnly.Substring(1);<br />
string[] strDirs = pathOnly.Split(&#8216;/&#8217;);<br />
int nNumDirs = strDirs.Length;</p>
<p>// URLs for breadcrumbs<br />
string strURL = &#8220;&#8221;;<br />
for (int i=0; i<br />
{<br />
strURL += &#8220;/&#8221;+strDirs[i];</p>
<p>// convert underscores to spaces<br />
strDirs[i] = strDirs[i].Replace(directoryNameSpacer,&#8217; &#8216;);</p>
<p>int counter = i+1;<br />
if (counter != nNumDirs)<br />
{<br />
sbResult.Append ( &#8220;&#8221; + strDirs[i] + &#8220;&#8221; + Separator );<br />
}<br />
else<br />
{<br />
// This is the last directory so don&#8217;t tack on Separator<br />
sbResult.Append ( &#8220;&#8221; + strDirs[i] + &#8220;&#8221; );    }<br />
}<br />
// write the PageTitle, pulled from the CodeBehind!<br />
sbResult.Append ( &#8221; : &#8221; + this.PageTitle );</p>
<p>output.Write ( sbResult.ToString() );</p>
<p>}<br />
}<br />
}<br />
[/source]</p>
<p>We are going to use an ASP.NET User control (.ascx) so that we may simply add this control to our pages making it easy to maintain and modify later if we should choose to. For example, if we have 100 pages and we want to modify the separator in our breadcrumb navigation from the â€œ&gt;â€ symbol to an image we will only have to modify 1 page, our .ascx page and not all 100 .aspx pages.</p>
<p>[source: c#]</p>
<p>&lt;%@ Register TagPrefix=&#8221;bc&#8221; Namespace=&#8221;BreadCrumbs&#8221; Assembly=&#8221;BreadCrumbs&#8221; %&gt;</p>
<p>protected BreadCrumbs.ctrlBreadCrumbs bc1;</p>
<p>private string _strHeaderText;<br />
public string HeaderText<br />
{<br />
get<br />
{<br />
return _strHeaderText;<br />
}<br />
set<br />
{<br />
_strHeaderText = value;<br />
}<br />
}</p>
<p>private void Page_Load(object sender, System.EventArgs e)<br />
{<br />
// Put user code to initialize the page here<br />
bc1.PageTitle = HeaderText;<br />
}<br />
[/source]</p>
<p>Creating Our Web Pages (.aspx). For each page (.aspx) you create for your website use a literal . This allows us to define the actual title for our page in the code behind. By defining the title of the page in the code behind we gain the benefit of only having to assign it once and have it not only be used for the title of our page, but also be used in our breadcrumb navigation.</p>
<p>Your code behind would look like this:</p>
<p>[source:c#]</p>
<p>public class CodeBehind_for_page: System.Web.UI.Page<br />
{<br />
protected System.Web.UI.WebControls.Literal lblPageTitle;<br />
protected NameSpace.headerBreadCrumb header;</p>
<p>private void Page_Load(object sender, System.EventArgs e)<br />
{<br />
// Put user code to initialize the page here<br />
string PageTitle = &#8220;ASP.NET Breadcrumbs with C#&#8221;;<br />
lblPageTitle.Text = PageTitle;<br />
header.HeaderText = PageTitle;<br />
}<br />
}</p>
<p>[/source]</p>
<p>We created a class for our breadcrumb navigation and used that class for an ASP.NET User control (.aspx). This allows us to simply drop the User control into each web page (.aspx) we create. Once we have our User control in our page we assign the PageTitle in the code behind and this gets used for the site.</p>
<p><em><strong>Copyright @ Abhishek Arya  2007</strong></em></p>
]]></content:encoded>
			<wfw:commentRss>http://9tutorials.com/2007/11/10/aspnet-breadcrumbs-with-c.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Edit and Encrypt Web.Config sections using C# 2.0</title>
		<link>http://9tutorials.com/2007/05/19/edit-and-encrypt-webconfig-sections-using-c-20.html</link>
		<comments>http://9tutorials.com/2007/05/19/edit-and-encrypt-webconfig-sections-using-c-20.html#comments</comments>
		<pubDate>Sat, 19 May 2007 17:35:35 +0000</pubDate>
		<dc:creator>dangtruong</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[encrypt]]></category>
		<category><![CDATA[web-develop]]></category>

		<guid isPermaLink="false">http://9tutorials.com/2007/05/19/edit-and-encrypt-webconfig-sections-using-c-20.html</guid>
		<description><![CDATA[o
]]></description>
			<content:encoded><![CDATA[<div class="spost"><p><span class="v11" id="lblSubTitle">An article to illustrate editing and encrypting of sections of Web.Config file programatically. It is illustrated with an ASP.NET 2.0 application, which can edit and encrypt sections of Web.Config.<br />
<span id="more-218"></span><br />
</span><em>.NET Classes used :</em></p>
<p><em># System.Web.Security;<br />
# System.Configuration;<br />
# System.Web.Configuration;</em></p>
<p><strong>Introduction</strong></p>
<p>ASP.NET 1.x allowed configurations in web.config file to be read from .NET application. But there were o options to manipulate Web.Config contents in programatically. To achieve this we had to consider Web.Config file as a normal file or an xml file. .NET 2.0 fills this gap and lso provides many other useful operations to be carried out on Web.Config file; like<br />
editing and encrypting sections of Web.Config file. This articles illustrates these functionalities ia a sample ASP.NET application.</p>
<p><strong>Using the code</strong></p>
<p>The classes and methods to take control of the Web.Config file span across 2 namespaces.</p>
<p>1.  System.Configuration<br />
2. System.Web.Configuration</p>
<p>Each section in the Web.Config file has a corresponding class in either of the namespace. These classes allow modification of corresponding sections. The classes for sections within the &#8220;system.web&#8221; section are found in System.Web.Configuration. Classes for other sections that are not specific to Web.Config are found in System.Configuration.</p>
<p><strong>Steps to modify a section in Web.Config      </strong><br />
1.  Open Web.Config for editing using WebConfigurationManager class.<br />
2. Using respective Configuration class, bring about the necessary changes.<br />
3. Save changes to the physical file using Configuration class.</p>
<p>[source:c#]</p>
<p>private void UpdateConfig(string strKey, string strValue)<br />
{<br />
Configuration objConfig = WebConfigurationManager.OpenWebConfiguration(&#8220;~&#8221;);<br />
AppSettingsSection objAppsettings = (AppSettingsSection)objConfig.GetSection(&#8220;appSettings&#8221;);<br />
if (objAppsettings != null)<br />
{<br />
objAppsettings.Settings[strKey].Value = strValue;<br />
objConfig.Save();<br />
}<br />
}</p>
<p>[/source]</p>
<p>In the above piece of code, OpenWebConfiguration() method of WebConfigurationManager   class opens Web.Config file in the root directory and returns it as a Configuration object. GetSection() method of Configuration class accepts path to a specific section as argument. The path is the relative path from the root node &#8220;configuration&#8221;. You can refer to deeper nodes(sections in our context) by their names separated by â€™/â€™. For example, to get access to the &#8220;authentication&#8221; section, provide &#8220;system.web/authentication&#8221; as the parameter to GetSection() method. It returns a generic ConfigurationSecton object, which can be typecasted to proper configuration section class. In our example we get hold of the &#8220;appSettings&#8221; section with the help of AppSettingsSection class. AppSettingsSection class instance has a Settings collection property which contains application setting from the configuration section as key-value pairs. The Settings property can be indexed using key to get the corresponding value. You can also set the value property and call the Save() method of the Configuration object to write configurations in the Configuration instance to config file.<br />
<!--adsense#inside--><br />
<strong>To delete an entry in the Web.config file:</strong><br />
The Remove() method of Settings collection deletes an entry from the Configuration instance. Remove() method accepts key of the entry to be deleted.</p>
<p><em>Note:          </em><br />
Please do not forget to call the Save() method of the Configuration instance to get the changes reflected in the physical file.</p>
<p>[source:c#]</p>
<p>objAppsettings.Settings.Remove(&#8220;Location&#8221;);</p>
<p>[/source]</p>
<p>To iterate through all the key-value pairs in a configuration section, access the string array of keys via All Keys property of Settings collection.</p>
<p>[source:c#]</p>
<p>foreach (string strKey in objAppsettings.Settings.AllKeys)<br />
{<br />
DataRow dr = dt.NewRow();<br />
dr["Key"] = strKey;<br />
dr["Value"] = objConfig.AppSettings.Settings[strKey].Value;<br />
dt.Rows.Add(dr);<br />
}</p>
<p>[/source]</p>
<p><strong>Encrypting sections in Web.Config file</strong></p>
<p>Now comes the security issues. At times there comes the necessity for protecting sections of config file. In .NET 2.0 there are options available to encrypt sections of Web.config file programatically. The following method encrypts the &#8220;appSettings&#8221; section in Web.config file.</p>
<p>[source:c#]</p>
<p>private void EncryptAppSettings()<br />
{<br />
Configuration objConfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);<br />
AppSettingsSection objAppsettings = (AppSettingsSection)objConfig.GetSection(&#8220;appSettings&#8221;);<br />
if (!objAppsettings.SectionInformation.IsProtected)<br />
{<br />
objAppsettings.SectionInformation.ProtectSection(&#8220;RsaProtectedConfigurationProvider&#8221;);<br />
objAppsettings.SectionInformation.ForceSave = true;<br />
objConfig.Save(ConfigurationSaveMode.Modified);<br />
}<br />
}</p>
<p>[/source]</p>
<p>The code above opens Web.Config file for modification. It then retrieves the &#8220;appSettings&#8221; section. The ProtectSection() method of SectionInformation class marks the configuration section for protection. It accepts the name of the protection provider to be used for the encryption. The ForceSave property indicates if the specified configuration section will be  saved even if it has not been modified. Finally the Save() of the Configuration object writes the configuration settings to the Web.config file. The argument to the Save() method indicates the only properties modified<br />
need to be written to the physical file.</p>
<p>Decrypting sections of web.config file through code is very identical. The UnprotectSection() method of SectionInformation class removes the encryption from he configuration section.</p>
<p>[source:c#]</p>
<p>private void DecryptAppSettings()<br />
{<br />
Configuration objConfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);<br />
AppSettingsSection objAppsettings = (AppSettingsSection)objConfig.GetSection(&#8220;appSettings&#8221;);<br />
if (objAppsettings.SectionInformation.IsProtected)<br />
{<br />
objAppsettings.SectionInformation.UnprotectSection();<br />
objAppsettings.SectionInformation.ForceSave = true;<br />
objConfig.Save(ConfigurationSaveMode.Modified);<br />
}<br />
}</p>
<p>[/source]</p>
<p>This encrytion and decryption functionality can be applied to other sections of web.config file also. It comes in use mostly for &#8220;connectionStrings&#8221; section where usually the user name and password would be specified. This can done by creating a ConfigurationSection object. An example for &#8220;connectionStrings&#8221; section is listed below.</p>
<p>[source:c#]</p>
<p>ConfigurationSection objConfigSection = objConfig.ConnectionStrings;</p>
<p>[/source]</p>
<p>ConfigurationSection class represents a section within the configuration file. Configuration class has propertes for each configuration section. This property can be used to get respective ConfigurationSection objects. This is an alternative to the usage of GetSection() method of Configuration class.</p>
<p><em> Copyright @ Mohammed Habeeb 2007</em></p>
]]></content:encoded>
			<wfw:commentRss>http://9tutorials.com/2007/05/19/edit-and-encrypt-webconfig-sections-using-c-20.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Implementing SQL Server 2005 Query Notifications in C# Windows Application</title>
		<link>http://9tutorials.com/2007/05/10/implementing-sql-server-2005-query-notifications-in-c-windows-application.html</link>
		<comments>http://9tutorials.com/2007/05/10/implementing-sql-server-2005-query-notifications-in-c-windows-application.html#comments</comments>
		<pubDate>Thu, 10 May 2007 08:36:45 +0000</pubDate>
		<dc:creator>dangtruong</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[grid]]></category>
		<category><![CDATA[sql-server]]></category>

		<guid isPermaLink="false">http://9tutorials.com/2007/05/10/implementing-sql-server-2005-query-notifications-in-c-windows-application.html</guid>
		<description><![CDATA[Query notification is a feature included in Microsoft SQL Server 2005 that allows applications to be notified when data has changed. It is mainly used for applications that stores cache of data from a database and get refreshed and notified in the client application everytime the data changes in the database.
Developers in ASP.NET 2.0 are [...]]]></description>
			<content:encoded><![CDATA[<div class="spost"><p>Query notification is a feature included in Microsoft SQL Server 2005 that allows applications to be notified when data has changed. <span id="more-34"></span>It is mainly used for applications that stores cache of data from a database and get refreshed and notified in the client application everytime the data changes in the database.</p>
<p>Developers in ASP.NET 2.0 are already familiar with the use the SqlCacheDependency classes in their web applications to buffer SQL Data. This article deals with the  high-level implementation as provided by the SqlDependency class in System.Data.SqlClient Namespace. It provides a simple and elegant notification functionality between the windows application and SQL Server, enabling you to use a dependency to detect changes in the server. It effectively leverages the  SQL Server 2005 notifications capability to the managed client applications using ADO.NET.</p>
<p>SQL Server 2005 allows ADO.NET Windows applications to send a command to SQL Server and request that a notification be generated if executing the same SQL command would produce result sets different from those initially retrieved. Query Notification is supported by the database&#8217;s Service Broker Event and Queueing Mechanisms.</p>
<p><strong>Step-By-Step approach in implementing Query Notifications</strong></p>
<p>The whole process of request-receive notification process between the client windows application and the SQL Server 2005 is done in five little steps as follows:</p>
<p><strong>1) The database that is in question must be configured to enable query notification services.</strong></p>
<p>For security reasons, SQL Server 2005 databases do not have Service Broker enabled by default. To enable query notifications for your database, in the SQL SERVER 2005 MAnagement Studio, Query window, run the following command:</p>
<p>ALTER DATABASE SET ENABLE_BROKER;</p>
<p>In this article, I uses the database named AdvtDB</p>
<p>ALTER DATABASE AdvtDB SET ENABLE_BROKER;</p>
<p><strong>2) The user must have the correct client and server side permissions to request and receive notifications.</strong></p>
<p>Users who execute commands requesting notification must have SUBSCRIBE QUERY NOTIFICATIONS database permission on the server.Client-side code that runs in a partial trust situation requires the SqlClientPermission.</p>
<p>[source:c#]<br />
private bool EnoughPermission()<br />
{<br />
SqlClientPermission perm = new SqlClientPermission(System.Security.Permissions.PermissionState.Unrestricted);<br />
try<br />
{<br />
perm.Demand();<br />
return true;<br />
}<br />
catch (System.Exception)<br />
{<br />
return false;<br />
}<br />
}</p>
<p>[/source]</p>
<p><strong>3) Use a SqlCommand object to execute a valid SELECT statement with an associated notification objectâ€”SqlDependency.</strong></p>
<p>SqlDependency Object The query notifications API provides SqlDependency object to process notifications. When using SqlDependency,  Service Broker objects, such as the notification queue, are predefined. It automatically launches a worker thread to process notifications as they are posted to the queue; it also parses the Service Broker message, exposing the information as event argument data. SqlDependency must be initialized by calling the Start method to establish a dependency to the database. This is a static method that need be called only once during application initialization for each database connection required. The Stop method should be called at application termination for each dependency connection that was made.<br />
<!--adsense#inside--><br />
On Click of a button, database connection is established, SqlDependency is started to listen the notification service and a DataGridView Control is displayed with current results returned from a query executed by the command object.</p>
<p>[source:c#]</p>
<p>private void button1_Click(object sender, EventArgs e)<br />
{</p>
<p>// Remove any existing dependency connection, then create a new one.<br />
connstr = &#8220;Data Source=localhost;Integrated Security=SSPI;Initial Catalog=AdvtDB;&#8221;;<br />
string ssql = &#8220;select * from advt &#8220;;</p>
<p>SqlDependency.Stop(connstr);<br />
SqlDependency.Start(connstr);<br />
if (connection == null)<br />
connection = new SqlConnection(connstr);<br />
if (command == null)<br />
command = new SqlCommand(ssql , connection);<br />
if (myDataSet == null)<br />
myDataSet = new DataSet();<br />
GetAdvtData();<br />
}</p>
<p>[/source]</p>
<p>GetAdvtData() is a helper function that creates the command object instance, associate it with the SqlDependency object. Note that the SqlDependency object has an OnChange event notifying the client that changes has been done in the database and its eventhandler dependency_OnChange will take care of the receiving part of the notification.</p>
<p>[source:c#]</p>
<p>private void GetAdvtData()<br />
{<br />
myDataSet.Clear();<br />
// Make sure the command object does not already have a notification object associated with it.<br />
command.Notification = null;<br />
// Create and bind the SqlDependency object to the command object.<br />
SqlDependency dependency =new SqlDependency(command);<br />
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);</p>
<p>using (SqlDataAdapter adapter =  new SqlDataAdapter(command))<br />
{<br />
adapter.Fill(myDataSet, &#8220;Advt&#8221;);<br />
dataGridView1.DataSource = myDataSet;<br />
dataGridView1.DataMember = &#8220;Advt&#8221;;<br />
}<br />
}</p>
<p>[/source]</p>
<p><strong>4) Provide code to process the notification when and if the data being monitored changes.</strong></p>
<p>The worker thread process encapsulates the OnChange Event handler and hence, the UI changes(updating the datagrid, displaying the status message) available in the Main thread might not be accessible here. Create another delegate thread UIDelegate that does these tasks and let the<br />
worker thread removes just the handler from the OnChange event of the SqlDependency object.</p>
<p>[source:c#]<br />
delegate void UIDelegate();<br />
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)<br />
{<br />
UIDelegate uidel = new UIDelegate(RefreshData);<br />
this.Invoke(uidel, null);</p>
<p>//Remove the handler as it is used for a single notification.<br />
SqlDependency dependency =(SqlDependency)sender;<br />
dependency.OnChange -= dependency_OnChange;<br />
}<br />
private void RefreshData()<br />
{<br />
// At this point, the code is executing on the UI thread, so it is safe to update the UI.</p>
<p>label1.Text = &#8220;Database had some changes and are applied in the Grid&#8221;;</p>
<p>// Reload the dataset that is bound to the grid.<br />
GetAdvtData();<br />
}</p>
<p>[/source]</p>
<p><strong>5) Stop the SqlDependency Notification Services while quitting the application.</strong></p>
<p>In the form_closing event, you include the code for stopping the SqlDepenency notification listener service.</p>
<p>[source:c#]</p>
<p>private void Form1_FormClosing(object sender, FormClosingEventArgs e)<br />
{<br />
SqlDependency.Stop(connstr);<br />
if (connection != null)<br />
connection.Close();<br />
}</p>
<p>[/source]</p>
<p>Following are the declarations used in this program.</p>
<p>[source:c#]</p>
<p>private const string statusMessage;<br />
private DataSet myDataSet = null;<br />
private SqlConnection connection = null;<br />
private SqlCommand command = null;<br />
private string connstr;</p>
<p>[/source]</p>
<p><strong>Testing the application</strong></p>
<p>Run the C# Windows application you have just created following the above steps. When the form shows up, Click the button and notice that your datagridview control is populated.</p>
<p>Now, Open the Query window of the SQL Server Management studio, and insert rows or update columns in the table concerned. When you switch back to your C# running application, you could see the changes appeared in the datagrid view.</p>
<p><em>Copyright @  Balamurali Balaji  2007 </em></p>
]]></content:encoded>
			<wfw:commentRss>http://9tutorials.com/2007/05/10/implementing-sql-server-2005-query-notifications-in-c-windows-application.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
