This is my personal blog. The views expressed on these pages are mine alone and not those of my employer.

Friday, 29 May 2009

I was caught Google's StreetView!

On the way to work this morning whilst sending a text message I realised the easily distinguishable Google StreetView cam flying past on the top of a foreign registered corsa, and I've a good bet it was filming at the time. 

No images are online yet...but I'll be checking on a daily basis.  I wonder how long it takes them to process and upload the images?

Tuesday, 19 May 2009

Creating a windows service using the .net framework

Today has been spent attempting to write a windows service in the .NET framework (Using C#). While reasonably hard to get of the ground, after a bit of research they are actually only slighty harder to write than a simple WinForm. I do however want to repeat some bits of my research here, hopefully to make coding a windows service easier in the future:

  • They cannot be simply ran by hitting F5 and hoping for the best. They must be tediously installed (or if like me you can write an External Tool to Visual Studio :-)
  • You need to start the service manually (or again you could write a small batch file [using net start])
  • The service will not be started from a network drive (You will get an error along the lines of: Error 403 File cannot be found) generic huh?

And last but not least....you can't use the Timer control contained in System.Windows.Forms, nope, it has to be a cousin (albeit almost identical) control within the System.Timers namespace. Use of the latter won't cause any compilation problems, it simply won't work, as I eventually found out from this obscure Microsoft article.

As you can see, writing a Windows Service is no trivial task...

Happy coding!

Friday, 15 May 2009

How to create a ComboBox in ASP.NET

The .NET framework has long been deficient in the fact it DOES NOT include a ComboBox control as standard.  For those of you who do not know what a ComboBox is, it is essentially an editable drop down list (see here) which allows the user to either type OR select from a list.  I suppose it is rather analogous to the browsers address bar.

There are other options out there to provide this functionality (such as the AJAX solution above) and countless paid for controls, but what if all you want is a simple, no frills ComboBox?

Well it is actually rather simple to create one.

What is required is to add a TextBox and a Button (or if you're feeling fancy an image) and a ListBox below them, so that you have something like:

<div>
   
    <asp:TextBox ID="TextBox1" runat="server"
        style="z-index: 1; left: 102px; top: 28px; position: absolute"
        Height="20px"></asp:TextBox>
   
    <input id="Button1" type="button" value="V" style="width:20px; height:28px; z-index: 1; left: 239px; top: 27px; position: absolute" onclick="toggle_visibility('div1')" />
   
    <div id="div1" style="display:none">
        <asp:ListBox ID="ListBox1" runat="server"
            style="z-index: 1; left: 102px; top: 59px; position: absolute"
            Width="155px" AutoPostBack="True">
            <asp:ListItem Selected="True">Sids Co</asp:ListItem>
            <asp:ListItem>Jacks Co</asp:ListItem>
            <asp:ListItem>Daves Co</asp:ListItem>
        </asp:ListBox>
    </div>
   
</div>

Then all is required is a spot of JavaScript to show/hide the ListBox when you click the button:

  function toggle_visibility(id) {
    
       var e = document.getElementById(id);    
       
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }

Of course you may find that you'll have to play around with CSS to get it looking correctly and a bit of codebehind so that when the user selects from the ListBox the TextBox is updated correctly.  However what you have is essentially a working ComboBox that works in all browsers, and to be honest, what else do you need?

Those looking for a more elegant solution are either going to have to cough up and pay for the control, or move towards the AJAX solution (which will NOT work with JavaScript disabled).  Blame Microsoft :-)

EDIT:  I have found a nice free version of a ComboBox control .  However I would use with caution as it doesn't seem to work in FireFox :-S

Getting the Ajax Control Toolkit to work with Visual Studio 2005

Like many out there I have recently been problems attempting to get the Ajax Control Toolkit to work (correctly) in Visual Studio 2005.

After LOTS  of research it appears that most of the problems are caused by a mismatch of the referenced versions versions of the System.Web.Extensions namespace.  Just to clarify the errors that pop-up DO NOT usually mention this is the culprit.  For example the error the parser kept throwing at me was:

The base class includes the field 'ScriptManager1', but its type (System.Web.UI.ScriptManager) is not compatible with the type of control (System.Web.UI.ScriptManager). 

Now as you need a Script Manager on the page in order to use any of the controls in the toolkit this was really starting to get annoying.  I couldn't see anything wrong with the System.Web namespace, but as usual, I was looking in the wrong place.

To solve this problem involves the web.config file.

You need to check through this ensuring the versions for the System.Web.UI is using version 3.5.0.0 and NOT 1.0.61025.0

For example the line I had was:

<system.web>
    <pages>
      <controls>
        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      </controls>
    </pages>
</system.web>


Which should have been:

<system.web>
    <pages>
      <controls>
        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      </controls>
    </pages>
</system.web>


Which completely solved my problem.

p.s. To find the solution of this problem I edited every instance of the version in the web config, although probably not required, doesn't seem to cause any problems.