Tuesday, November 1, 2011


In the previous post, I mentioned how to convert DATETIME to TIMESPAN.

Now if the requirement is to convert DATETIME TO TIMESPAN, we can do that in one line:

DateTime dt="XXX";
TimeSpan TS = new TimeSpan(dt.Now.Ticks);

This will convert the DateTime to TimeSPan.


Convert TimeSpan To DateTime in c# + One line Code

We usually fall into the situation where conversion of Timespan to DateTime is required.

Here is how it can be done easily just by casting:

Suppose we have two variables DateTime and Timespan and Timespan one with some value
DateTime dt;
TimeSpan ts="XXX";

We can covnert 'ts' to 'dt' like this:

dt= Convert.ToDateTime(ts.ToString());

Thats it!!!

Hope you have liked the solution.


Tuesday, February 2, 2010

Microsoft Access was unable to create the .accde, .mde or .ade file


Many times we have faced issue that after creating the MDB file, when we try to compile the database to MDE file ,Access gives the following error :

Microsoft Access was unable to create the .accde, .mde or .ade file

On clicking the button 'Show Help', it shows following error message:

This error is usually associated with compiling a large database into an MDE file.  Because of the method used to compile the database, a considerable number of TableID references are created for each table.  The Access database engine can only create a maximum of 2048 open TableIDs at one time.  Exporting a database as an MDE potentially can exceed this limit if the database has a large number of objects (table, macro, form, report, etc).
There is no accurate method to estimate the number of TableIDs the Access database engine uses during the process of compiling a database as an MDE.  However, each VBA module and each form uses one TableID, as a result, if the database has 500 forms, and each form's HasModule property is set to Yes, as many as 1,000 TableIDs are used.


The main reason of database not getting compiled to mde is that it failed during compilation. That is , yiu have some code tha failed to compile.


1. Press Alt + F11 to display VBA editor.
2. In the menu 'Debug' , you will find 1st menu item as 'Compile AccessDb'.
3. Click that option.
4. If there is no error, the database will get compiled.
5. If during compilation, any error is encountered, the VBA editor will display that error and you can rectify that.
6. Once all errors get rectified, you will be able to compile the database.

Once successfully compiled, try making the mde again ans it will definitely make the mde file.

Wednesday, December 23, 2009

How to reset the autoincrement field in MS Access Table

How to reset the autoincrement field in MS Access Table

Many times we use autoicrement field in Ms Access table and we have noticed that the value of autoincrement field keeps on increasing ever if we delete all the rows of the table and add new row.
In order to reset the autoincrement field value back to 1, do the following:

1. Delete all the rows from the table.
2. Compact the whole database .

This will automatically reset the autoincrement value back to 1.

Tuesday, December 22, 2009

Introducing WCF 3.5

This post is dedicated to WCF and its basics. We will go through various conceptual points of WCF and learn step by step implementation of WCF.
This post will cover few fundamental topics like:
• What is Windows Communication Foundation?
• Advantage of WCF
• What is an endpoint?
• Structure of endpoint or ABC of an end point.
• Explaining Address
• Explaining Binding and type of binding
• How to select proper binding
• Contracts and type of contracts
• Discussing Service Contract in detail।

What is WCF?
WCF stands for Windows Communication Foundation and comes along with Dot net framework 3.0 onwards and as a part of operating system from Vista onwards.
Business process in today’s market depends on various systems based on systems that runs on different platform within or outside an organization. There must be a way , a solution to have a communication between these various systems.
Why to use WCF?
WCF basically is a Microsoft’s solution to develop communication between distributed application। One may think why to use WCF when we already have lot of options to develop distributed application in past. The one line answer is that WCF acts as an unified technology that encapsulates all existing technologies to develop distributed application such as .NET Remoting, ASMX, MSMQ etc.

Apart from that we get lot of option on how to host the WCF service like IIS, or Widows Activated Service (WAS) etc. That we will discuss in later post.
I am eliminating discussion on history of distributed application since distributed application development is such a huge topic that it will divert our objective to learn WCF basics , However, throughout the document, I will map the similarity between existing distributed application solution to WCF।

Endpoint is the part that a service exposes to the outer system. It defines various details of a service hosted. The consumer reads the endpoint to judge the nature, type of the hosted service.
The ABC of WCF Endpoint:

Three basic component of any WCF service is :
“A” is for Address: Where to find the service
“B” is for Binding: How to communicate with the service
“C” is for Contract: What to send to the service or what to expect from the service.


It defines where on the network messages should be sent so that the endpoint receives them. This is the location to which messages must be sent by the client.

The address for an endpoint is a unique Uniform Resource Locator (URL) that identifies the location of the service. The address should follow the Web Service Addressing (WS-Addressing) standard, which means it might contain the following four parts:

• Scheme: The top-level portion of the address, this is typically “http” followed by a colon.
This is not the same thing as a protocol, even though it commonly uses the same letters as the protocol.
• Machine: Identifies the machine name, which can be a public URL or a local identifier.
• Port: The optional port number, preceded by a colon.
• Path: The path used to locate the service files. Typically, this is just the service name, but the path

Syntax of an address will be: Scheme://Machine[:Port]/Path1/Path2



WCF supports several protocols, and each has its own particular addressing format.
1. HTTP protocol:
HTTP services can be either self-hosted or hosted on Internet Information Services (IIS). When addressing an HTTP service in a self-hosted scenario, you use the following format:
2. TCP Protocol:
The TCP transport uses the net.tcp: scheme but otherwise follows the same rules as described
for HTTP addresses. Here is an example:
3. MSMQ:
You can use the Microsoft Message Queue (MSMQ) transport in an asynchronous one-way
(fire-and-forget) or duplex type of messaging pattern and use the MSMQ features of Windows. net.msmq://localhost/private$/QuickReturnSettleTrade
4. Named Pipes:
It is a common way to provide a means to implement inter- or in-process communication.
The Named Pipes transport in WCF supports only local communication and uses the
net.pipes scheme. Port numbers don’t have any meaning with the Named Pipes transport.
This results in the following address format:


A binding defines how you can communicate with the service. It is the primary extension point of the ABCs of WCF. The binding controls the following:

• The transport (HTTP, MSMQ, Named Pipes, TCP)
• The channels (one-way, duplex, request-reply)
• The encoding (XML, binary, MTOM…)
• The supported WS-* protocols (WS-Security, WS-Federation, WS-Reliability, WS-Transactions)

WCF provides a default set of bindings that should cover most of your requirements. If the default bindings don’t cover your requirements, you can build your own binding by extending from CustomBinding.

There are nine preconfigured bindings in WCF. Each of these provides the means for a particular distributed computing need. There are several factors that determine which binding to choose for a specific application, including security, interoperability, reliability, performance, and transaction requirements.

Table below compares the nine preconfigured bindings by showing the common features they support. This table can be used to select a best binding for a particular need.

Below is the process that will help you to select the binding you required for your application:


A contract is a description of the messages that are passed to and from service endpoints.

There are three types of contracts in WCF:

• Service contracts : Service contracts describe the functional operations implemented by the service. A service contract maps the class methods of a .NET type to WSDL services, port types, and operations. Operation contracts within service contracts describe the service operations, which are the methods that implement functions of the service.

• Data contracts: Data contracts describe data structures that are used by the service to communicate with clients. A data contract maps CLR types to XML Schema Definitions (XSD) and defines how they are serialized and deserialized. Data contracts describe all the data that is sent to or from service operations.

• Message contracts: Message contracts map CLR types to SOAP messages and describe the format of the SOAP messages and affect the WSDL and XSD definitions of those messages. Message contracts provide precise control over the SOAP headers and bodies.

We will cover each type of binding and contract as we moves ahead with the implantation of WCF service. All theory will really make all of confuse so we need a bit of coding also..

Explaining Service Contract:

NOTE: before going into code, I would like to mention that all class, attributes specified to WCF comes from System.ServiceModel namespace. So import this namespace before going for coding.
A service contract is a collective mechanism by which a service capabilities and requirements are specified for its consumers.
Few points regarding Service Contract:
• A service contract can be applied to a normal dot net class or an interface.
• Service contract attribute is not inherited. That means if you are inheriting a class that has service contract attribute to it, you need to define the attribute to the derived class also. It will not get inherited automatically.
• Best practice is to use service contract attribute to interface.
• Always use namespace and Name parameter when defining a service contract attribute.

Sample Code:

An interface defined as a service contract without any parameter.
public interface ITaskManagerService
{ …. }
An interface defined as a service contract without any parameter.
public interface ITaskManagerService
{ …। }

Explaining Operation Conntract:

The OperationContractAttribute, also defined in the System.ServiceModel namespace, can be applied only to methods. It is used to declare the method as belonging to a Service contract.

public interface ITaskManagerService
[OperationContract(IsOneWay = true,name=”Operation1”)]
void ProcessInsertMessage(Message message);
So the above piece of code displays a interface defined as a service contract and its method as an operation contract।

In next article, I will discuss Message Exchange Pattern and two other contracts that is Data Contract and Message Contract.

Wednesday, November 18, 2009

All about Global.asax

This file is used by the application to hold application-level events, objects, and variables — all of which are accessible application-wide. Active Server Pages developers had something similar with the Global.asa file.

Your ASP.NET applications can have only a single Global.asax file. This file supports a number of items.

When it is created, you are given the following template:

<%@ Application Language="VB" %>
<script runat="server">

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application startup
End Sub

Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application shutdown
End Sub

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when an unhandled error occurs
End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a new session is started
End Sub

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a session ends.
' Note: The Session_End event is raised only when the sessionstate mode
' is set to InProc in the Web.config file. If session mode is
' set to StateServer
' or SQLServer, the event is not raised.
End Sub


Just as you can work with page-level events in your .aspx pages, you can work with overall application
events from the Global.asax file.

In addition to the events listed in this code example, the following list
details some of the events you can structure inside this file:

- Application_Start: Called when the application receives its very first request. It is an ideal spot in your application to assign any application-level variables or state that must be maintained across all users.

- Session_Start: Similar to the Application_Start event except that this event is fired when an individual user accesses the application for the first time. For instance, the Application_Start event fires once when the first request comes in, which gets the application going, but the Session_Start is invoked for each end user who requests something from the application for the first time.

- Application_BeginRequest: Although it not listed in the preceding template provided by Visual Studio 2008, the Application_BeginRequest event is triggered before each and every request that comes its way. This means that when a request comes into the server, before this request is processed, the Application_BeginRequest is triggered and dealt with before any processing of the request occurs.

- Application_AuthenticateRequest: Triggered for each request and enables you to set up custom authentications for a request.

- Application_Error: Triggered when an error is thrown anywhere in the application by any user of the application. This is an ideal spot to provide application-wide error handling or an event recording the errors to the server’s event logs.

- Session_End: When running in InProc mode, this event is triggered when an end user leaves the application.

- Application_End: Triggered when the application comes to an end. This is an event that most ASP.NET developers won’t use that often because ASP.NET does such a good job of closing and cleaning up any objects that are left around.

In addition to the global application events that the Global.asax file provides access to, you can also use directives in this file as you can with other ASP.NET pages. The Global.asax file allows for the following directives:

- @Application
- @Assembly
- @Import

These directives perform in the same way when they are used with other ASP.NET page types.

How to place two classes written in different languages in App_Code

What is App_Code ?

The \App_Code folder is meant to store your classes, .wsdl files, and typed datasets. Any of these items stored in this folder are then automatically available to all the pages within your solution.

Everything placed in the \App_Code folder is compiled into a single assembly. The class files placed within the \App_Code folder are not required to use a specific language. This means that even if all the pages of the solution are written in Visual Basic 2008, the class in the \App_Code folder of the solution can be built in C#.
Here is a small catch...

Because all the classes contained in this folder are built into a single assembly, you cannot have classes of different languages sitting in the root \App_Code folder, as in the following example:


Having two classes made up of different languages in the \App_Code folder causes an
error to be thrown. It is impossible for the assigned compiler to work with two different languages.

Therefore, in order to be able to work with multiple languages in your \App_Code folder, you must make some changes to the folder structure and to the web.config file.

The first step is to add two new subfolders to the \App_Code folder
— a \VB folder and a \CS folder.
This gives you the following folder structure:

This still will not correctly compile these class files into separate assemblies, at least not until you make some additions to the web.config file.

In the web.config file , change the <compilation>
node so that it is structured as following:

<add directoryName="VB"></add>
<add directoryName="CS"></add>

Now that this is in place in your web.config file, you can work with each of the classes in your ASP.NET pages. In addition, any C# class placed in the CS folder is now automatically compiled just like any of the classes placed in the VB folder.

NOTE: The name of sub directories are'VB' and 'CS' for this example. You can put any name you wish to.