Handling various types of SQLException in C#

I had a usual scenario of handling various SQLExceptions and I found that there is in fact a very nice table which holds a list of all such scenarios.You just need to run the following query in your master DB.

SELECT * FROM sysmessages

And you can handle the errors in the C# side by looking into the Number property for SQLException.

try
{
}
catch (SqlException exception)
{
if (exception.Number == 2601)
{
//Ignore.Its ok I can live with this!
}
else
throw;
}

Copying Directory or File in VSTS test cases

Well this one was pretty nasty I must admit.I didn’t know that I would lose some time on this one.And since I did I might as well blog about it.So here goes the problem statement.Have you faced the scenario wherein you had to copy some files under some folder in Unit tests in Visual Studio? This is because the test cases run under a folder called test results and each time you run it creates a new folder for that particular test case. Mind you before I start I must say that I have encountered this on the VS-2010 and not on the nascent versions of Visual Studio viz 2012.So here goes as to what you need to do to achieve the same.

1)First go the TestSettings file in VS 2010 which is directly under solution.
2)Go to Deployment section and check Enable deployment and add the directory that you want to add.

testsettings

3)Now is the trick part you have to edit the Test Settings file in Xml editor and go the tag under Deployment which has the required DeploymentItem and the attribute outputDirectory as follows.

deployment

4)Finally the worst part was now when you run the unit tests you will still now find it added.So to get it in working in VS 2010 you have to select the test list editor under the Test–>Windows menu in Visual Studio IDE.And then select your tests and hit run.Eureka… ooo still not there yet.The directory or file is still not out there!

5)Close your Visual Studio IDE and open it once again.(Age old trick works in the realm of WINDOWS).

So happy testing once again 🙂

XML Diff – Compare XML Tool

Well off late i have been having a torrid affair with XML as the data that flows across into my modules is xml. And so I had to do a series of tests on them to validate the modifications we were doing on the input xml. So hence started my hun t for xml related tool to compare two xmls .After searching for sometime i understood that the overwhelming favourite was XMLDiff.So here goes the simplest of all tools.All you need to do is

1)Download the tool from here.
2)Install the xmldiffpatch.exe
3) Go to $:\Program Files (x86)\XmlDiffPatch\Bin where you will find xmldiffpatch.dll
4)Include in your project as a assembly reference and you are ready to code.

A basic simple one that i used is the following overload of Compare in public bool Compare(XmlNode sourceNode, XmlNode changedNode) for the XmlDiff tool.The code for the same goes as follows.


string original =
@"SHERLOCK HOLMESCONAN DYLEWILEY";
String modified = @" CONAN DYLE WILEY SHERLOCK HOLMES ";
XmlDocument document = new XmlDocument();
document.LoadXml(modified);
XmlNode newNode = ((document).DocumentElement).ParentNode;

XmlDocument document1 = new XmlDocument();
document1.LoadXml(original);
XmlNode originalNode = ((document1).DocumentElement).ParentNode;

XmlDiff xmldiff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder |XmlDiffOptions.IgnoreNamespaces |XmlDiffOptions.IgnorePrefixes);
bool bIdentical = xmldiff.Compare(originalNode, newNode);

As you can notice i have spaces in between tags and the ordering of the child elements is not correct ,but still they will compare and return TRUE.
There is another overload of Compare() method,which takes in two file locations.But i didnt like to pass ,paths of two different files as I thought it was cumbersome,hence i took the overload with XNode as parameters.
There are several other overloads to try out as well as Diffgram, the changes between two XML documents are described in a document called an XDL Diffgram. Yet to try that out but looks like lot of fun with Diffgram.

DotNet Decompiler dotPeek

Ever since Reflector became paid from free, dotNet developers have been searching for a good alternative.They even said till version 6.0 the Reflector would still work,but for some reason it never used to for me,even if i would never upgrade my reflector.Anyways i found a good one called dotPeek from the JetBrains(yeah the same guys who have that wonder tool called ReSharper).You can download from here.And you can decompile from .NET versions 1 to 4.5 . Good luck reengineering !

Tip for the day!

I was recently working with xmls and xsd’s and came across this wonderful utility called XSD.exe.Using this you can create classes or xmls from schema’s and viceversa as well.Wonderful aint it?
Example
Generate classes from a given xsd

C:\classfiles>xsd “C:\SampleServices\Item\Lookup.xsd” /classes
For more info go to the path where xsd is(mine is C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools or C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\x64.This location is for VS2010 as you would have already presumed) in the command prompt is and hit

$:\xsd /h

And did you know you can even generate schema for a type from a dll or exe.. Cool eh?!

How to a run the ASP worker process under another user credential

Well i discovered this when i had to debug a service on a server on which i no permissions.Since it was a WCF call and was hosted on the IIS,i figured i needed a cheat.So this is what i did.Before i start with the details let me tell you the environments that i have been using.VS-Studio –2008 dotnet stack on Windows XP(sp3).And i was using IIS 5.1 on my machine which is an XP.So here goes.

First change the username and password in the machine.config under the processModel tag as shown below.

Code Snippet
  1. <system.web>
  2.     <processModel autoConfig="true" userName="username" password="******"/>
  3.     <httpHandlers/>

And i tried adding permission to the C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files folder too.

That resulted in an error which gave an “Failed to access IIS metabase” error .So finally i figured MetaAcl tool needs to be downloaded from the following location

http://download.microsoft.com/download/5/7/3/57316f36-ded6-41f0-b694-8b0102ade818/metaacl.exe

This has a metaacl.vbs script which adds permission to the users.You need to add the file to the following folder c:\Inetpub\Adminscripts and then go this folder from the command prompt.Then run the command

c:\Inetpub\Adminscripts>cscript metaacl.vbs IIS://LOCALHOST/W3SVC
                                     mydomain\mydomainaccount RW

 

For more info go to the following KB article in MSDN.(http://support.microsoft.com/kb/326902)

Shortcuts in Visual Studio

As i have self professed earlier, i am a rather lazy coder.So i thought if i had a few newbie Dotnet brethren down there somewhere,who might find the following interesting.(Pros this is just a cakewalk for you!)I am just jotting down some of my favorite Dotnet shortcuts,though there are a few exhaustive hundreds of them.Here are some of them.

Ctrl + “.” ——–Adds the references, ie it opens the tag window and we can just hit Enter when it pops up.

image

Ctrl+K+C and Ctrl+K+U ——Comments and Uncomments code

Ctrl + ”-” and Ctrl +Shift + “-”————Used for navigating backward and forwards

Ctrl +X or Ctrl + L ————No need to select the entire line and do a shift+home or shift +end,stay anywhere on the line and just do it.It cuts the entire line.Similarly for Ctrl + C.

F7 ————-Changes from Design view to Code view or vice versa

F12 ———Go to definition

Shift + F12—— Find all references

Ctrl +M+L and Ctrl + M + O ——– Collapse and expand definitions.

Ctrl +Shift +V ——- To rotate the pasted items in the clipboard.ie you can get prevously copied snippets!only 20 at a time.

Ctrl + U and Ctrl +Shift +U—— to make the variables lowercase or uppercase.

Ctrl + R + E ——- if you have missed writing a property with prop + tab + tab,dont worry,first click on that line and type this shortcut which basically opens the encapsulate method smart window.

Alt + Ctrl + P —– Opens the Attach Process Window

Ctrl + D + P ——- View all the attached processes.You can individually detach each process here.Pretty useful especially if you want to detach one and add another

image

Alt + Ctrl + Q or Context  + Q——opens QuickWatch

Ctrl + K + D ———–Formats(sets proper indentation) the entire code in the current file .Or else just got to the last curly braces and just remove it and add it again.Voila its done!

Ctrl +Shift+F10 —– set the execution point to the current line

Ctrl +Shift+F9 —– removes all breakpoints

Alt + Ctrl + I —– command window

In addition to the hundreds of shortcuts that you have,we can create more by going to Tools->Options->Environment->Keyboard and then specify the shortcut that you want.My Fav one, is the one that we are all used to doing with the mouse..File.OpenContainingFolder(i just assigned the Alt+O)

image

Trivia..try running this from Run/Command Prompt..(try this at ur own risks).Later you can go to the prompt and try out other switches like ”devenv /nospash”.

“devenv.exe /resetuserdata”.(the switch kinda gives away the clue!)

For the exhaustive set of C# shortcuts download the pdf from the following location.

http://www.microsoft.com/downloads/details.aspx?FamilyID=92CED922-D505-457A-8C9C-84036160639F&displaylang=en