Tuesday, December 15, 2009

Thursday, December 10, 2009

DataBind

With nothing of particular note to bulk up this entry, I'm hoping to later collect all my knowledge on data binding into one post...

The Links...

Tuesday, December 08, 2009

Lifehacker Roundup

And lastly, from the Network Lights site: http://www.itsamples.com/index.html - the best part? They provide source code for a lot of stuff.

Stuff it, instead of consecutive posts for this stuff, I'll just build up this week's...
And the fun continues on Thursday...

Wednesday, December 02, 2009

Debugging Tools And Fun, Erm...

Some useful links if you're trying to find web dev debug tools:
Of course, deploying your DLLs in debug mode can help your logging, but release mode is going to provide at least 4x performance improvements. If you attach a profiler for checking memory usage or performance, you can leave your .pdb files up to get more information as these contain the .NET symbols used for inspecting the code. Please don't leave them there, as they are a serious security consideration - in short, if a hacker were to copy your pdb files from your server they've effectively stolen everything but the source code:
Here's some profilers that don't involve installing Visual Studio on a live server (always a bad idea):
There are, of course, sites out there for providing information about your site:
Of course, if you're intent on debugging in Firefox (www.firefox.com, of course) you should have:
Some tools for developing your code (essentially, my favourites, as there are tonnes, Microsoft's list notwithstanding (http://msdn.microsoft.com/en-us/magazine/cc300497.aspx)):
And a list of developer must-have's:

Thursday, November 19, 2009

Dealing With Exceptions

Knowledgeable blogs on the subject of debugging, crashes, exceptions and dumps - good reading:
Grabbing the dump file and getting at the data from the dump:
Very useful links for debugging, including dump file cheat sheet and analysis check list:
Every developer's starting point when looking for debugging tools:
Page of links for Microsoft debugging tools:
WinDbg is the top one:
At time of writing this is labelled "Install 32-bit version 6.11.1.404 [16.9 MB]"

And finally, why you should use try...catch...finally, or even try...finally:

Sunday, November 01, 2009

XSLT, XPath Knowledge Base

XPath explained and easy-to-use grid of descriptions...
Very useful XSLT scripts (including url encode/decode templates)...
A list of XSL elements and descriptions...
.NET 3.5 page on navigating XML in XPath...
Using XPath and LINQ...

Saturday, October 31, 2009

String.Reverse And Microsoft Interviews

A question which often comes up in interviews (and, I'm told, at Microsoft) is "write a method/function to reverse a string."
This, of course, isn't very difficult and lots of people get it wrong or give up. The following links are useful in getting the best answer, but really the best answer is how you work it out at the time. My opinion, though, is that the best answer is along the lines, "I'd look up some good examples and apply what I've learnt to the actual problem I have." This is appropriate simply because most people at work (especially when programming) are not in the firing line solving some real time James Bond type challenge and that there is usually a better solution created/invented/found by someone more intelligent/luckier somewhere in the world.

None of the points above are excuses for laziness or stupidity, however. Mostly, in my experience, points are gained by having experience and applying it well. Therefore;
It's also worth noting that:

string reversed = forwardStr.Reverse();

does not work. See link above.

[EDIT]

I'll include some extra links here, found recently while pondering the current state of affairs in the world. It's interesting to note that (it seems) a large number of interviews think that some (alleged) programmers are not, in fact, programmers...

Monday, October 19, 2009

Roundup: Week Ending 16/10/09

Hmmm...
And the not-gizmodo not-lifehacker...

Chasing The Tail

Sunday, October 18, 2009

System32 Always Opening! Argh!

I've had this infuriating event happening every time I log into Windows. The Windows\System32 directory keeps opening up in Windows Explorer, every single time, just after I log in. This didn't used to happen, is really annoying and just highlights how crappy Windows can be.

Fortunately, there's a solution - even Microsoft know about this. And it's weird. My initial google search on this brought back this handy little site:
Which lead me to this Microsft knowledge base article:
Which lead me to opening the registry editor (Start -> Run -> regedit), following the path down the registry folders to:
  • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
And finally deleting the entry for "MultiScreen", which had a value of " ". Like I said; Weird.

Anyway, this seems to have solved the problem. Why there is code somewhere inside Windows which opens the System32 directory when you log in if there is an invalid registry entry is beyong me, but at least it's fixed now.

Thursday, October 15, 2009

How To Capture Page Output, Caching

Found this on the net, while trying to capture the rendered output of a page...
Only takes a little modifying to adjust to writing to disc. In .NET2.0 and above you can use the File.WriteAllText to dump out your html file and easily implement disc caching:

The Fear Of The Noob

Monday, October 05, 2009

That Will Always Be The Dream

Roundup: OCR, LifeHacker, Gizmodo And The iPhone, For The Week

This week's latest...
Some verrry interesting stuff last week...

Wednesday, September 30, 2009

Useful, Popular And Commonly Required MSDN Pages

Just thought a collection of these would be good. I'll try to keep it updated...

Tuesday, September 22, 2009

Monday, September 21, 2009

Properly Handling CDATA In .NET XSLT

Prologue:

I have a T-SQL table of data, one field of which contains escaped X/HTML - that is, it has XHTML where the important characters, like < and >, are represented as &lt; and &gt; etc. Some of this XHTML is broken because the writer was probably using a rich-text editor which sent fragments of XHTML to the database. Of course, using SQLXML to pull this out I find that the body of some of the elements contains this escaped HTML. So now I need to unescape it back into normal HTML, so it can be rendered on a page without hassle, but without making it part of the XML document.

Sumary:
Ok, so, in summary, we have valid XML with escaped XML (or XHTML) which needs to be wrapped sensibly so it's not mixed up and we need to do this in .NET.

First problem:

Unescaping the code, in XSLT, is a little tricky, but if you assume every element which needs unescaping is the same, then we can do this:

<xsl:template match="Description">
<xsl:copy>
<xsl:value-of select="node()" disable-output-escaping="yes" />
</xsl:copy>
</xsl:template>

What this does is match every element named 'Description' in the original XML and unescape it's content - so code like &lt; and &gt; become < and >. Simple. If you want to do that for specific elements only, just move the 'value-of' XSL element to where you want it.

Second problem:

We now have XML containing some more XML, which should really be considered plain text, until it's needed. So, we want to keep this 'inner XML' as it is (ie: pretty XML and not escaped) but indicate that it's not part of the current XML document.

Second solution:

Wrap the body content of the elements we want to remain as body content in a CDATA tag, like this:

<xsl:template match="Description">
<xsl:copy>
<![CDATA[<xsl:value-of select="node()" disable-output-escaping="yes" />]]>
</xsl:copy>
</xsl:template>



What the CDATA block does is tell any parsers that the content is to be, essentially, ignored. Simple.

Unfortunately, the CDATA block tells the parser not to parse the content within it, so what we actually end up with is:

<Description>
<![CDATA[
<xsl:value-of select="node()" disable-output-escaping="yes" />]]>
</Description>

Third problem:

If you just add the CDATA block to your XSLT a standard parser will take the XSL element within it as to be ignored, and not process it.

Third solution:

The answer to this is not to include the CDATA block ourselves, but to tell the XSLT document which elements need to contain the CDATA block, instead. Thus, the XSLT parser will wrap them itself. We do this by adding the 'cdata-section-elements' attribute to the output element of the XSLT document:

<xsl:output method="xml" indent="yes" cdata-section-elements="Description" omit-xml-declaration="yes" />

Now, every <Description> element, in the output XML, will contain a CDATA block which wraps the body content of the element, like this:

<Description>
<![CDATA[
<strong>Some content that was parsed from the value-of XSL element.</strong>]]>
</Description>

However, if you have been trying this as we go along and seeing something different - ie, it's not working for you - that's because, in .NET, you have to handle XSLT in a certain way.

The short answer is, here's the C# to do it:

/// <summary>
/// Takes a string containing XML to be parsed and a string containing the the XSLT to do the parsing.
/// </summary>
public string NewTransform(string xml, string xsl)
{
// create the readers for the xml and xsl
XmlReader reader = XmlReader.Create(new StringReader(xsl));
XmlReader input = XmlReader.Create(new StringReader(xml));

// create the xsl transformer
XslCompiledTransform t = new XslCompiledTransform(true);
t.Load(reader);

// create the writer which will output the transformed xml
StringBuilder sb = new StringBuilder();
XmlWriter results = XmlWriter.Create(new StringWriter(sb), t.OutputSettings);

// write the transformed xml out to a stringbuilder
t.Transform(input, null, results);

// return the transformed xml
return sb.ToString();
}

/// <summary>
/// Takes an absolute path to a file which is then loaded into a string and returned.
/// </summary>
public string LoadFileAsString(string fullpathtofile)
{
using (var sr = new StreamReader(fullpathtofile))
return sr.ReadToEnd();
}

The key to ensuring that the cdata-section-elements attribute, and therefore the CDATA block, is handled correctly is the t.OutputSettings - passing the settings to the writer informs it of the intentions of the XSL transformer.

Epilogue:

I battled with all of the above until I stumbled upon this post, so thank you to everyone there:

Thursday, September 17, 2009

Augmented Reality On The iPhone

Well, I've finally found something cool, useful and right here, right now...
I found this via the original link...
Now, signing up (free, easy and quick) to the iPhone app Yelp, gives you something very useful: Switch on Monocle (shaking the iPhone isn't necessary any more - I discovered too late!) and it'll show you which restaurants, bars, etc are in the direction your phone is facing. It's that easy to find places. Tapping the screen takes you to the contact information and from there the world is yours!

Yes, I'll say it again, I'm in love with the iPhone. Well done Apple - and all the enterprising developers out there making the best use of a very good gadget!

Space Photos On A Budget

Recently, some MIT students took their own photos of the Earth's curve...
Further reading, mostly derived from the above links, but the Apollo stories are some of Mankind's most important, heroic and touching...

Wednesday, September 16, 2009

Make Sure Your IIS Uses The Correct .NET Version - Unrecognized attribute 'type'

Recently, I created a new .NET 3.5 web site in VS2008 and started getting this error:
  • Parser Error Message: Unrecognized attribute 'type'.
This page helped me here:
Essentially, the problem is that IIS must be configured to use the correct version of .NET to run the site. Therefore, I opened the IIS panel "properties" for my site, clicked the ASP.NET tab and switched the version from .NET 1 to 2 and everything was fine.

Point IIS to .NET 2 even for .NET 3.5 web apps.

So, yes, the issue was I was using the wrong version of .NET to run my website. D'oh!

Lifehacker Fun

LifeHacker fun...

Wednesday, September 09, 2009

iPhone Problems And Solutions

Well, I love the iPhone, but like any device it's not perfect. Ok, I've not found anything that isn't pretty much perfect (built by Apple, at least) but having found my first problem, hopefully here's some solutions, some generally useful links follow...
  • Unsent email won't send. A sent email fails to appear in the "Sent" folder and the message "1 unsent email" displays at the bottom of the mail application.
    For me, this resolved itself, I assume by finally managing to send. Here is a useful link related to the issue:
    http://support.apple.com/kb/TS1426?viewlocale=en_US
Generally useful links to be aware of:

Sunday, September 06, 2009

Loving Up The iPhone

Finally made the switch from a dying T-Mobile Windows Mobile MDA Vario II, to the 16GB iPhone 3GS and it's awesome! This thing completely surpasses all expectations in performance, usability, speed and sheer number of applications available - there are far more applications than for Windows Mobile and they are so easy to get. The addictive nature of downloading to the iPhone, I think, would compare to the discovery of P2P file sharing.

Anyway, I was disappointed when I discovered (yes, lots of this is new to me) that I can't just dump files onto the iPhone like I can with the iPod (using it as an external HD, for example) or that ringtones cannot be selected from any music on the device. I did, though, find a very good page with instructions on how to do this. Originally written, then tidied up, for the iPhone 3G, this page is accurate for 3GS as the method is fairly generic;
In short, it goes like this:
  1. Get an audio file and edit to around 20-30 seconds
  2. Save as MPEG-4 (.m4a - see WavePad for easy converting)
  3. Change file extension to .m4r
  4. Drop into the Ringtones section of iTunes
  5. Sync Ringtones to your iPhone
The ringtones will now be available in the Sounds section of you iPhone's settings. Easy. There is more detail on the above link and a pointer to the http://audiko.net/ site for even easier conversions, but manual conversion was my preference (and is really very easy.)

WavePad is available here:
On the other side of the coin, we have videos for the iPhone. As with iPod and iPod Touch, there are plenty of videos to purchase or stream (iPhone comes with a YouTube app) but what if you have a load of wmv's or avi's you want to just dump there? I would get the iPod/iPhone video converter Videora because it has all the "want that one" simple functions that you could need, while being fast and producing quality output. Then it's simply a case of syncing your iPhone to your videos (videora can auto-add conversions to iTunes' library or you can manually drop them onto the phone) to make them play on the iPhone.
Red Kawa, the makers of Videora, even have easy-to-use apps for making wallpapers for the iPhone, but with the wallpaper selector built in, I don't think those are necessary.

Tuesday, September 01, 2009

Friday, August 21, 2009

Equals Equals Vs. Equals

Recently I was asked, "What's the difference between 'a == b' and 'a.Equals(b)'. The short answer is that the second is a method call (easily overridden) and the first is not. While this is true, it's not the whole truth.

The real answer breaks down into two parts. First, for value types such as int, bool and float, the '==' is a direct comparison - it does compare the value of a and b. This is also true for 'string's, though these are a special case. For value types the '.Equals()' also compares the value of a and b because every value type can be treated as though it is boxed - .NET implicitly allows the class methods and properties to be called on their value type siblings.

For reference types - those created from classes, such as String, Int32, MyCarClass etc - the value comparison '==' cannot be called because an instance of a class is not the most primitive type of data the system can work with (in fact, Java calls value types 'primitives'.) In this case, the class must provide an 'Equals()' method - and every class does because all classes inherit from Object. However, because the default implementation (from Object) of 'Equals()' is to compare the references (ie: memory address value, not the data contained at that address), if 'Equals()' is not overridden you might find that two objects with the same value return false from Equals().

All the classes representing value types provide the appropriate Equals method (in that they all compare the value and not the reference of the object.) But for your own classes you must override Equals and provide your own implementation - because .NET does not implicitly know how to compare instances of your own classes. (It is possible to write reflection code which can do this, but it is long, difficult and prone to infallibility, so don't do it - unless you fancy a learning challenge ;)

So, in short (but not as short as earlier) the guide is:

For int: == compares the value, .Equals() compares the value (this is the .Equals() in Int32).
For Int32: == compares the value by calling the .Equals() in the Int32 class, .Equals() compares the value.
For MyInt32: == compares the references of the variables, .Equals() calls the method in your MyInt32 class.

If you want the '==' operator to perform a value comparison on instances of your own classes, you can use the 'operator' keyword and define your own '==' functionality;
For example, in a class called 'MyClass', if you have a working 'Equals()' method:

public static bool operator ==(MyClass a, MyClass b)
{
return a.Equals(b); // or whatever comparison you want to do
}

Another site with some code:

Thursday, August 20, 2009

Tuesday, August 11, 2009

I'm Back, And It's Ok

Thought I would just post something that might be of use to anyone trying to get back-end (or "code-behind") code working inside the ASPX or ASCX of their control - this can be useful to avoid having to do a full code release or if the page/control is dynamically loaded, say from a DB or plain text...
Isn't that great? Just a script block with runat=server and you're done.

This might also be useful:
Run along now, you're done.

Tuesday, July 21, 2009

Swapping Two Numeric Variables In Only Three Instructions

Having done this in the past, needing it again and finding this page:
I've found that two numerics can be swapped quite easily without the use of a third variable:

a = a + b
b = a - b
a = a - b

Neat.

Wednesday, July 08, 2009

Google OS - What's To Come? Goose?

Google have announced that they plan to start building an Operating System. This should be fairly exciting for anyone who mildly doesn't like Windows and is free to start using another system. Also, exciting for anyone who isn't, but also believes that the new GOS (Can we start calling it 'Goose' yet? Too soon?) will be compatible enough to allow easy migration. As a .NET developer myself, I'm doubtful. The only reason that I stayed with Windows Mobile on the smartphone was simply because I could write my own apps for it. Now I've not done that in a dog's age, I'm switching to iPhone ASAP! Android, I'm afraid, hasn't had a look-in. Sorry, it just looks too flimsy.

I guess the other question is that, what with a bunch of things that require Windows to be able to be installed, will all downloadable apps from Google be GOS-compatible right out of the barn?

Monday, July 06, 2009

Canonical Tagging - SEO

Google, Microsoft and Yahoo can agree on one thing:
It's a good idea to be able to identify the original ('canonical' - http://dictionary.reference.com/browse/canonical)

Wednesday, July 01, 2009

Tuesday, June 30, 2009

How HR And A Programmer Read A Programmer's CV

he he he: http://gandolf.homelinux.org/~smhanov/blog/?id=56

Tweet Buffer: Twuffer

Hmm, I quite like this idea, simply because it's a neat utilisation of twitter which you might think should have been part of the original web app (twitter) but is just a sensible extension - and a good example of third party expansions to open API's:

Creating A Shortcut To Open Current File In Explorer From Visual Studio

Here's a neat little item that most people won't know about in Visual Studio 2008 - opening the directory of the current file in windows explorer:

I Know It's A Joke, But I Might Do This

Monday, June 29, 2009

Local And Global Temporary Tables In SQL Server

Some good information for those wishing to create temp tables in their stored procs...

It seems that CREATE TABLE #tablename is very local, however ## is not so local after all...

Thursday, June 25, 2009

Monday, June 22, 2009

Google Feeds - Why RSS Should Be Embraced

Well, having previously thought Google didn't like to provide RSS feeds (you can see the content essentially being stolen) here's how they decided to implement it:

It's Done. And I'm Shattered.

Well, thankyou to everyone who has (or will have) sponsored me for yesterday - you can now hit me up at http://original.justgiving.com/matthewwebster/

You can check out my tweets at #londonbrighton or http://www.twitter.com/horacebury and my photos at http://www.twitpic.com/photos/horacebury

Sunday, June 21, 2009

London To Brighton Bike Ride For BHF

I would like to remind everyone that you can sponsor me for the London to Brighton (58 mile) bike ride on June 21, 2009: http://www.justgiving.com/matthewwebster

I really appreciate any and all sponsorship I can get.

If you want to take part, please visit BHF's website where, for the first year ever, you can sign up online: http://www.bhf.org.uk/

Friday, June 12, 2009

An Interesting Piece Of Research On Weight Loss

This seems to back up the information about simply moving about more each day eating more healthy meals, rather than cutting down drastically and exerting yourself ridiculously, to be the key to successful weight loss:

Subversion Explained

This enterprising young chap has broken the Subversion (SVN) source code control and versioning system down for all us pro techy geeks...

Windows 7 To Ship Without IE In Europe

Yep, finally, it's true and Opera's whinging has finally paid off. Sort of...

Favourites From Salami Tsunami

Yes, it's true, Dusty Scott, aka Pork Tornado, aka Salami Tsunami, is the greatest blogger in the world...

Swine Flu Is a Pandemic

http://www.time.com/time/health/article/0,8599,1904062,00.html

Swearwords

If you want to block swearwords on your site, here's some resources. Things to think about are your intended audience, the seriousness of your site and how strict you want to be...

Power Save, Bread And Back To The Future II

Lifehacker and Gizmodo to the rescue, again...

Saturday, June 06, 2009

Acorn Gamez - Reliving The Wonder

While getting a bit hacked off with a PC yesterday, I went looking for some videos from years gone by. I found some videos by a guy who really should be linked: http://www.youtube.com/user/peetvanpeebles

I really have to say thank you to Peet Van Peebles here - these videos are so inspiring! I just want to fire up the old Risc PC and get playing again :)