T-SQL CHARINDEX Returns 0 Incorrectly

March 5, 2009 14:25 by nat

I was tearing my hair out recently (I didn't have much hair left in the first place, which makes it all the worse!) when the CHARINDEX function was returning 0 even though the string I was looking for definitely existed in the input string!

The database column being searched was of type nText and it turns out that the function concatenates the column value (at what length, I don't know), unless you CAST it to a more usable data type.

So, I added a CAST([ColumnName] AS nVarChar(max)) to the script and hey presto it starts working!!

I would be very appreciative of a message or something to let me know that the input string was concatenated.

Hope this saves someone else from tearing some of their precious hair out!

Nat


Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

System.Uri.Query and the Ampersand character

February 6, 2009 09:50 by nat

I recently had a requirement to place an ampersand in a querystring as a value, now this is handled perfectly well by UrlEncoding when using the Request.QueryString() method, however, when dealing with a Uri.Query, it goes a bit wrong. When inspecting the Uri.Query value even after UrlEncoding the ampersand, %26 ("&" when UrlEncoded) has been UrlDecoded already, before you have a chance to do anything about it.

The way round this that I found is to HtmlEncode the ampersand too, so it ends up as "%26amp%3b", e.g.

string theUrl = "http://www.domain.co.uk/pagename.aspx?qskey=" + HttpUtility.UrlEncode(HttpUtility.HtmlEncode("lorem & ipsum"));

Then to grab it out of the Uri.Query value do the following

HttpUtility.HtmlDecode(this.Query.Replace("&", "%26"))

Now the querystring & delimiters show as "&"s and the encoded one as "%26".

 

 


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Dynamically adjusting height of IFRAME from child page

October 20, 2008 11:20 by nat

I had to do this recently and thought it would be tougher than it ended up being. Here's the code I used:

function adjustMyFrameHeight()
{
var frame = getElement("myFrame");
frame.height = document.body.offsetHeight + 60 + "px"; // add 60 pixels to be safe...
}

function getElement(aID)
{
return (document.getElementById) ?
     parent.document.getElementById(aID) : parent.document.all[aID];
}

Then, just simply call the "adjustMyFrameHeight()" method from the child page's BODY onload event:

<body onload="adjustMyFrameHeight();">

Et voila, c'est bon!!


Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Improving .NET Website Perfomance

October 17, 2008 09:18 by nat

A couple of the CJ Dev team were down in Brighton for Re-Mix 2008 a few weeks ago, and one of the most useful sessions I went to was "ASP.NET Front End Performance" by Chris Hay.

There's some really obvious things, but others that I didn't know. I've collated the key points together for easy reference:

  • Combine all JavaScript files together (i.e. single request)
  • Combine all CSS files together (i.e. single request)
  • Use inline scripts and CSS on the home page, load script and CSS assets post UI
  • Reduce image requests
    • Use image maps
    • Use large background images
  • Implement IIS caching
  • Use versioning policy on filenames to avoid unwanted caching (e.g. my-js-file-001.js)
  • Use Server.Transfer instead of Response.Redirect where possible
  • Use SilverLight controls for common areas of the site (headers, footers, etc.)
  • Implement HTTP compression in IIS
  • Use the .NET ViewState wisely
    • Only on pages where necessary
    • Compress the ViewState
    • Offload ViewState to Session if possible / advantageous
  • Avoid use of .NET UpdatePanel control where possible
  • Expose web services to AJAX for use by JavaScript code, manually altering DOM
  • Combine auto generated scripts by .NET framework (e.g. ScriptResource.axd)
    • Use script resource profiler to discover all script files
  • Use SilverLight instead of AJAX
    • Detect presence of SilverLight, and fail safe to AJAX if preferred to forcing use of SilverLight
  • Use Content Delivery Network where applicable (large projects) to bring the content closer to the user
  • Implement use of data caching
  • Use page / control output caching  
  • Queue requests for processing (if any large / long requests will occur)
  • Ensure all debug flags are removed from application

Cheers to Chris Hay!!


Currently rated 4.7 by 3 people

  • Currently 4.666667/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Regular Expressions - Everyday use with System.Text.RegularExpressions.Regex.Replace()

April 10, 2008 09:28 by nat

I recently had to display some text on a page where the source field was fully marked up html, but I only wanted the content. I imagined a long drawn out process of doing a string.Replace() on all html tags, and was considering booking a holiday, when REGEX came to the rescue. It was so simple - much simpler than I ever thought it would be.

Using the expression "<.*?>" to match all html tags, i then called the System.Text.RegularExpressions.Regex.Replace() method, which is very similar to string.Replace, only you can pass a regular expression to replace only the matches, and voila - all the html markup is removed.

Simple now that I think of it, but before I had seen this used, this task seemed a real pain in the behind.

A couple of useful regex resources:
http://www.quanetic.com/regex.php - on online REGEX testing tool
http://www.regular-expressions.info/reference.html - REGEX reference site - from basic to complex, everything you need.

Hope this comes in handy...

Nat


Currently rated 4.5 by 2 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Calendar

July 2010
SuMoTuWeThFrSa
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567