C# Snippets

I am not a fan of C# really, however after being forced to use it for 1/2 of my life for the past 6 months or so, I feel have a pretty good grasp of some of the language.

There are a few snippets I have written which are possibly useful for some people;

  • Zooming, Panning, Rotating images
  • Custom alert boxes
  • Threading / Background tasks
  • Image resizing
  • Zip file open/closing
  • Dynamic form population
  • Simple XML parsing – I use this to convert between county names & country codes
  • Custom ini handler – I havn’t done this yet, but its VERY likely.
  • Dynamic DLL includes – This is currently being used to allow custom forms within my application

I might not get a chance to do all of those, but I hope to.  I’ll also link to them when they are done so feel free to bookmark this page.

There may also be some “things you may not have known” section appearing.  It is surprising what you come across when you’ve spent time in other languages for a while.

Update 16/08/10 – I was hoping to have put some of this online by now, however I am off on holiday in 2 days and have been putting together FAQ’s for my colleagues & finishing off as much as I can so that I don’t get a call while I’m away.  I’ll be posting this stuff at the beginning of September instead.

PostgreSQL, how about you don’t make migration more complex

I am currently work on building a new site, from an existing one.  A lot of the front-end work is done, now to a lot of the admin.

Today a really quite annoying “feature” of PostgreSQL raises its head and takes up 3 hours of my time.  A lot of the data from the old site PHP/MySQL has been migrated to the new one Django/PostgreSQL which all worked fine, the front end works and its all lovely.  To maintain the old database id’s we inserted them all into the new database, this in itself is not the problem.  The problem arose what starting to adding new rows, which are expected to go to the end.

INSERT INTO table (title) VALUES (‘my_title’);

Instead of actually insert into a new row, this started working from id 1, throwing up errors whenever the id already existed and sometimes started to delete items from the end of the database.

Just what the hell.  After plenty of research and testing, initially expecting it to be a problem with my code and the problem is found.

Adding the Primary Key manually DOES NOT update the auto-increment value, and here is the fix (change field names where necessary)

SELECT setval(‘”table_id_seq”‘, coalesce(max(“id”), 1), max(“id”) IS NOT null) FROM “table”;

If you are using Django  you can generate the SQL by running, it does not run the automatically but you can pipe the response directly into the psql tool.

python manage.py sqlsequencereset appname

Very simple but this does not follow the MySQL style I am used to, then again SQLite also does things differently which had me stumped at first.

Argh….The Cloud

Cloud/VPS hosting is great, except when its not.  The idea is, you have dedicated hardware to take over if something fails and on smaller installations, is quite a bit cheaper.

I have been using it for a over 12 months and for the most part its been fine. There have been a few occasions where there has been a bit of downtime, but it was early days…..except today there has been some quite serious downtime.

A power outage takes down a large chunk of machines, both primary and backup machines.  Not much of a problem I hear you say…WRONG.

A conventional server

If a power outage takes out a datacenter, once its back remote start, manually pressing up to 42 buttons per rack boots servers, or servers that just reboot when power is back (this will be most I’d guess), a couple of minutes later everyone’s server us running with minimal issue.

The Cloud

When a VPS cloud is affected the process is very different.  The main servers are booted, the disk servers are booted at which point not a single “client” server has started.  Now we go through the large queue of servers starting them, good luck if your at the end, up to 4 hours it seems to recover from this one.

Is the Cloud worth it?

This is a difficult question to answer.  The cloud can handle multiple failures at the same time (or at least its meant to) however due to the many number of components, the chance of something failing is far greater.  Having your own machine, if your down due to a hardware fault…that’s it..your down.

I still like the Cloud scenario, almost instant deployment of new servers & more CPU than you are often going to get in a standalone box.  It really does depend on what your system requirements are.  I’m hosting a game server in the cloud.  It would cost me the same, if not more, to just get a game server from a game server provider – without the flexibility I have now, but a lot more to host 1 game server on the cheapest dedicated server you could fine.

Some of the 3 website “breaking” due to the iPhone

As has happened already this year, the iPhone 4 is breaking websites.  Three have not managed to escape this.

An SMS & E-mail was sent out recently to several/all? of the “register interest” folk which has in turn caused a lot of traffic to the main 3 website.  3 have done a very good job of separating parts of their site so a lot still works without problem.

O2 & Orange have dedicated websites, on different domains just to handle the iPhone, this should mean they have kept downtime to a minimum.  O2 have also announced no online ordering until at least the end of July.

This is a very good example of what to do when expect BIG product releases.

  • Static websites perform very well, so remove the PHP
  • Setup a sub-domain with a site dedicated to this release
  • Plan ahead. Similar to 3, get different elements of your sites separated permanently, worst case scenario, only a small part goes down.

Using the 960 Grid System

Originally, the tutorials mentioned here, were going to include the 960 grid system.  After some thought weighing up the benefits on convenience, performance, future updates etc, I have decided this will not be used for the main site.

I will still do tutorials on designing & prototyping using the grid system but that is as far as I will go.

So what’s wrong with the grid system?

As a developer who is trying to build a site that performs well, write tutorials from real experience and allow myself to easily apply updates in the future things have had to change.

  • The grid system has a lot of CSS I will not personally be using in this site.  Granted I COULD remove this, but I could also create what I need myself.
  • Future updates…well I intend to actually improve the site as time progresses.  The design will evolve as more content in introduced to the site, 960 may not even be wide enough, however, the problem comes when needing to adjust column widths.  Using CSS on its own, you adjust the width of the 2 columns and your down.  Using the grid system, you need to go through the HTML adjust class names.
  • Without using the grid system I am also able to create another tutorial for site layouts.  The grid system is fairly self-explanatory and will be covered on the prototyping stage.
  • Fluid or expanded columns.  By default the 960 grid system does not support fluid layouts, there is a branch for fluid and elastic layouts, but at this stage I am not entirely sure how or what I want to achieve with this.

Dansette