Categories
.NET C#.NET Programming

Extensions Method in .NET

Recently I use extension method in C# project. I knew those for years, but really never get a chance to use them where they are most effect. I have couple of extension method in my project, such as ToInteger(), for a string to convert a String to Integer, which I used more frequently in my project. But that doesn’t seems a good use of technology to me.

But I now found a perfect use for extension method. i.e. I was upgrade my code from old ASP.NET project to MVC and for this I am making a class library of all objects. Now, this scenario need me to make a conversion from old enum to new enum  which are now part of namespace of my class library, rather than sitting around in App_Code folder. Here I make a extension method that convert a OLD enum to new name space base. This ease lot of my works.

Method goes like:


public static NewType GetSaleType(this OldType s)
{
	NewType r = NewType.Regular;
	switch (s)
	{
		case OldType.BK: r = NewType.BK; break;
		case OldType.IL: r = NewType.IL; break;
		case OldType.MM: r = NewType.MM; break;
		case OldType.PC: r = NewType.PC;break;
		case OldType.Regular: r = NewType.Regular; break;
		case OldType.Trigger:r = NewType.Trigger; break;
	}
	return r;
}
Categories
.NET ASP.NET C#.NET VB.NET/VB WPF

GDI+ Generic Error

I see lot of people asking about GDI+ Generic Error. Since this is “generic” error nobody can tell what is wrong with code. So did a work around and get it working. I had this error quite a time in early days of my programming and just found same error in one of junior’s code. Well from my experience I can say one thing. GDI+ Generic error 99.9% means “developer miss a detail or overlooked something”. Most common reason of error are negligence of

1. Path should be physical path

2. All folder should present before you create file.

3. Previous files should be in write mode if case of overriding files.

4. Make sure permission are for writing to User of process.

5. Do not include any special character which windows do not permit in folder name or file name.

6. FILE FORMAT has nothing to do with this error most often.

7. Recheck permission if you try all above.

8. Recheck Path again :).

Point #7 and #8 are most important solution and always works for me.

Categories
.NET ASP.NET C#.NET JQuery JSON VB.NET/VB

ASP.NET JavascriptSerializer for Array

I need to throw some database entry which are as simple as TIME, for this I got a string array in my ASP.NET code behind. I was throwing this String Array in JSON format after using JavascriptSerialize.Serialize method and it is making not so perfect Json script. I said it is not making perfect json because the expect result for me was something like this:

[“10:20″,”10:30″,”10:40”]

but it was returing something like

{“d”:”[\”10:20\”,\”10:30\”,\”10:40\”]”}

the difference is that it is converting the array in to string, however the ideal result can be

{“d”:[“10:20″,”10:30″,”10:40”]}

But we cannot change that functionality now, so what I realise we should use the eval function of javascript to make our Jquery or whatever JSON parse you are using. So my Jquery code looks like

….

success: function(data) {
var items = [];
$.each(data, function(ind, val) {
var myobject = eval(val);
$.each(myobject, function(i, v) {                   
items.push(‘<option value=”‘ + v + ‘”>’ + v+ ‘</option>’);
});
});
}

this convert the String to Javascript object using Eval function. Easy enough !!!

Categories
.NET Server Configuration

.NET 4.0 On IIS 6

With my dedicated Windows 2003/IIS 6.0 server I want to run latest version of .NET framework on it. Hence I use my Web Platform Installer to install .NET 4.0. Everything goes smooth. It was piece of cake to install it.

Now the time came to run my first .NET 4.0 Framework application, I configure a New website in IIS. Select .NET framework 4.0 from ASP.NET tab in Website property. But what I found is I am getting 404 error. Strangely enough that Default.aspx page does exists and with .NET 2.0 it works just fine. So what goes wrong ? I search and experiment with web.config, but to no avail.

Then I found this article http://www.denalimultimedia.com/info/Articles.aspx?cid=20&topic=IIS-6-ASP-NET-4-0-page-not-found-404, which clearly mention that

  • Install framework asp.net 4.0 and restart [Web Platform Installer does it just fine]
  • IIS 6.0 console should now display ASP.NET 4.0 under tab “ASP.NET” [It was again done if #1 above is right]
  • Under Web Services Extensions (in IIS console) .NET 4.0 framework might be set as: prohibited. This must be ALLOWED otherwise a browser might just return the error 404 page not found which is not very descriptive
  • ASP.NET 4.0 applications must be run in a separate process (Application Pool) otherwise you’ll get the error ‘Server Application Unavailable’.

It is anyways always good to run different programming languages in different pool. For Example, if you want to run PHP on same server I use different pool for them. Covering those 4 steps I got my server working with .NET 4.0. Wondering why Web Platform Team forgot to complete 2 step in their installer.