Monthly Archives: June 2013

Access HttpContext.Current from WCF

Wcf doesnt have an HttpContext. if you want to store data in a user session all you need to do is set teh InstanceContextMode of the service class to Session, then use a channel that supports sessions such as wsHttpBinding. What this causes is a new instnace of teh service to be created per session. By default a new instance of the service is created with each and every call. So if a new instance of the service is created per session, then that means that all of the local varaibles in the service are implicitly per session. So you can just store the values there.

You can get access to HttpContext.Current by enabling AspNetCompatibility, preferably via configuration:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

You can even enforce AspNetCompatibility by decorating your service class with an additional attribute
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

Error Message: Retrieving the COM class factory for component with CLSID {C5CBD7B2-490C-45F5-8C40-B8C3D108E6D7} failed due to the following error: 80040154 Class not registered

This error is because of x64 bit machine. To fix this error, Right-click the project node in Solution Explorer and click Properties. Click on Configuration Properties.Click on Configuration Manager. click on New under Platform and change it to x64 and see if it fixes the problem. If it doesn’t then change the platform to x86.

Steps to create and deploye Windows Service

To create and deploy the WCF Service as the Windows Service in step by step. The following article describe about.

  1. Steps to Create Windows Service using WCF
  2. Steps to Deploy Windows Service using installutil.exe

Steps to Create Windows Service using WCF

  • Create new project using VS2012, Select File > New Project from the Menu.

New Project

  • Select Windows Service template from New Project dialog.

Create Winodw Service

  • Right click on designer and select Property and set ServiceName and Name

ServiceInstaller Property

  • Drag an EventLog component to the designer, from the Components section of the Toolbox.

Drag and Drop EventViewer in Design screen

  • Press F5 and use the below code.

Sample Code

public MyFirstWindowsService()
            if (!System.Diagnostics.EventLog.SourceExists("WSMessage"))
                    "WSMessage", "NewWSLog");
            eventLog1.Source = "WSMessage";
            eventLog1.Log = "NewWSLog";


        protected override void OnStart(string[] args)
            string str = "Service Start at :" + DateTime.Now.ToString();


        protected override void OnStop()
            string str = "Service Stopped at :" + DateTime.Now.ToString();

Now Windows service is ready and it is ready to write message to Event handler while start and stop event is triggered.

Steps to Deploy Windows Service using installutil.exe

  • Right-click Service1.cs and select View Designer from Solution Explorer

Select Property for Service.cs file from solution explorer

  • Right-click, and then click Add Installer in View Designer Screen. The two installers is added to your project inside ProjectInstaller.cs screen.
  • Right click on serviceInstaller1 from ProjectInstaller.cs and make sure the Service Name property is set to MyFirstWindowsService and Start Type property is set to Automatic.

Select process PropertyServiceInstaller Property

  • Right click on serviceProcessInstaller1 from ProjectInstaller.cs and Set the Account property to LocalSystem. This will cause the service to be installed and to run on a local service account.


  • Build the project

Steps to install

  • Open the Developer Visual Command Prompt in Administrator mode
  • installutil.exe C:\MyNewService\bin\MyNewService.exe
  • Open the Control panel and check the ‘MyNewService’ is started else start service.

Date Formatting


Numeric Formating using String.Format

This String.Format method accepts a format string followed by one to many variables that are to be formatted.


Managing Data Persistent With ASP.NET


Persistence Method Number of Users Expire Time Data Size
Application All users Until the next application restart Any Size
Cookie One user As short as desired, or for months or even years if the user doesn’t delete their cookies Small data
Form Post One user For the next request (can be reused across many requests) It store virtually any amount of data
QueryString One user or one group of users For the next request (can be reused across many requests) Simple Data
Session One user As long as the user is active, plus a timeout period (typically 20 minutes) Can store any amount of data.
Cache All users or a subset of users As long or as short as needed Any type and size of data
Context One user This request only Can hold large objects, but typically does not since it is often used for every request
ViewState One user One Web form Reasonable amount of data
Config file All users Until the configuration file is updated Can hold a lot of data; usually organized as many small strings or XML structures

For More 

How to Call Java Script Funcation in C Sharp

RegisterStartupScript("buttonClick", "<scripttype=\"text/javascript\">
alert('Javascript works from codebehind');


"Call my function", "MyFunction()", true);

ClientScript.RegisterStartupScript(typeof(Page), "SymbolError", 
"<script type='text/javascript'>alert('Error !!!');</script>");


Onpage load event

Button1.Attributes.Add(“onclick”, “return yourJavaScriptFunction();”);

Enable Paging in GridView


If you are binding the data in page load You no need to assign the datasource again else you should assign the datasource here


Click here for more Gridview Paging

Difference between RowCreated, RowDataBound ,RowCommand

RowCreated event fires when the html-controls for a row are created. For example the controls you specify in templates, such as a text box or a dropdownlist. Data binding has not yet taken place.

RowDataBound event fires when the data row is bound to the data of the GridView. Can be used to do data manipulation based on the custom condition, or do logical function based on the data for each GridViewRow or to alter the UI of each row based on the  condition.

RowCommand event fires when a button is clicked from inside the GridView Row. Can be used to Select,Insert,Update, Delete, or to do custom operation inside the GridViewRow





%d bloggers like this: