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;
}

Find the table given a column name

I am using the below query pretty often nowadays, as i was digging my way,analyzing something in a legacy application.
You can replace the COLUMN_NAME in between the %% with the value that you want to search for.

SELECT T.NAME AS TABLE_NAME,C.NAME AS COLUMN_NAME
FROM SYS.TABLES AS T INNER JOIN SYS.COLUMNS C
ON T.OBJECT_ID = C.OBJECT_ID WHERE C.NAME LIKE ‘%COLUMN_NAME%’

I wanted to extend it to various databases as well and I found a cool little sp called sp_MSForEachDB .But couldnt get it to work 😦 with my above query.

Enable Fill and Cell drag and drop in Excel

I was scratching my head for sometime on this.I just wanted to keep it in my blog, so that i know where to get it from next time i lose it.The filling of cells by dropping makes copying in excel extremely fluid.But this option gets lost when you play around with excel in different formats.But this is what you need to do in case you lose yours.Go to File–>Options and then go to “Advanced” tab.Just check the enable fill handle and cell drag and drop checkbox.

drag

Setup Load Testing in Visual Studio

Well most often MS doesn’t setup stuff .So you have to do it for yourself.Like for Load testing in VS test cases,
you have to configure the database “LoadTest2010” and the corresponding tables like LoadTestRun,LoadTestCase,…
Well this is how you can do it in case you stuck at it.

Browse to this folder and execute in command line else you can always run the script in Management Studio.

cd “C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE”

SQLCMD /S localhost\sqlserver -i loadtestresultsrepository.sql

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.

Run Unit Tests in Visual Studio after locally building your Solution !

Ever felt the need to run unit tests as a part of building your solution locally?Yeah,that sounds like you can do fix/break solution immediately πŸ™‚ Ok,s before we jump to conclusions and go overboard, let us see how we can do this in VS2010.Like you would have already guessed there is an extension called “Continuous Testing for Visual Studio 2010“. Download it from here.Since its free and from Visual Studio Gallery ,I was tempted to install it.So just build it and saw that all my tests were failing as usual! Also in case you are pissed off that your test cases failed and dont want this feature you can always disable it from the Extension Manager from Tools Menu in Visual Studio.Here is the output post building locally,not great though πŸ™‚

unittest

BTW i had read posts that you can execute MSTest.exe from build events using commands,but i was facing some issue with it .So do let me know if you work with that as well.Anyways Continous Testing works great.So let me build and break with more abandon!

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?!

Metro vs Desktop

As i finally persuaded myself to take a shot at the latest OS in town primarily because of the huge discount that MS fished out to me for a win 8 pro upgrade from my all time fav win 7 and i happily took the bait.Here is the link for the same (http://windows.microsoft.com/en-in/windows/buy?ocid=GA8_O_MSCOM_Prog_FPP_Null_Null)

So there i was downloading the ISO at the very instant that i got a chance .Initially it would download the Win8 Upgrade Assistant and once you have finished reviewing it like this.

 

image

The assistant would also do a check whether the programs in the existing OS are compatible or not in the new Win 8 OS.

image

Once you have downloaded the ISO which has a size of approx 2.76 gigs,you are ready to go as you can either burn the it onto a drive or make a bootable Usb and install it .Please ensure that you have taken down the serial key before you move to other things.Obviously they would have mailed you the key as well,but keep it handy and safe.

There is a good article by Tim Fisher on how to clean install Win 8 which will pretty much cover everything you would ever want during installation.Here goes the link once again (http://pcsupport.about.com/od/windows-8/ss/windows-8-clean-install-part-1.htm) .And here is the part where he explains how to do it via USB. (http://pcsupport.about.com/od/windows-8/a/install-windows-8-usb.htm).

Once you land into the Win 8 world you will be taken aback initially by the Metro look but then you will feel back to Win 7 world when you hit Windows Key(this key acts as a switch between the 2 worlds).Never ever i have so often used this key as now.Its almost impossible to keep my hands of it as you would soon realise that nearly all the shortcuts are based on it.

Before we run and install the shortcuts,we will all feel the never ending urge to install all the required softwares and go to each of their sites and install .But do you remember them all?Don’t bother, following is the link from Scott Hanselman who has prepared a list of all the shortcuts in windows 8 here as well as a well written article on win 8 tricks.  (http://www.hanselman.com/blog/Windows8ProductivityWhoMovedMyCheeseOhThereItIs.aspx)

Also here is the list of most used software that we would ever need from Scott !(http://www.hanselman.com/blog/ScottHanselmans2011UltimateDeveloperAndPowerUsersToolListForWindows.aspx)

Finally to just let the whole thing start sinking i would leave you with a video from Scott on YouTube which would make you a lot more comfortable on the new OS.

 

My favorites in Windows 8 are hitting Win + PrintScreen and it goes directly into the picture folder as  a PNG.Also if you a dev guy you will find yourself hitting Win + X key a lot of times over the next few years!For people who hate finding the charms with your mouse hit Win + C.It is next to impossible not to love the new copy-paste mechanism built in .Did i mention the new Task Manager? Its a gem! And hitting del a file no longer asks you for confirmation ,unless you do shift +del.

Finally my two cents is Win 8 will not excite the enterprise market as the new OS is in neither land .Its not tablet nor desktop and this leaves a lot to be desired on a system which is not having touch screens.But then Windows were always like this.They were always the patches and fixes and bugs and SP1/SP2/SP3 guys.So they ended up having a OS which was a mix of everything.God forbid lest it become the next Vista!