Friday, 9 November 2007

Lateral parking solution for women everywhere

There are a lot of people in the world having problem with lateral parking. It is not only difficult to manage when you have nothing to go on but the little tiny images in the car mirrors, but stressful as well, as normally the procedure is done on a crowded street with drivers behind you urging you to do it faster.

Not a problem anymore! The Chinese have found the perfect way of parking in under 5 seconds! Here is a video tutorial on how to do it:



Here is an alternative solution.

Sunday, 4 November 2007

Fruit of the mall - part 2

Two months ago I wrote a post about the exotic fruits I found in a hypermarket in Sibiu, now it's time for additions, taken from the Bucharest Metro.

My own photo of the fruits
So here are the fruits I bought today:
the Maracuya
. It has the same feel as a fruit from my first post, the Kiwano. The taste, though, is very sour, like a lemon, and a little spicy. I could eat it, but I think people don't really eat it raw. The outer skin is hard, inedible and thick.
the Kaki fruit. It is very tasty, although it has a peary texture that I didn't quite like. It is a sweet banana tasting fruit.
the Cactus fruit has a sweet coating around the hard inedible seeds inside with a texture of baby food and taste like a not aromatic cantaloupe, similar to the Pepino. The seeds, though, make it less than pleasant to eat.
the Pepino mellon has a similar texture to the Kaki, but it has the taste of Cantaloupe, yet not so aromatic.
Now, the Papaya is an interesting tasting fruit. I am afraid my best approximation is still the cantaloupe, but the papaya also has its aromatic properties and the texture. Its aroma, though, is slightly different, more like banana. It has a big core of inedible seeds and the skin is also inedible. That makes the useful part of the fruit rather small.

In conclusion, one must definitely try the papaya and the kaki. The maracuya is the weirdest taste among all, not entirely pleasant, although I can try to eat it with sugar or something. The net suggests mixing the seedy content with water and sugar after letting the skin wrinkle.

Fried Calamari - simple and damn tasty!

I went to one of the Metro hypermarkets in Bucharest and I was pleasantly surprised by the variety of stuff one can find there, as compared with other hypermarkets that seem to be supplied from the same sources. Also check my next entry, I will write about exotic fruits there.

Calamari in a plate. My own picture.In this post I will talk about calamari! There is an entire store dedicated to fish in Metro, filled with a lot of nice looking and/or packaged treasures of the sea. My picks were swordfish stakes and one big calamari. Well, not that big... it's no architeuthis, but it will do.

I went home, made a longitudinal incision, threw away the awkward looking organ inside (which I suspect had a digestive function) and the eyes and beak, then threw it in boiling butter after putting a bit of spice over it. I removed it after 2-3 minutes and ate it. Yes, it's that simple! The taste is not strong, but really special and it was totally worth the buy.

Warning, as read from the googling on calamari: Calamari is either to be cooked in 2-3 minutes or in more than 30. Everything in between turns it to rubber. So, if you are like my wife and you want to spit it after you taste it, you might want to try the long cooking calamari recipes out there. :)

Friday, 2 November 2007

Getting the index or rank of rows in SQL Server 2005

A while ago I wrote a little post about changing a paging GridView in order to show certain number of pages and page index, but extract from the database only the data you needed. I was looking there for something in SQL Server like the MySql LIMIT, the T-Sql equivalent of TOP, but which accepts two parameters instead of only one.

I just found out that there is a way of doing this in Sql Server 2005 by employing functions that return the position of a row, depending on a certain ordering and partition, called Ranking Functions. From the little googling I did, it seems that Microsoft borrowed this technique from Oracle, but one never knows, maybe it was the other way around.

I will write a short example on ranking functions, then link to the appropiate articles. Given this SQL query:
select *,
Row_Number() OVER (ORDER BY name) as [Index],
Rank() OVER (ORDER BY name) as Rank,
Dense_Rank() OVER (ORDER BY name) as DenseRank,
NTile(3) OVER (ORDER BY name) as NTile,
Row_Number() OVER (PARTITION BY nr ORDER BY name) as IndexNr,
Rank() OVER (PARTITION BY nr ORDER BY name) as RankNr,
Dense_Rank() OVER (PARTITION BY nr ORDER BY name) as DenseRankNr,
NTile(3) OVER (PARTITION BY nr ORDER BY name) as NTileNr
from test
ORDER BY ID

you get the following result:
ID Name Nr Nr2 Index Rank DRank Ntile IndexNr RankNr DRankNr NtileNr
1 Mark 1 7 8 7 4 2 2 2 2 2
2 Mike 1 4 11 11 6 3 3 3 3 3
3 John 2 8 5 5 3 2 1 1 1 1
4 Charles 3 2 1 1 1 1 1 1 1 1
5 Ellen 3 6 4 3 2 1 2 2 2 2
6 Mary 4 1 9 9 5 3 2 2 2 2
7 Mark 4 17 7 7 4 2 1 1 1 1
8 Mike 2 41 12 11 6 3 2 2 2 2
9 John 6 83 6 5 3 2 1 1 1 1
10 Charles 1 72 2 1 1 1 1 1 1 1
11 Ellen 0 68 3 3 2 1 1 1 1 1
12 Mary 3 21 10 9 5 3 3 3 3 3


As you can see, Row_Number returns the row index, Rank returns the rank, Dense_Rank returns consecutive rank (no gaps between rank numbers) while NTile puts each row in a category using a given number of total categories. Partition by makes the operations work for each distinct value of a certain column, in this case nr. If the partition would have been on nr2, all the ranking values would have equaled 1, since there are only distinct values on the nr2 column. The Over clause can be used on more than just ranking functions; it also works on Aggregate functions. Yummy!

Links:
Ranking Functions (Transact-SQL)
OVER Clause (Transact-SQL)
Aggregate Functions (Transact-SQL)
This article also shows a similar method in Sql Server 2000 of which I knew nothing until today: Row_Number() function in SQL Server 2005
Returning Ranked Results with Microsoft SQL Server 2005

Thursday, 1 November 2007

Enabling GZip compression in IIS 5 (Win2000 and not XP) for ASP.Net files

Maybe it works for other IIS versions as well, but I certainly was looking for a way of turning it on on our Windows 2000 development/test computer. So this is the long story:
HOW TO: Enable ASPX Compression in IIS

and this is the short one:
Step 1: backup your site metabase
Go to the Internet Information Services (IIS) tab and right click on it, go to All Tasks, choose Backup/Restore Configuration and save it.

Step 2: make the change to the metabase
Create a .bat file that has the following content:
net stop iisadmin
cd C:\InetPub\adminscripts
CSCRIPT.EXE ADSUTIL.VBS SET W3Svc/Filters/Compression/GZIP/HcScriptFileExtensions "asp" "dll" "exe" "aspx"
CSCRIPT.EXE ADSUTIL.VBS SET W3Svc/Filters/Compression/DEFLATE/HcScriptFileExtensions "asp" "dll" "exe" "aspx"
net start w3svc


Make sure to restart the SMTP service or any others that were stopped by the bat. I don't know how to start it from the command line and I pretty much don't care. The batch file will notify you of possible services it will shut down, but will restart in the end only the Web service.

The performance is immediately visible and it also works with Ajax.

Update:
This article was originally talking about Windows XP. Thanks to McHilarant (see comment below) I realized that, even if the changes in the metabase are possible on any IIS5 (Windows XP and Windows 2000), the actual compression will not be possible on XP. I remembered then that the actual modification that I did that time was not on my dev machine, but on our office server, therefore I updated the post accordingly.

Another Update:
Here is a link about a script to enable IIS 6 gzip compression: Script to Enable HTTP Compression (Gzip/Deflate) in IIS 6.

Speed up Javascript

I was looking for ways of speeding up my web pages, considering that one utility page I built reached 500Kb and from that, only 170kb where actually content, the rest was Javascript. Bad Javascript, I accept, but it works, ya know?

So, after thinking and thinking I thought of a novel approach to this: how about googling for it? and I found this very nice and comprehensive article: Speed Up Your Javascript Load Time.

There isn't much else I can say about it.

Friday, 26 October 2007

Using Asynchronous Javascript with validators

Before Asp.Net Ajax was released I used to work with a library called AjaxPro (formerly Ajax.Net). It also wrapped specially decorated methods and sent them to the Page as javascript functions with a callback javascript function as the last parameter. In this library, by not using a callback you were saying that you want to execute the function synchronously, that you want to wait for a result. After all, that's a functionality included in the XmlHttpRequest object.

I was amazed to see that Asp.Net Ajax does not offer such an option. You NEED a callback function. So... how do you use a WebMethod as a javascript validation function, since the validators only accept a synchronous function?

The first idea I had was the easy way, to cycle inside the function until I had a result. Bad idea: Javascript itself is not really multitask, so it just took the CPU to 100% and that was the end of it: kill task. The second idea was the hard way: redo all code in the XmlHttpRequest wrapper and use the included synchronous functionality. Too complicated, although I found an article about how to do that. So here is the way I did it:
1. Get the validator object
2. Set a property on it to tell if the validator is inside an async call or not
3. Set a property to hold the last value that was validated
4. Set a property to hold the last result returned
5. Set a method of the validator as the call back function
6. If is in async return false
7. If the last value is the same with the current validating value, return last result
8. Execute the async validation function with the validator callback method as a parameter and return false (or "in async")

Now, the callback method just sets the result and clears the async flag and also updates the visual of the validator. By doing this I essentially don't allow postbacks during validation calls, which makes sense. I also had an idea of creating a special "in async" validator that would not allow postback and would display something nice, but the solution I am explaining keeps it local, with ValidationGroups and everything working just fine.

I will show the code in a few seconds, but first be warned that I am using a validator made by myself which expects null or true as valid and a string or false as invalid. If the result is a string, it displays it as the ErrorMessage. So, on with the code:
The function to get a reference to the validator (which in some cases, especially Ajax, is not the same as the span with the same id)
function getValidator(id)
{
if (typeof(Page_Validators)=='object')
for (c=0; c<Page_Validators.length; c++)
if (Page_Validators[c].id==id) return Page_Validators[c];
return false;
}


The preparing function, which can be reused for more validators. I couldn't find a better way to encapsulate this behaviour yet, please feel free to enlighten me with ideas
// parameters are the value to be validated 
// and the validator object
function prepareAsync(value,validator) {
if (!validator) {
validator.doReturn=true;
return 'Error in prepareAsync: validator not found!';
}
// it's validating
if (validator.inAsync) {
validator.doReturn=true;
return 'In async validation';
}
// it has validated this value before
if (validator.prevValue==value) {
validator.doReturn=true;
return validator.result;
}
// create the callBack function as a method
// of the validator object
if (!validator.callBack)
validator.callBack=function(res) {
validator.inAsync=false;
validator.result=res;
if (res==null) {
validator.isvalid=true;
} else
if (typeof(res)=='boolean') {
validator.isvalid=res;
} else {
// setting the validation message is
// as simple as changing the innerHTML
// of the validator span
validator.innerHTML=res;
validator.isvalid=false;
}
// that's an ASP.Net validator function
// since .NET 1.0 upwards
ValidatorUpdateDisplay(validator);
}
validator.inAsync=true;
validator.prevValue=value;
validator.doReturn=false;
}


and now a sample validation function:
// parameters are the value to be validated
// and the id of the validator
function ValidateProdotto(value,id) {
// get the validator object
var validator=getValidator(id);
// reuse this function to all validators in the page
var res=prepareAsync(value,validator);
// doReturn says if it has a result for us
if (validator.doReturn) return res;
// perform the actual validation
PageMethods.ValidateProdotto(value,validator.callBack);
// and return false, since we are in async now
return false;
}


Hope this helps somebody. This applies to short validations, but feel free to enhance my design with cancellations of running async operations in case the value is changed or with an array caching all the values tried and validated.