Wednesday, 10 February 2010

Thursday, 17 April 2008

Cannot resolve the collation conflict T-SQL

Cannot resolve the collation conflict between XXX and YYY in the equal to operation

I recently got this whilst doing some SQL queries that involved joins between two fields with different (albeit very similar) collation types. To stop SQL server from screaming, you'll need to perform a sort of collation cast. The quickest way is to cast one of the field's collation so it matches the other, or you can cast both. Casting to the default database collation is also an option:

JOIN table t ON t.field COLLATE database_default = s.field COLLATE database_default

More info on collate from MSDN.

Sunday, 13 April 2008

Use CSS not JavaScript for rollovers!

Why use Javascript for your rollovers when simple CSS will do? I prefer CSS over Javascript because it degrades better in non-compatible browsers in comparison to JavaScript and it's so straight forward to implement in CSS, just take a peek below...

CSS rollover example in action:

CSS rollover code:

<style type="text/css">
 display: block;
 width: 50px;
 height: 50px;
 text-decoration: none;
 background: url('');
 background-position: -50px 0;
<a class="rollover" href="#">7lt;span class="hidden">Hidden Text</span></a>

Key points about the example

The image used is made up of two images; this is often referred to as a CSS sprite. Having just one image is better than two because you've halved the number of requests needed to retrieve the images, resulting in better site performance. Initially, the first 50×50 pixels of the image is shown (CSS element a.rollover). Then when we activate the rollover with the mouse the CSS element a.rollover:hover is activated, which moves the background image left by 50 pixels this results in a different image being output.

To counteract the loss of text value for the link because images are used (text in links is used by search engine crawlers and screen readers), we have a span that contains text - but the span text is hidden using the display:none; CSS attribute.


There is a great article with some more advanced examples by Dave Shea called CSS Sprites: Image Slicing's Kiss of Death which you should check out.

Thursday, 10 April 2008

How to access the Call Stack C#

As methods are called, information about them are placed on the call stack. The call stack is invaluable in development work for debugging problems, but it can also be useful in other situations (e.g. when logging for an entire app or library is processed by a logging utility in one location).

.NET makes it simple for us to work with the call stack. The functionality is provided via the System.Diagnostics.StackTrace, System.Diagnostics.StackFrame and System.Reflection.MethodBase classes.

Access the call stack via a new instance of the StackTrace class. The stack contains stack frames, these frames represent each method executing on the current thread; programmatically access a frame via a StackFrame instance. Using the StackFrame.GetMethod(), which returns a MethodBase object, you can now easily retrieve the detailed information you wanted. The sample code below demonstrates this.

// First create an instance of the call stack
StackTrace callStack = new StackTrace();

// Next select the frame we want...
// 0 : current frame for the current method
// 1 : Frame that called the current method
// 2 : Frame that called the frame that called the current method
// 3 : get the idea!
StackFrame frame = callStack.GetFrame(1);

// Using StackFrame.GetMethod(), which returns a 
// MethodBase object, we can obtain detailed 
// information about about a method
MethodBase method = frame.GetMethod();

// Get the declaring type and method names 
string declaringType = method.DeclaringType.Name;
string methodName = method.Name;

Friday, 4 April 2008

Configure log4net only once

log4net is a great logging framework with powerful levels of flexibility. This flexibility can result in complexity when used in larger systems. One key point when using log4net in complex systems is to remember to configure the file to log to just once per process.

The file to log to is configured in the call to log4net.Config.XmlConfigurator.Configure() call this just once per process

In complex and constantly changing systems, it isn't always clear whether log4net has been configured. One option is 'if in doubt leave it out', and if you get no logging you know to add the configuration call. But the smart ones out there might disagree with this approach - just as I do! I prefer to just check log4net to see if it is configured before attempting to configure it:

// Only configure log4net once per process
if (!log4net.LogManager.GetRepository().Configured)
    // Call log4net.Config.XmlConfigurator.Configure();
    if (log.IsDebugEnabled)
       log.DebugFormat("Loaded log4net config in {0}.", System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Assembly.FullName);
    if (log.IsDebugEnabled)
       log.DebugFormat("log4net already configured for {0}.", System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Assembly.FullName);

Some people might see the immediate logging as unnecessary, but I like to add it so I have a visible record that log4net is correctly configured.

Thursday, 3 April 2008

Removing XmlDocument white space c#

I've recently been working on matching certain API calls with XML data pulled from an XML file for testing purposes. I noticed there was a large amount of white space left in the XML when pulled from the resourced XML file; which is something I didn't want.

I thought setting the XmlDocument.PreserveWhitespace property to false would remove this for me, but it just seems to remove the preceding and trailing white space; making it similar to the string.Trim() method. I needed to use something akin to string.Replace() (this replaces a specific substring with another substring), but more powerful. Here comes the Regex.Replace function to the rescue, which is a bit like string.Replace() on steroids! Regex.Replace() allows replacement of text using regular expressions, something which can make make complex replacements a piece of cake.

Here is the code to replace white space in XML or any XML dialect (such as HTML - or XHTML):

// Remove inner Xml whitespace
Regex regex = new Regex(@">\s*<");
string cleanedXml = regex.Replace(dirtyXml, "><");

Friday, 28 March 2008

HttpHandler for all requests in ASP.Net on IIS

Setting up a HttpHandler for handling ALL incoming requests is straight forward with .Net and IIS. Provided you've created your .Net HttpHandler project to sucessfully handle the requests, you need to do the two following tasks:

1) Ensure that the web.config references the HttpHandler you've created

To the httpHandlers section of your web.config file add a reference to your httpHandler for all HTTP verbs and extensions:

      <add verb="*" path="*" type="your.assembly.reference"/>

2) Setup IIS to route all request to your handler

a) IIS 5 (Win XP)
  • Open the website properties in IIS
  • Select the Home tab
  • Click the Configuration button
  • In new window select the Mappings tab
  • Click the Add button
  • In new window's executable field Browse to the ASP.Net ISAPI dll (usually something like C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll)
  • In the Extension field type .*
  • Untick the Check that file exists box and click OK
N.B. There is a bug that if your OK button is not active you need to click the executable field text box, you should see the middle section of the text in the box change from /.../ to the full file path this should now make the OK button active.
b) IIS 6 (Windows Server 2003)
  • Open the website properties in IIS
  • Select the Home tab
  • Click the Configuration button
  • In new window select the Mappings tab
  • Insert an entry in to the Wildcard application maps with the ASP.Net ISAPI dll (usually something like C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll)
  • Untick the check that file exists box and click OK

You should now see all requests being handle via your handler. If you are having issues, a good place to start finding out why is your IIS logs - but where are my IIS logs?. Good luck!