Posts from November 2008

Nov
22
2008

GridView with Dynamic Columns



Posted in ASP.NET and Programming

The ASP.NET GridView control can be a really useful control for displaying and editing tabular data, but if you try to make it work with MySQL or a cross-tab query you’re likely to run into problems.

I wanted to create a grid showing the data from a cross-tab but also allow users to edit the data. If you’ve never used one before, a cross tabulation is a query which maps repeating rows to columns instead. This means that a column in a displayed result might not actually exist as a column in a database table. If you just need to display the data and aren’t bothered about editing it, the GridView works out of the box and will automatically generate the columns for you – but what if you need to display some columns but not others?

Read more »

Nov
16
2008

Crosstab queries



Posted in Programming

I was working on ResourceBlender last week trying to find a way to get the data from multiple rows in SQL. I thought I’d have to loop through programatically and abuse Dictionaries and HashTables until I found out about a lesser known and at first confusing feature of SQL known as cross tabulation.

The data I needed contained translations. I have a table for elements and a table for translations, and each translation can be used by multiple elements.

mysql> SELECT elementname, LANGUAGE, translation, elements.translationid 
FROM elements 
	INNER JOIN translations ON translations.translationid = elements.translationid 
WHERE elements.translationid 
	BETWEEN 24 AND 25;
+-------------+----------+-------------------+---------------+
| elementname | LANGUAGE | translation       | translationid |
+-------------+----------+-------------------+---------------+
| PrevWeek    | de-DE    | Vorherige Woche   |            24 |
| PrevWeek    | en-GB    | Previous Week     |            24 |
| PrevWeek    | es-ES    | Semana anterior   |            24 |
| PrevWeek    | nl-NL    | Vorige week       |            24 |
| PrevWeek    | ro-RO    | Saptamana trecuta |            24 |
| PrevMonth   | cs-CZ    | Předchozí rok   |            25 |
| PrevMonth   | de-DE    | Vorheriger Monat  |            25 |
| PrevMonth   | el-GR    | Προηγ. Ετος|            25 |
| PrevMonth   | en-GB    | Previous Month    |            25 |
| PrevMonth   | es-ES    | Año anterior     |            25 |
| PrevMonth   | fr-FR    | Préc. An         |            25 |
| PrevMonth   | it-IT    | Anno prec.        |            25 |
| PrevMonth   | nl-NL    | Vorig jaar        |            25 |
| PrevMonth   | pl-PL    | Poprzedni rok     |            25 |
| PrevMonth   | pt-PT    | Ano anterior      |            25 |
| PrevMonth   | ro-RO    | Luna trecuta      |            25 |
| PrevMonth   | ru-RU    | Предыдущий год|            25 |
| PrevMonth   | sl-SI    | Prejšnje leto    |            25 |
| PrevMonth   | sv-SE    | Föreg. År       |            25 |
| PrevMonth   | tr-TR    | րnceki Yýl      |            25 |
| PrevMonth   | zh-Hans  | 上一年         |            25 |
+-------------+----------+-------------------+---------------+
21 rows IN SET

Read more »

Nov
9
2008

Cross page request variables in ASP.NET



Posted in ASP.NET and Programming

If you have a page which uses a master page, then put controls inside a ContentPlaceHolder on the sub page, accessing the values of the controls can be difficult if you’re posting the form to another page. The problem is, the ContentPlaceHolder mangles the control names so instead of radApplication, you get ctl00$cntMain$radApplication, meaning you can’t read them from Request.Form.

Hardcoding the name of the placeholder would be a bad idea incase you ever change the name of it or change the master page. There are actually two ways you can get at the controls.

Read more »

Nov
3
2008

Java: Matching URLs with Regex Wildcards



Posted in Programming

After someone suggested a way to match URLs and protocols with wildcards in LockCrypt, I started work implementing a URL which accepted wildcard (*) characters. The result is a class which takes a URL string as a constructor and breaks it apart into it’s component parts. The class is based on a JavaScript regex from Steve Levithan.

The full specification for the URL it constructs is protocol://user:password@host:port/direc/tory/file?query#ref. Any parts left blank are assumed to be wildcards. Performance averages out to about 0.05ms per check, not bad for a beefy regex.

Just the constructor is listed here, the full source is available at http://leghumped.com/WildcardURL.java.

Read more »