Tuesday, 9 September 2008

Sorting a Generic List in C#

Using Sort algorithm was pretty tedious for asp.net versions prior to 2.0, and also in ASP3.0. But ASP.Net 2.0 onwards we have a very innovative feature of using delegates and sorting out the records in a generic list based on any property of the object in that list.

The following is the code sample for the same.

List SectionsList = SectionsListDataSourceObject;


delegate(dataType dt1, dataType dt2)


return dt1.SortField.CompareTo(dt2.SortField);


You can use all types of fields to sort ranging from string, integer to dates, and you dont have to override any functionality. If you want to sort the records in descending order just reverse the pattern in the return statement. So that would be

return dt2.SortField.CompareTo(dt1.SortField);

Hope this saves someone quite a lot of frustration!

Thursday, 3 April 2008

It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level.

One hell of an error I got while working with membership provider in my project. The error said something like "It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS."
Now this doesn't really help as an error message, but a quick googling got me some surprising scenarios where you can have this error. Surprising because, there were only two ways it can surface!

  1. When you create a new application and the IIS fails to configure, the virtual folder as an application. Now I seriously don't believe there are many geeky nerds, who actually create an application virtual folder manually (I don't want to be rude, but have'nt come across any!). Also no one creates even a folder into the application manually. Atleast I do it via Visual Studio. It is quick, efficient and prevents me to configure it properly in the IIS!
  2. The second scenario, is the one that is quite possible most of the time. When you have sub-directories in your application, you can have web.config for all the sub-directory nodes. You can configure, almost everything for that folder using that web.config, and it will override the settings, in the parent web.config. But saying that I must admit, there are certain properties which cannot be set in the web.config of the sub-directory.
Let me explain the second in detail here!

You cannot set the Authentication, Session State and a couple of other properties for the sub folder in the web.config at that levet. It must inherit those properties from the parent file in principle. This after careful thinking was pretty common sense to me. You cannot have an application that accepts Form authentication at the application level, and suddenly execute windows authentication in a sub folder. You can have it vice-versa though, but I think just as a precaution, and not making it scary and confusing for the developer, MS has configured the parser accordingly.

The good news is, that this is true only for Authentication, and not Authorization. You can configure authorization at folder level, because, you need to! For all those who are having this error, I have enclosed following solution to the problem.

You should have the authentication at the Application root level web.config under the <configuration>
<location path="ClientArea">
<allow roles="Clients"/>
<deny users="*"/>

However, if you wish to have a web.config at the sub-directory level and protect the sub-directory, you can only specify the Authorization mode as follows:

<deny users="*"/>

Friday, 25 January 2008

Visual studio 2008 professional edition

Hello all,
Today i have received a 90-day evaluation pack of visual
studio 2008 Professional edition. I have been reading a lot about the
said edition, and the latest and some cool features, it has to offer. I
think 90 days is too little a time for a professional like me, who
hardly has some time after coming from a long day at work, to try out
new things. But guess what, i have decided that i will spend atleast an
hour on the new version to make sure, i utilize the complete 90 days to
the maximum.
Will keep you all posted of anything cool, or may be any glitches from the new software.
So keep checking back, and till then chao!

Powered by ScribeFire.

Sunday, 20 January 2008

Mix n match C# and VB codes in a single project

Got this wonderful stuff from http://www.aspnetlibrary.com

Whilst it isn't ideal to both mix C# and VB.NET languages in the same ASP.NET project, it is possible. The code still has to be separated and it can't be mixed at page level but you can create a class in each language and then use and reference them both from any page.

To do this, you'll need to follow a few simple steps.

Step 1

Create an App_Code folder if it doesn't already exist. Then, inside this folder create two other folders to hold each language type. For example,


Step 2

Edit your web.config file and make sure the following section is added:

<add directoryName="vbcode"/>
<add directoryName="cscode"/>

<add directoryName="vbcode"/>
<add directoryName="cscode"/>

Add each class to the relevant folder and you're done! You can now reference any of the classes in the standard manner and ASP.NET will do all of the compilation automatically for you.

Wednesday, 16 January 2008

Distributed Transactions in MS SQL server 2005

I was developing a .net 2.0 web application with SQL 2000, and used TransactionScope. The machine had the access to database on server through LAN. Everything was working fine, and we had no problem. Then came the upgrade for the SQL server. We decide to roll out 2005 edition of MS SQL and this is where the problems started.
SQL 2005 does not by default allow distributed transactions, so if you are using nested transactions, which could be TransactionScope as well, it will bug up. After a bit of digging up, I found out following steps to get around this bug! So here they are.

First verify the "Distribute Transaction Coordinator" Service is running on both database server computer and client computers

1. Go to "Administrative Tools &gt; Services"
2. Turn on the "Distribute Transaction Coordinator" Service if it is not running

If it is running and client application is not on the same computer as the database server, on the computer running database server

1. Go to "Administrative Tools &gt; Component Services"
2. On the left navigation tree, go to "Component Services &gt; Computers &gt; My Computer" (you may need to double click and wait as some nodes need time to expand)
3. Right click on "My Computer", select "Properties"
4. Select "MSDTC" tab
5. Click "Security Configuration"
6. Make sure you check "Network DTC Access", "Allow Remote Client", "Allow Inbound/Outbound", "Enable TIP" (Some option may not be necessary, have a try to get your configuration)
7. The service will restart

On your client computer use the same above procedure to open the "Security Configuration" setting, make sure you check "Network DTC Access", "Allow Inbound/Outbound" option, restart service and computer if necessary.

On you SQL server service manager, click "Service" dropdown, select "Distribute Transaction Coordinator", it should be also running on your server computer.

Powered by ScribeFire.