Tuesday, March 8, 2011

Could not open key: Software\Microsoft\ASP.NET\x.x.x.x : ASP.NET Application Installation issue

The problem is related to ASP.NET application installation. I had an ASP.NET 4.0 application. I created a setup for that application. I tried to install it in a machine where .Net framework 4.0 was installed. I got the following error:

clip_image002

I was unable to infer the problem from the above error. After googling a bit, I figured the problem out. My machine had .net framework 4.0 Beta installed and my web application was developed in released version of .Net framework 4.0. The version of the web application and .net runtime of deployed machine didn’t match. In order to install asp.net application, the ASP.NET version of install application and .net runtime version of the web server’s machine has to be same. You can check the version from the property of Setup project.

clip_image004

This requirement is true for any version of ASP.NET application to be installed.

Monday, March 7, 2011

Never define blank div as <div id=”dv”/>

Today, I came across an interesting behavior of div. It may be well-known to everyone but new to me. I needed a blank div. I wanted to set the InnerHTML of the div from javascript. Instead of end tag, I declared the div as <div id=”dv”/>

No wonder, I can access the div from Javascript. When I assign anything (say “Test”) into innerHtml of the div, the html below the div is replaced by the “Test”. However, if you declare the div as <div id=”dv”></div>, it will work as expected. 

Therefore, we should always use end tag of div.

Hope this helps to someone!

Sunday, March 6, 2011

The page cannot be found: Install .Net framework 4.0 and deploy ASP.Net 4.0 application in IIS 6.0

You have an IIS 6.0 web server with .net framework 2.0 installed. You want to deploy ASP.Net 4.0 application in this web server. In order to this, you will install .net framework 4.0 and deploy your asp.net 4.0 application. When you access your newly deployed application, you will get “The page cannot be found”.

A lot of headless running around ensued after this, trying to figure out why 404 is occurring. It is difficult to trace this error. Since the web application is deployed accurately, there is no reason of this error.

The problem is in the following image:

clip_image002

When you install the .net framework 4.0, by default it’s not allowed. You have to allow it to run the asp.net 4.0 application.

Hope this helps!

Tuesday, March 1, 2011

Use jsTree with WCF Restful Service

If you try to use jsTree with WCF Restful service, you will find some difficulty. It is not as simple as drag a control from toolbox, drop it onto aspx page and bind the data source. You need to tweak the jsTree source code to use it with WCF Restful Service. Below is the snapshot of Demo application.

The demo application uses WCF restful service. The service contains a GetTreeData operation which returns a list of TreeData as a JSON. The property name of the TreeData class has to be same as declared in the demo application. jsTree uses these property name to construct the tree. I have added an additional property for custom css which is not available in the default jsTree source code. You can add a custom css for each node now.

When WCF restful service returns JSON, it adds a root property under which the actual returned object remains. So you have to tell the jsTree to use the actual JSON object. To make this happen, I have added a “dataPath” property to jsTree and changed the source a bit.

If you click on the node, it will not be expanded by default. The jsTree doesn’t support this. I have changed jsTree source to make this happen. Therefore, use the source code of JsTree included in the demo application rather than original jsTree source code.

You can download the demo application from here.

Hope this helps!

Friday, January 7, 2011

A step by step guide to use Asp.Net 4.0 Chart control with IIS 7

Asp.Net 4.0 has built-in chart control. You can drag it from the ToolBox and drop it onto any markup page. It is supposed to work if you bind data with the chart control’s data source. However, it doesn’t work. Let’s see the steps which are required to use chart control properly.

1> You will find the Chart control inside the Data section of the toolbox. Do drag and drop the control in any markup page.

2> Set the XValueMember and YValueMembers property of “Series” element with the properties of your object you want to bind with the chart control.

3> Set the Datasource of the chart control.

4> Open the web.config file.

5> Add the following element inside the handlers section.

<add name="ChartImageHandler" preCondition="integratedMode" path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

6> Add the following element inside the appSettings section:

<add key="ChartImageHandler" value="storage=file;timeout=20;Url=~/tempImages/;" />

Point out the Url of the value. Instead of Url, you can set absolute path.

7> Create a folder named “tempImages” in your root virtual folder.

8> Let the write privilege of “tempImages” folder to the IIS user (IIS_IUSRS).

Now, if you run you web application, you will see the chart control. You can download a small demo  from here.

Hope this help!

Thursday, October 28, 2010

A cool tool to rename your .Net project

Have you ever tried to rename your solution, project, namespace? If you have ten projects in your solution and you try to rename all projects and solutions, it will be nightmare for you. It’s a totally boring work. Once, I faced this scenario. My solution had 21 projects. Instead of renaming the projects , namespaces manually, I built a tool which did the work for me.

clip_image002

This tool is easy to use. Say, you have projects named; MyProject.DAL, MyProject.Buiness, MyProject.Common, MyProject.Model etc. You want to rename MyProject into HelloProject. In order to do this, write “HelloProject” onto new solution Name text box, “MyProject” onto Old Solution Name text box and locate the folder of your project.

Consider the following things while using this tool:

  • · Keep a backup project.
  • · You might get an error if you don’t have permission to rename each file and folder. In spite of getting error, you can open the rename solution. The tool should change the necessary files and folders.
  • · After opening the solution, clean it and rebuild it.

You can download the source code from here.

Happy Programming!

Wednesday, October 6, 2010

A generic Data Access Layer using ADO.Net entity framework for N-Tier application

If you work with ADO.Net, you know object context is mainly responsible to track changes of entity in an object graph. So, ADO.Net entity framework works fine as long as your entity is connected to object context. However, in N-Tier application/web application, the entity has to be disconnected from object context. In order to work with disconnected entity, entity framework doesn’t have any straight forward way before the entity framework team introduces self-tracking entities.

I have created a simple data access layer for disconnected entities using the self-tracking feature of Entity framework. The following code snippet shows generic implementation for CRUD operations:

public class MallInteractiveMapEntities2 : MallInteractiveMapEntities
    {
        private static Dictionary<string, Action<MallInteractiveMapEntities2, object>>
            _AddToMethodCache =
                new Dictionary<string, Action<MallInteractiveMapEntities2, object>>();

        public MallInteractiveMapEntities2():base()
        {
            if(_AddToMethodCache.Count == 0)
            {
                lock(_AddToMethodCache)
                {
                    _AddToMethodCache.Add(typeof (Mall).Name,
                                          (context, entity) => context.Malls.ApplyChanges(entity as Mall));
                    _AddToMethodCache.Add(typeof(Floor).Name,
                                          (context, entity) => context.Floors.ApplyChanges(entity as Floor));
                }
            }
        }

        public void AddTo<TEntity>(TEntity entity)
        {
            _AddToMethodCache[typeof(TEntity).Name](this, entity);
        }

        public bool ApplyChangesToEntity<TEntity>(TEntity entity) where TEntity : IObjectWithChangeTracker
        {
            try
            {
                AddTo<TEntity>(entity);
                SaveChanges();
            }
            catch
            {
                return false;
            }
            return true;
        }

public bool ApplyChangesToEntityList<TEntity>(IEnumerable<TEntity> entities) where TEntity : IObjectWithChangeTracker
        {
            try
            {
                entities.AsParallel().ForAll(AddTo);
                SaveChanges();
            }
            catch
            {
                return false;
            }
            return true;
        }
      
    }

MallInteractiveMapEntities class is generated from Entity framework designer for a specific database. MallInteractiveMapEntities2 class which is inherited from MallInteractiveMapEntities has some methods to perform the CRUD operations.

To illustrate the use of the above class, consider we have two entities named “Map” and “Floor”. “Floor” is the child of “Map” and the relation is one to many. In order to create(C) Map and Floor, use the following pseudo code.

  Mall mall = new Mall();
  mall.Name = "American Shopping Mall";

            Floor floor1 = new Floor();
            floor1.Name = "Ground Floor";
            mall.Floors.Add(floor1);

            Floor floor2 = new Floor();
            floor2.Name = "First Floor";
            mall.Floors.Add(floor2);


            using (MallInteractiveMapEntities2 context = new MallInteractiveMapEntities2())
            {
                context.ApplyChangesToEntity<Mall>(mall);
            }

When you instantiate an entity, it will be marked as an added state.

To update an entity with child entities, use the following pseudo code:

      Mall mall = new Mall();
      mall.Id = 1;
      mall.Name = "American Shopping Mall";
      mall.MarkAsModified();
 
      Floor floor1 = new Floor();
      floor1.Name = "Ground Floor";
      floor1.MallId = mall.Id;
      floor1.MarkAsModified();
      mall.Floors.Add(floor1);

      Floor floor2 = new Floor();
      Floor2.MallId = mall.Id;
      Floor2.MarkAsModified();
      floor2.Name = "First Floor";
      mall.Floors.Add(floor2);


      using (MallInteractiveMapEntities2 context = new MallInteractiveMapEntities2())
            {
                context.ApplyChangesToEntity<Mall>(mall);
            }


Here, you have to mark the entity as a modified state. You also have to set the parent id to a child entity.

To delete (D) an entity, simply mark each entity as a deleted by using the “MarkAsDeleted()” method.

Self tracking is a promising feature of Entity framework. By using this feature, you can create a data access layer for disconnected entity of ADO.Net entity framework.

Hope this help!