Thursday, November 24, 2011

Updating configuration parameters programmatically using ASP.Net

Download sample code
Introduction
In this article, we would see the method to update the key parameters in configuration file without causing the application instance.
This article assumes that the reader is already familiar with ASP.NET programming.

Keywords
web.config: Application configuration files contain settings specific to an application. This file contains the configuration settings that the common language runtime reads (such as the assembly binding policy, remoting objects, and so on), and settings that the application can read [MSDN].

xml: Extensible Markup Language (XML) is the universal format for data on the Web. XML allows developers to easily describe and deliver rich, structured data from any application in a standard, consistent way. XML does not replace HTML; rather, it is a complementary format [MSDN].

Requirement
According to the requirement we need to modify the values of the keys found in web.config programmatically or by providing configuration screen and also without re-instantiating Application instance (i.e. Application instance is not lost). So the user can modify the values of the keys for which he is allowed to do so.

Description
We all are familiar with ASP.Net programming and also the production environment of the website under Internet Information Services (IIS).
During the application life cycle, the application raises events that you can handle and calls particular methods like Application_Start and Application_End placed in Global.asax in the root directory of your application.
The Application_Start and Application_End methods are special methods that are called once for the lifetime of the application domain.
Here are some of the conditions in which the Applicaton_Start event is invoked.
  • On restarting of IIS.
  • Application pool of the website is restarted.
  • Updating the web.config file.
Solution

Add the new xml file in your solution by selecting Add New Item > Web Configuration file from dialog box.
Name this file whatever you like. Here I named it as “DataKey.config”.

 Web.config
  
 

In the above code snippet we have used configSource attribute of the appSettings tag. ConfigSource take the path of the another config file in the solution. And during the runtime the keys from config file are replaced in the appSettings section in web.config file.

 DataKey.config (custom configuration file)

  
  

Here there is no need for XML declaration, but the document element should be same as that we provide in web.config.

C# Code
System.Configuration.ConfigurationManager.AppSettings["merchantid"] 

System.Configuration.ConfigurationManager.AppSettings["merchantkey"]
Accessing the value of the key is same way we access from web.config.

C# Code
System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
xml.Load(Server.MapPath(@"~\DataKey.config"));  
System.Xml.XmlNode node = xml.SelectSingleNode("//appSettings/add[@key='" + txtKeyName.Text + "']");
node.Attributes["value"].Value = txtKeyValue.Text.Trim();
xml.Save(Server.MapPath(@"~\DataKey.config"));
Updating DataKey.config can be done using methods of System.Xml.XmlDocument.

So here the keys can be saved in XML file and can be modified without loosing the Application instance.

Hence we are done!!!

Learn by diving in Programming Ocean...
Happy Programming!!!

Tuesday, November 8, 2011

SSRS - Report Viewer - Hide PDF Export option

SQL Server Reporting Services eases designing of reports, whereas Microsoft Report Viewer control is used to display .rdl/.rdlc report into ASP.Net website.

Moreover report viewer control provides the various options in its toolbar. Of the different options, one option provides functionality of Exporting the report in different formats (like XML, CSV, Word, Excel, PDF ...)

Now what if one wants to exclude any/all of the above options?

Say if I want to hide PDF export option from Report Viewer toolbar. Below are the steps to do so.

Step - 1
Locate the file name "RSReportDesigner.config" at
"Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\RSReportDesigner.config".

Note: Location of the file varies in different version of SQL Server.

Step - 2
Disable PDF export option - add the visible attribute with value false
<Extension Name="PDF" Type="Microsoft.ReportingServices.Rendering.ImageRenderer.PDFRenderer,Microsoft.ReportingServices.ImageRendering" Visible="false" />


Enable PDF export option - remove the visible attribute
<Extension Name="PDF" Type="Microsoft.ReportingServices.Rendering.ImageRenderer.PDFRenderer,Microsoft.ReportingServices.ImageRendering" />


Step - 3
After this you need RESTART your Report Server.

This can be applied to other exporting options too.



Learn by diving in Programming Ocean...
Happy Programming!!!