Installing assembly in GAC in Windows Server 2008 : Resolved

I developed an event handler and need to deploy it on production server. My production server is built on Windows 2008 Enterprise Server 64 Bit and there was only one installation on server i.e. SharePoint Server 2010. Now I need to install my assembly in to GAC. I copied my .dll to production server. Then I opened c:\windows\assembly and drag drops my assembly in to GAC (c:\windows\assembly folder). But nothing happened and my assembly couldn’t install in GAC 😦

Drag and drop was not working. So second alternative was to use gautil.exe to install the assembly inside GAC but as it was production server so there wasn’t any installation of visual studio and hence there was no SDK installed therefore there wasn’t such exe like gacutil.exe. Again that was a problem. Now I worked on why drag and drop is not working. I searched here and there and follow some articles but nothing helped me. I disable UAC and restart server but it doesn’t work. I opened console and explorer using run as administrator but nothing helped me. Then I copied gacutil.exe file from my development machine to production server but I couldn’t run it (Though lastly this technique worked for me).So finally I resolved this issue and below are the directions for how to resolve this?

1. First check that if production server is of 32 bit or 64 bit?

2. If it is 64 bit then open folder “C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\x64

But if it is 32 bit then open folder “C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin

3. Copy 2 files i.e. gacutil.exe & gacutil.exe.config

4. Paste these files to any location of production server.

5. Your gacutil.exe is now ready to run and u can install assembly in to GAC using this small utility.

 Points to Remember

* When you drag and drop assembly in GAC it won’t give proper error messages (Like targeting framework error) if it has any so better to use gacutil to install assembly.

* gacutil.exe doesn’t run standalone rather it needs a configuration file to run i.e. gacutil.exe.config


Call Hierarchy : A handy feature in Visual Studio 2010

Suppose we have a solution that contains different dependent projects. We can face 2 most common situation here:

Situation 1: We have a method that we need to change by modifying the definition of that method or just we want to check that which other methods are calling this method.

Situation 2: We have a method that contains so many lines of codes and we need to check that inside this method which all other methods are calling as method is too large and we don’t want to go line by line for this.

Before VS 2010, for situation 1, we need to search the method name in whole solution then we can find the call of this solution in search window but the drawback of this approach is that if you have overloaded methods then in search result all methods will display and you need to take extra care for that.

Now in VS 2010, Microsoft provided a beautiful menu and context command to help these situations. Below are the steps to use these helpful commands:

  1. First you need to open your solution.
  2. Now open the file, where the method is located and needs to be checked.
  3. Go to View > Call Hierarchy
  4. Call Hierarchy window will open on bottom side, in this window choose My Solution from drop down. (You can also choose Current Project, Current Document depending upon your needs.)
  5. Now right click the method name in file opened and click on “View Call Hierarchy” from context menu.
  6. Your Call Hierarchy window will populate with details like Calls To “Method Name” and Calls From “Method Name” you can just expand these nodes and check all the incoming and outgoing call from this particular method.

Enjoy this new feature 🙂


Resolved: Could not load file or assembly ‘Microsoft.SharePoint.Search, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c’ or one of its dependencies…

I have a solution that contains SharePoint dll’s reference. Whenever I run the solution, start page get crash and show me this error.

“Could not load file or assembly ‘Microsoft.SharePoint.Search, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c’ or one of its dependencies. An attempt was made to load a program with an incorrect format.”

Well every time I run the project it creates Microsoft.SharePoint.Search.dll and Microsoft.SharePoint.Search.xml inside the bin folder and it creates the problem. Well there are lots many things have written on web regarding this problem. I follow all these things but could not get rid of this error. Finally I decided to fix this problem according to my way. So below are the steps to fix this problem permanently.

1.       Inside solution explorer (Visual Studio), select the project and right click on that.

2.       From context menu, click on properties.

3.       When properties page opens, Click on Build Events.

4.       Under Post – build event command line, copy and  paste below lines

cd $(TargetDir)

del Microsoft.SharePoint.Search.dll

del Microsoft.SharePoint.Search.xml

Now save the project, build the project and run. Now above mentioned error will never irritate you. 🙂

Common errors with SharePoint 2010 development using Visual Studio 2010 in x64 architecture: Resolved

“The type or namespace name ‘SharePoint’ does not exist in the namespace ‘Microsoft’ (are you missing an assembly reference?”

(Even you add Microsot.SharePoint.dll reference in your project and import the namespace in code but while building the solution you will get above mentioned annoying error.)


“FileNotFoundException was unhandled: The Web application at http://localhost/ could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.”

(When you initialize the SPSite class in your code the above mentioned error will thrown. Well some people suggest that in place of localhost you should use machine name or ip of the machine but trust me I used all tricks but even then I couldn’t get rid of above mentioned error)

The above two problems are most annoying when you develop an application for SharePoint 2010 using Visual Studio 2010. Well no problems below are the solution of above two problems. Before writing the code just check for two things that are by default set by Visual Studio 2010 and need to change.

  • Right Click on your project (Not your solution).
  • Click on Properties.
  • On project properties page, Click on Application tab.
  • Under Target Framework select .NET Framework 3.5 in drop down list.
  • Click on save button in toolbar.
  • Now click on Build tab.
  • In Platform Target, select x64 option.
  • Click on save and close the project properties page.

Build and run the application and it would work fine now 🙂

Resolved: COM DLL is not showing in COM Library after Registering on Windows Server 2008 x64

Well I had to implement a payment gateway at my server. My server was built on x64 architecture and had Windows Server 2008 installed. Well according to the document I had to put my COM dll inside c:\windows\system32 folder and then from Run command I had to register this dll like “regsvr32 c:\windows\system32\MyCOM.dll”.

Well I done above mentioned procedure successfully now I want to use this dll in my C# project so I create a project and go for add reference but even after successful registration my COM dll was not showing inside COM tab. I tried all possible way to add reference of this dll without any luck. I tried un-register, register as well as browsing this dll to add reference but nothing could help out me. So after doing some goggling and research I came to know that in x64 architecture there is a folder inside “c:\windows\” i.e. SysWOW64. This folder is specially designed for old 32 bit programs. Well if you are registering the old 32 bit COM dll or other 32 program then you should put the dll inside SysWOW64 folder and then register them from that location it will then use RegSvr32.exe of SysWOW64 folder for normal 64 bit dlls and other 64 bit programs u should put dlls inside System32 folder and run RegSvr32.exe from System32 folder.

Get HTML of a web page using C#.NET

Below i am using a function that takes a web page full url as string and return back HTML of that page as string. You can
use this returned HTML in your code as per your requirement.

Step 1:
First import 2 namespaces that are required by this function to work.

using System.IO;
using System.Net;

Step 2:
Copy & Paste this function in your code behind.

public string GetHTML(string strURL)
HttpWebRequest wbrq = (HttpWebRequest)WebRequest.Create(strURL);
wbrq.Method = “GET”;
HttpWebResponse wbrs = (HttpWebResponse)wbrq.GetResponse();
StreamReader sr = new StreamReader(wbrs.GetResponseStream());
strResult = sr.ReadToEnd();
return strResult;

Step 3:
Call this function as per your requirement.

string strHtml=this.GetHTML(“”);

Happy Programming 🙂

Nullable Types in Depth

This Article explains the Nullable Types in .NET 2.0

Nullable Types are instances of the System.Nullable struct introduced in .NET 2.0. Null means value undefined or value not known.

Reference types by design support storing null but values types have been designed to store only the range of values they are made for. They don’t have capacity to store null.

But many times there may be situation where we don’t know the value to store in value type e.g. in case of database fields where some fields may be empty of null.

To support such requirements Nullable types have been introduced.

Nullable types are supported using a Nullable Generic Structure in System Namespace.

public struct Nullable<T> where T : struct

The following struct can be used in following two ways :

Nullable<int> x = new Nullable<int> (100);


int? x = 100;

The two main properties of this struct are HasValue and Value.

HasValue indicates whether the current instance has a value. True if the instance has a value & false if has no value. The value in the object can be accessed by property Value.

The operator ?? can be used to assign a default value that will be applied when a nullable type whose current value is null is assigned to a non-nullable type.

e.g. int? x= null; int y = x ?? 1; y will be assigned a value of 1.

Now how can we declare a nullable value type?
Simply like this:


DataTime? dtmVarName = null;

int? intVarName = null;

bool? bVarName = null;


Dim dtmVarName As Nullable(Of DateTime)

Dim intVarName As Nullable(Of Integer)

Dim bVarName As Nullable(Of Boolean)

The following is written in MSDN, but I found it very useful to be used to close this article. Nullable types have the following characteristics:

Nullable types represent value-type variables that can be assigned the value of null. You cannot create a nullable type based on a reference type. (Reference types already support the null value.)
The syntax T? (in C#) is shorthand for System.Nullable<T>, where T is a value type. The two forms are interchangeable.

Assign a value to a nullable type in the same way as for an ordinary value type, for example:

C#: int? x = 10; or double? d = 4.108;

VB.NET: Dim x As Nullable(Of Integer) = 10 or Dim d As Nullable(Of Double) = 4.108
Use the System.Nullable.GetValueOrDefault method to return either the assigned value, or the default value for the underlying type if the value is null, for example

C#: int j = x.GetValueOrDefault();

VB.NET: Dim j as Integer = x.GetValueOrDefault()
Use the HasValue and Value read-only properties to test for null and retrieve the value.
The HasValue property returns true if the variable contains a value, or false if it is null.
The Value property returns a value if one is assigned, otherwise a System.InvalidOperationException is thrown.
The default value for a nullable type variable sets HasValue to false. The Value is undefined.
Use the ?? (C#) operator to assign a default value that will be applied when a nullable type whose current value is null is assigned to a non-nullable type, for example int? x = null; int y = x ?? -1;
Nested nullable types are not allowed. The following line will not compile:

C#: Nullable<Nullable<int>> n;

VB.NET: Dim n as Nullable(Of Nullable(Of int))

Object cannot be cast from DBNull to other types

Sometimes you need to code around dates in your program. Today i will discuss about the common error we get while displaying a date from back end to front end. What if you have blank/null value inserted in your back end for date and when you get this date to front end and try to bind this in a text box control then you will get a common error …

“Object cannot be cast from DBNull to other types”

No problem you have to put a check before binding the date in text box like below:

if (drPublication[strReleaseDate] != System.DBNull.Value )
this.dtTest.SelectedDate = Convert.ToDateTime(drPublication[strReleaseDate]);

Convert date from dd/MM/yyyy to MM/dd/yyyy for storing in SharePoint list or Sql Server Database

I was working on a custom web part that takes the date in Indian format e.g. dd/MM/yyyy while inserting the record in SharePoint custom list. But as we know that SharePoint custom list insert the date value as MM/dd/yyyy format. So it was throwing me the error for invalid string format so below is the code snippet that how i resolved this issue:

string sdate = “15/04/2010”;
DateTime dt = new DateTime();
dt = DateTime.ParseExact(sdate, “dd/MM/yyyy”, new System.Globalization.CultureInfo(“en-US”));
string dateToStore = dt.ToShortDateString();

In this way you are capturing date in dd/MM/yyyy format and storing this date in MM/dd/yyyy format in SharePoint custom list.

Happy Coding 🙂

Resolved: Collection was modified; enumeration operation may not execute

I was playing with a data table object, my requirement was to merge all duplicate rows to one and remove the duplicates.
Book     Price Quantity
Title 1 210   4
Title 1 210   6
Title 1 210   8

These were the rows inside my data table object. I want a consolidated row in place of these three duplicate rows.

Book     Price Quantity
Title 1 210   18

I used for each loop to add the quantity and then remove the rows but after executing this i got “Collection was modified; enumeration operation may not execute” error so i came to know that foreach statement uses an Enumerator, which is a little object optimized to move through a collection which doesn’t change.
So if we remove the items from the Enumerator, You are changing the contents of the list during enumeration which is the no-no being reported. The better way to handle would be to avoid changing such collections. So below is the code i used to resolve this:

for (int i = dtCartTable.Rows.Count – 1; i >= 0; i–)
DataRow dr = dtCartTable.Rows[i];

string dtitem = dr[“Book”].ToString();

if (dtitem == lblTitle.Text)
totalQuantity += Convert.ToInt32(dr[“Quantity”]);


Voila its working now. Happy programming 🙂