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.
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.
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
I had the control registered on the page:
My user control just had to put the ContextText in a Label...
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...
...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.
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)
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.
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.
Wednesday, 26 November 2008
Blank map markers for Google Earth

http://maps.google.com/mapfiles/kml/paddle/red-blank.png

http://maps.google.com/mapfiles/kml/paddle/ylw-blank.png

http://maps.google.com/mapfiles/kml/paddle/blu-blank.png

http://maps.google.com/mapfiles/kml/paddle/orange-blank.png

http://maps.google.com/mapfiles/kml/paddle/grn-blank.png

http://maps.google.com/mapfiles/kml/paddle/purple-blank.png

http://maps.google.com/mapfiles/kml/paddle/wht-blank.png
Best method to read a web page into a string in C#
There are a number of ways to get the contents of a web page (or xml document such as an RSS feed) into a string in C#.
Unless someone can show me an even shorter method, I think the least lines of code way to do this is using the WebClient class in the System.Net namespace as follows:
Job done!
Note that this is much shorter than the method you're most likely to find on Google which is to build a web request then process the resulting response stream into a string using a StreamReader as in the below example. This works but is a lot more lines of code and creation of unnecessary objects.
Unless someone can show me an even shorter method, I think the least lines of code way to do this is using the WebClient class in the System.Net namespace as follows:
WebClient client = new WebClient();
string pageContents = client.DownloadString(url);Job done!
Note that this is much shorter than the method you're most likely to find on Google which is to build a web request then process the resulting response stream into a string using a StreamReader as in the below example. This works but is a lot more lines of code and creation of unnecessary objects.
using System;
using System.IO;
using System.Net;
using System.Text;
public static void GetFile(string strURL, string strFilePath)
{
WebRequest myWebRequest = WebRequest.Create(strURL);
WebResponse myWebResponse = myWebRequest.GetResponse();
Stream ReceiveStream = myWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream =
new StreamReader( ReceiveStream, encode );
string strResponse = readStream.ReadToEnd();
readStream.Close();
myWebResponse.Close();
}
Wednesday, 19 November 2008
Samsung Omnia firmware update for UK Vodafone users
After much complaining from Omnia owners on the Vodafone support forum (many people were stuck on the HH3 "battery killer" firmware), Vodafone have finally released a firmware update (through Samsung) for the Omnia (i900) to bring it up to date with the updates that Samsung have been releasing.
The new firmware (version HJ) is officially approved by Vodafone so no worry about voiding your Vodafone warranty (installing Samsung firmwares that are not approved by Vodafone will result in them abandoning you) and can be downloaded from here.
Cannot resolve the collation conflict
Joining two tables on a name column...
select * from tableOne as a
join tableTwo as b
on
a.name = b.name...came up with this error...Msg 468, Level 16, State 9, Line 1Solution was to change the collation of the appropriate column:
Cannot resolve the collation conflict between "Latin1_General_CI_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
select * from tableOne as a
join tableTwo as b
on
a.name COLLATE Latin1_General_CI_AS = b.name
The message(s) could not be sent - Windows Mobile 6.1 SMTP email bug
I came up against this message yesterday when trying to send an email through gmail from my Samsung Omnia:
There are unofficial fixes for this around on the web to avoid having to re-setup the email account but Microsoft have released an official patch which just involves downloading a .cab file from here and installing it on your mobile.
The message(s) could not be sent. Check that you have network coverage and that your account information is correct. Then try sending again.Sending email had worked before but now didn't. I checked all the smtp settings and they were all correct. A bit of web-hunting found that this is a problem with all Windows Mobile 6.1 devices - if at any time a connection to the smtp server fails for some reason, the email account gets corrupted and will no longer send email. The account would then have to be deleted from the device and set up again from scratch.
There are unofficial fixes for this around on the web to avoid having to re-setup the email account but Microsoft have released an official patch which just involves downloading a .cab file from here and installing it on your mobile.
Tuesday, 18 November 2008
The endpoint you entered was not correct
Solution to Flickr problem with linking to WordPress using xmlrpc.php file.
I was trying to set up my Flickr account to automatically upload to a WordPress blog but having enabled XML-RPC in the WordPress control panel, Flickr kept giving the error "The endpoint you entered was not correct". Navigating to the xmlrpc.php file (http://yourblogpath/xmlrpc.php) in a browser returned a 403 Forbidden error whereas it should show a page saying "XML-RPC server accepts POST requests only".
Googling around suggested a number of potential solutions such as trying again until it works and trying in Internet Explorer (rather than Firefox) but the one that fixed it was to add the following code to the .htaccess file in the blog root and then try again:
(Final solution found here)
I was trying to set up my Flickr account to automatically upload to a WordPress blog but having enabled XML-RPC in the WordPress control panel, Flickr kept giving the error "The endpoint you entered was not correct". Navigating to the xmlrpc.php file (http://yourblogpath/xmlrpc.php) in a browser returned a 403 Forbidden error whereas it should show a page saying "XML-RPC server accepts POST requests only".
Googling around suggested a number of potential solutions such as trying again until it works and trying in Internet Explorer (rather than Firefox) but the one that fixed it was to add the following code to the .htaccess file in the blog root and then try again:
<files>
SecFilterInheritance Off
</files>
(Final solution found here)
Resolve the branch for all nodes in the node tree
SQL to resolve the path from the root to the particular node in both node name and node id form.
(Demonstrates cursors, converting int to string, concatenating strings and selecting into variables)
(Demonstrates cursors, converting int to string, concatenating strings and selecting into variables)
drop table NodePaths
create table NodePaths
(Id int,
NamePath varchar(1000),
IdPath char(500)
)
declare @thisname as varchar(200)
declare @fullpath as varchar(1000)
declare @idpath as varchar(500)
declare @startid as int, @pnid as int, @currentid as int
declare @thisid as varchar(10)
declare c_1 cursor for
select id from node
open c_1
fetch c_1 into @currentid
while @@fetch_status = 0
begin
set @startid = @currentid
select @pnid = parentnodeid, @fullpath=[name], @idpath = id from node where id = @startid
while @pnid <> -1
begin
select @thisname = [name], @thisid = CONVERT(varchar(10),id), @pnid = parentnodeid from node where id = @pnid
set @fullpath = @thisname + ' > ' + @fullpath
set @idpath = @thisid + ' > ' + @idpath
end
insert into NodePaths
select @startid,@fullpath,@idpath
fetch c_1 into @currentid
end
close c_1
deallocate c_1
Wednesday, 12 November 2008
Transfer SQL table to different schema (eg dbo)
The SQL command to assign a table to a different schema is:
This can therefore be used to assign tables created under a particular user to dbo (which can be necessary as sometimes stored procedures may fail with "Invalid object name" if tables are not under dbo) as follows:
ALTER SCHEMA newSchemaName TRANSFER [OldSchemaName].MyTableThis can therefore be used to assign tables created under a particular user to dbo (which can be necessary as sometimes stored procedures may fail with "Invalid object name" if tables are not under dbo) as follows:
ALTER SCHEMA dbo TRANSFER [OldSchemaName].MyTable
Matlab fatal error on startup
Had the situation where Matlab fails as soon as you open it and posts a very unhelpful message - "Fatal error on startup" with no useful extra information.
A hunt on Google suggested renaming the folder with the matlab.prf preferences file in it (see matlab support here) so that Matlab creates a new one on startup. That didn't help.
Next thing Google suggested was killing the spoolsv.exe (windows printer spooling service) process in task manager. This allowed Matlab to be opened successfully but I don't want to have to choose between Matlab and printing!
Further investigation found that changing the default printer to a different one on the network allowed Matlab to be opened once spoolsv.exe had come back. Assume that Matlab tries to connect to the default printer on startup but the original default printer was unreachable so Matlab just falls over. Problem is solved by simply changing to a different printer.
A hunt on Google suggested renaming the folder with the matlab.prf preferences file in it (see matlab support here) so that Matlab creates a new one on startup. That didn't help.
Next thing Google suggested was killing the spoolsv.exe (windows printer spooling service) process in task manager. This allowed Matlab to be opened successfully but I don't want to have to choose between Matlab and printing!
Further investigation found that changing the default printer to a different one on the network allowed Matlab to be opened once spoolsv.exe had come back. Assume that Matlab tries to connect to the default printer on startup but the original default printer was unreachable so Matlab just falls over. Problem is solved by simply changing to a different printer.
Subscribe to:
Posts (Atom)