Using the ‘using’ keywork in C#

C# Super HeroIn the System.Data.SqlClient namespace, SqlConnection and SqlCommand are two examples of managed types that use unmanaged resources down in the COM layer of the run-time. Microsoft says that all of these types must implement the IDisposable interface.

Continue reading

Making Form Screenshots In .NET

Have you ever need to take a screenshot of your .NET form from code within the form?  Some sort of print screen method?  Don’t touch that PrtSc button just yet, there is another way.

No, I’m not talking about using the gdi32.dll or the Visual Basic Power Pack.  The .NET graphics object has it’s own built in screenshot-taking ability, and ImageSource is here to help show you how to use it.

Getting a screenshot of your form is as easy as instantiating a Bitmap object (sized to your form’s width and height), creating a graphics object from that bitmap and finally calling Graphics object’s CopyFromScreen method.  The code below demonstrates how to do this:

private Bitmap GetFormScreenshot(Form frmRef)
   //Setup the bitmap and graphics objects
   Bitmap bmp = new Bitmap(frmRef.Width, frmRef.Height,
   Graphics gfx = Graphics.FromImage(bmp);

   //Take the screenshot
   gfx.CopyFromScreen(frmRef.Bounds.X, frmRef.Bounds.Y, 0, 0,
       frmRef.Bounds.Size, CopyPixelOperation.SourceCopy);   return bmp;

Wow, it’s just that simple.  Enjoy the rest of your day knowing that you’ll be screenshotting your troubles away.

Adventures with Oracle IPM 11g (part 1)

Part 1

Oracle starting shipping its latest IPM product, 11g earlier this quarter. As a company, ImageSource has been working as a partner with Oracle on this next step in their ECM evolution. I, personally, am a little late to the 11g game, so I will be sharing with you my impressions as a relative newcomer to the 11g world.

Continue reading

When In Doubt – Log Out

Recently, while working on a project for ImageSource, I ran into a bit of problem that pretty much had me stumped.  I had written a .NET library to integrate with a 3rd party COM dll.  The code looked correct, it passed all of its test cases and ran just fine when compiled in Debug mode.  I created a test application to use this library and stress test it.  Everything checked out.

Then I compiled it for Release and obfuscated it.  Suddenly it did not run fine anymore.  The test application would consistently give me errors of varying severity.  Everything from Automation error The server threw an exception to The remote server machine does not exist or is unavailable.

Error Message

An always unwelcome message.

I could get various errors depending on my input parameters, but they didn’t always occur at the same time.  Sometimes I would get a COM exception after 10 iterations of a loop, other times it would be 20.

At first, I thought the issue was that the code was obfuscated.  I’ve run into some small bugs in the past when obfuscating code, and thought this was no different.  However, testing a non-obfuscated Release build of the code produced the same problem.  Only switching back to the Debug build fixed the issue.  Lots of head banging ensued.

After trying to figure out error messages that seemed to have little  rhyme or reason, I finally went back and looked at sample code provided by the 3rd party.  After testing their code and finding that it run fine in Release mode, I had to figure out what this code was doing that mine was not.

Their code called a logout method in the API and mine didn’t.

Looking at my own code, I had completely missed making that call and it made all of the difference in the world.  My code was staying logged into the server, or at least causing problems server-side because I wasn’t disconnecting.

One added line later and the Release build of my library was working perfectly.  In fact, even the obfuscated version worked without a problem.  All of that grief and headache caused by one line of missing code.

So I guess the moral of this story is this: Make sure that you close you connections, you release your objects, that you make sure you clean up your mess.  Because if you don’t, it could come back to bite down the road.  Also, don’t be ashamed to go back and look at sample code when you’re stuck.  Sometimes the answer is right in front of you.

SharePoint 2010 Deprecated Types and Methods

If you are writing code for SharePoint 2010, please refer to the the link below to avoid calling deprecated APIs.