Thursday 14 February 2013

New toy - Raspberry Pi

I'm reviving this blog as a place to store things I find out in trying to get a Raspberry Pi to do various things...

Wednesday 18 August 2010

Failed to create listen socket on port 21

Trying to set up an FTP server, Filezilla gave the following error message

Failed to create listen socket on port 21
Failed to create a listen socket on any of the specified ports. Server is not online!

From command prompt run:

netstat -abn > c:\test.txt

In the resulting text file look for a line for 0.0.0.0:21. I found that it was being used by the process inetinfo.exe which is part of IIS. Going to "Services" found FTP Publishing Service was running so disabled and restarted Filezilla FTP server.

Thursday 25 February 2010

Remove NULL values from SQL data result in Excel

If you want to plot a numerical data set that's come from an SQL query, you want to remove the NULL values so they plot as gaps in the series rather than Excel connecting the line between data points...

Select the column.
Press F5 to bring up the "Go To" dialog.
Select "Special..."
Select Constants and then uncheck everything except "Text".
OK twice and then press delete and all your NULLs are gone.

Thursday 10 December 2009

Adding an ASP.Net User Control dynamically in code behind

I was having problems with creating and adding instances of a user control to a page from the code behind eg

WebUserControl wuc = new WebUserControl();
wuc.ContentText = "Add this text to my control's custom ContextText property";
PlaceHolder1.Controls.Add(wuc);


I had the control registered on the page:

<%@ Register TagPrefix="jon" Src="~/WebUserControl.ascx" TagName="WebUserControl" %>


My user control just had to put the ContextText in a Label...


public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = this.ContentText;
}


public string ContentText { get; set; }
}


This didn't work and failed with a null reference exception "Object reference not set to an instance of an object" on my Label control.

The solution is in the way the user control is created. If you change the code above to...

WebUserControl wuc = (WebUserControl)LoadControl("~/WebUserControl.ascx");
wuc.ContentText = "This one is added dynamically";
PlaceHolder1.Controls.Add(wuc);

...suddenly it all works fine.

I don't understand this. I expect it's to do with the page life-cycle and the control not being created properly with the first version of the code.

Monday 29 June 2009

How to do ASP.NET Localization manually

In the "<%@ Page" declaration at the top of the aspx page add UICulture="auto" and Culture="auto".

Add a "meta:resourcekey" attribute to any element that needs localizing

eg...
<asp:label id="MyLabel" runat="server" text="Default Label Text" meta:resourcekey="MyLabelResource" />

Add an App_LocalResources folder to the solution. There seems to be some disagreement over whether and when to use App_LocalResources or App_GlobalResources. I think local unless it's something that'll be repeated on a lot of pages.

Add a new resource file to App_LocalResources folder named like "nameofmypage.aspx.resx" - this is the default language file.

Entries are then things like MyLabelResource.Text etc...

Other language resource files are created with names like "nameofmypage.aspx.fr.resx" for French or if specific to a particular region of a language "nameofmypage.aspx.en-US.resx" for US English and "nameofmypage.aspx.en-GB.resx" for Proper English etc.

That's all for aspx files. To retrieve resources programatically in C# is as simple as:

Label1.Text = GetLocalResourceObject("Label1Resource.Text").ToString();

Testing in IE, go to Tools > Options > Languages and add the appropriate entry and then move it to the top and refresh.


Globals

Globals work a little bit differently. In App_GlobalResources add a resource file eg globals.resx then to retrieve value use:

Label1.Text = GetGlobalResourceObject("globals", "myGlobalResourceString").ToString();

or in the page:

<asp:label id="MyLabel" runat="server" text="<%$ Resources:globals, myGlobalResourceString %>" />

Note the two arguments where the first is the name of the resource file (globals).


From .cs files

To get a resource programatically from within a .cs code file you just need to append HttpContext to the front of the above methods

eg HttpContext.GetGlobalResourceObject("globals", "myGlobalResourceString").ToString()


To use the current culture to format a date into the current language use:
date.ToString(System.Globalization.CultureInfo.CurrentCulture)

Wednesday 17 December 2008

XPath in C#

string url = "http://www.example.com/somedata.xml";

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream());

string xmlstr = sr.ReadToEnd();

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlstr);

XmlNode root = doc.DocumentElement;

// Get a single node
XmlNode tnode = root.SelectSingleNode("/FEED/SOURCE");

// Get all nodes that match a certain XPath expression
XmlNodeList alltitles = root.SelectNodes("/BOOK/TITLE");

There is a useful table of XPath syntax on this page.

Thursday 11 December 2008

BBC iPlayer is stealing my bandwidth (and slowing down my computer)

If you're a BBC iPlayer user and like to download programmes to your computer rather than streaming them, you will have downloaded the BBC iPlayer Download Manager. What the BBC didn't tell you (or not very clearly, at least) is that when you install the Download Manager, it creates a Windows Service running in the background on your PC which is constantly making use of your internet connection (even when you don't have the Download Manager running) to upload chunks of the programmes you've got downloaded to your library to other iPlayer users.
Don't panic, this is not neccessarily a bad thing - in fact when you downloaded those programmes you did so from other users who were uploading them through the Windows Service running on their PCs - but a few weeks ago my computer started running slower than normal and I was finding that streaming iPlayer videos would stop and re-buffer every few seconds. With a quick Google search I found that it could be caused by this background behaviour of the iPlayer software. There were a few suggestions for how to stop this invisible uploading but the correct method is to do the following:

Open the Windows Services Manager by going to Start > Run... (or Start > Search box on Vista) and typing services.msc and pressing Return.
Scroll down until you find KService.exe. Right-click on it and click Stop. When it has stopped, right-click on it again and select Properties. In the dialogue that appears change the Startup Type drop-down menu to Manual or Disabled (Automatic means it will start again next time you reboot which we don't want).

That's all there is to it. Clearly if everyone does this, the peer to peer system that allows you to download iPlayer programmes won't work as there'll lots of downloaders but no uploaders but it's worth knowing if, like me, the iPlayer Download Manager starts causing you problems.