Thursday, August 23, 2007

XML & XPath Expression

When we have an application which is having XML Files as the data storage and need the fastest way to only read the data, XPath will be a good solution.

How to loop the data from an xml into XPath is quite simple.
I will be using .Net code below to do this.

XPathDocument xpDoc = new XPathDocument(@"C:\Temp\data.xml");
XPathNavigator xpNav = xpDoc.CreateNavigator();
XPathNodeIterator xpIter = xpNav.Select("/root/row");
while (xpIter.MoveNext())
{
Console.WriteLine(xpIter.Current.GetAttribute("id", string.Empty));
}


The code will simply load the data.xml and then loop the data and write the ID to the console.

There will be occasions where we need to perform more complex queries to the data which we can do it quite simple such as in the SQL statement below:

Select * From data

Where colA In ('AA', 'ZZ')
And colB = 'X'
And colC Like '%123%'


We can achieve the same result with XPath by using this expression below :

XPathNodeIterator xpIter = xpNav.Select("/root/row[(@colA = 'AA' or @colA = 'ZZ') and @colB = 'X' and contains(@colC, '123')]");

There is a lot of ways that we can use in XPath, you can find the complete references in http://www.w3.org/TR/xpath

XML is always an interesting type of data that we can use, there is a lot of database technologies nowadays which has actually supports XML type of data such as SQL2005 where I heard we can set the indexes in the XML type of field ;)

Unicode content in Cookies

This is another small small things but can get to the nerves ;)

So basically, I was doing some changes with a Taiwan web application, previously it was using Session to store some information and right now because of we're integrating with other application, we need to use the cookie instead so the information can be shared.
Note: When using cookies, if the information is confidential, you may need to consider encryption.

After i modified the application and tested it in one of the server, everything is working perfectly, until I tried it in the other server, just to make sure that this is working fine as well. And the unexpected actually happened, the unicode characters were corrupted, they are becoming question marks (????).
Firstly I thought that the server probably missing some language pack, and then I installed the language pack to the server and turned out, it was not helping either. Then I tried this application into another server again (it is fun to have a lot of servers to play and test with hohohoho), and some of them were able to save the unicode correctly and some of them didn't, including my own PC :(

I was kept thinking, what is the difference between these machines, why some of them are working and some of them aren't. I looked into the cookie file in the my local machine, the cookies which were saved correctly are having UTF-8 as the encoding and the cookies which are corrupted were having ANSI as the encoding.

So in the end, actually instead of thinking why UTF-8 and ANSI, there is a simple solution.
Use HttpUtility.UrlEncode when saving cookie, and use HttpUtility.UrlDecode when reading back from the cookie.
Using this way, we don't need to worry whether the characters will be corrupted because of some machine settings :)

Sounds easy rite? hhhmm... it took me almost 2 days browsing and browsing and then have this solution come to my mind. Getting older I guess hahaha ;)

Thursday, July 12, 2007

ORM - SubSonic rocks

It's been a while since my last post, just delivered a project last week and finally got time to browse around again hahaha :)

As usual, if i need to update my self with the latest news, i will go to www.asp.net and find my self like living in the rock age again ;)

And as usual as well, we can find very interesting things in Scott Guthrie's Blog (http://weblogs.asp.net/scottgu)

I've been interested lately with the ORM tools, such as nHibernate, ActiveRecords (Built on top of nHibernate). Since previously I used a port of JAVA Persist ORM, it really helped in the development phase since I don't have to do all the DAL classes, Stored Procedures, etc. It generate the code for us based on the provided xml schema (We need to provide this with the relationship between objects).

This time, I found a very interesting tool, called SubSonic.
You can find the website of the tool in
http://www.codeplex.com/actionpack (Old one)
http://subsonicproject.com/ (Latest one)

You can download the tool and see the SubSonic walkthrough pages.

This one is the one that i like most, because this will guide you for the ASP.Net Web Application Project in detail : http://www.wekeroad.com/sonic1.htm

I tested this northwind sample walkthrough and whoa man, I can generate the DAL classes and implement in the web page less than 5 minutes :D (see how lazy I am hohohohoho).

There's a lot of nice features in there, such as build customized query, etc. If you like to try a nice tool, I would recommend this one since it is very straight forward :)

One thing that is very important in the ORM tool is the relationship between the objects, I haven't tested on this one, but I think it should be there or else it will only be ORM without an R in the middle :P

Have fun :)

Sunday, April 22, 2007

Find application with debug set to true using WinDbg

Actually I just found out about this about always set the debug configuration to false several months ago.
At that time we were having memory problems with our servers, asp.net worker process (aspnet_wp.exe) recycles very frequently (several times / day). Because our servers have a lots of applications, probably around 100+ applications (asp legacy + asp.net), and from our team, we only manage 2 applications at the servers (and very little information about who's the owner of the other applications), we decided to ask for M... (you know who) Support Team (MST) for help.
So we collected several dump files and sent them to the MST. The first advice we received from them was to set all the debug switch to false in the web.config for all applications, and they sent the list to us. Actually at that time, I was thinking, hhhmmm... is this possible? how can this relate to the memory problem? but in the end we followed the suggestion and we modified several hundreds of web.config files in the servers :P

For more information about this debug=true setting will cause, you can find in the Scott Guthrie's Blog : http://weblogs.asp.net/scottgu/archive/2006/04/11/Don_1920_t-run-production-ASP.NET-Applications-with-debug_3D001D20_true_1D20_-enabled.aspx

When i received the list of the applications which have debug set to true, i was kind of interested how they can get this information from the dump files, whether there's a command line for this, or they already have a script to run with the dump files. So i browsed and browsed the internet about this, and gradually i know a little bit about how to get process the dump files. But very unfortunate, i was not able to find out how to get the information about debug=true at that time, until several days ago where we got another server memory problem, so i have a chance a play around with the windbg again hehehe :P

So the steps are quite easy :
1. Load the dump files with windbg
2. Load the sos.dll (.load clr10\sos.dll)
3. Execute !FindDebugTrue
Hopla, it will list all the applications with debug set to true, quite simple rite :P
you can find more help information with !help command

Note : For a memory leak problem, there's a useful tool named Debug Diagnostic Tool from Microsoft, you can find more information in here : http://blogs.msdn.com/debugdiag/

Addition at 20071010 : You can find very good information regarding to the .Net memory debugging in Tess's Blog site (http://blogs.msdn.com/tess/default.aspx)

Sunday, April 15, 2007

.Net Reflector by Lutz Roeder

Recently, I was assigned to implement and test a tool in our company.
I was given only the application output (aspx, dll, no source code), and turned out the output has also been pre-compiled, so that means we were not even able to look at the aspx (all compiled to assemblies by .net).
The purpose of the pre-compilation is mainly for performance, but it is quite useful also when u don't want the client to modify anything to the application, even to the aspx content :P
So at that time, i only have the application output and the database file (luckly they didn't encrypt the stored procedures :P)
Of course, when doing the setup and deployment to our environment, i found a lot of things which are not being covered in the document, such as "what will happen if i click on this button", etc. They were all like in a black box, which i am as a software programmer, always want to know what is the exact process flow detail, so i will be able to answer with confident if there's any question to the application.
This is the time where the Reflector becomes very handy :P
So with the great help of the reflector, i was able to see the logic of the application and find the root cause when i was stuck with the deployment.
I remembered one time several months ago, when i have a small modification which i have to make to a tool, so i was given the source code at that time. But when i finished the modification and deployed to the production, the tool was not working anymore, whereas i was quite sure that the small modification will not cause any failure to the system. So at that time, i did a dummy debug to the application, i put console.writeline every step in the code, until i found that at this certain portion of code / function, the error always happened. So i downloaded the original dll which i have backup earlier (remember, always backup!!) and then i opened the dll with reflector, searched for that part of code / function. And there i found that the code was different compared to the source code which was passed to me :( can you imagine how frustated i will be, if i didn't use this tool? So i sent the explanation to my project manager and he's ok with that since i also showed him the comparison proof that i was given not the latest source code of the tool :) so thanks so much again to Lutz Roeder's Reflector :)

You can find the tool with other very useful tool in Lutz Roeder's site : http://www.aisto.com/roeder/dotnet/

So i always have these several tools when doing the programming, such as :
1. EditPlus : very handy for editing text, regex plus syntax templates for c#, javascript, sql, etc
2. Araxis Merge : compare files and folders, very very useful especially you have several environments (development, testing / staging, production)
3. .Net Reflector : as mentioned above.
4. SnagIt : Image capture tool, very fast and handy when doing screen captures for documentation, etc

For more very useful tools when doing the programming, there are several good articles such as :
James Avery : http://msdn.microsoft.com/msdnmag/issues/04/07/MustHaveTools/
Scott Hanselman : http://www.hanselman.com/blog/MyGrokTalkTenToolsInTenMinutes.aspx

Tuesday, March 20, 2007

HTTP & HTTPS / SSL in ASP.Net Applications

If you have the requirements to need to implement https / SSL in the asp.net application.

To avoid popup notifications for partial secured page in IE, make sure to modify references from http:// to https:// (e.g. image references, script sources, etc)

There are several way to implement the SSL in the applications, depends on the requirements :
1. Fully secured : All pages in the application will be using https://
2. Partial secured : Only certain pages in the application will be using https://, for example : login page, data submission page, reporting page, etc.

I found 2 interesting articles how to implement this partial SSL :
1. http://www.codeproject.com/useritems/switchprotocol.asp
This is a simplier solution for partial secured pages, but will need to recompile if suddenly the users need other pages to be secured :P
2. http://www.codeproject.com/aspnet/WebPageSecurity_v2.asp
This is more complete solution for partial secured pages, we can easily change the file / directory path that we need to secure in the configuration file.

PS : As you try the sample code, the session information can still be retrieved when switching from http to https vice versa.

There's a tool from microsoft called IISDiagnostic Tools, you can use the SSL Diagnostic to setup a temporary SSL certificate in your local, so you can use this to do the SSL testing locally.
http://www.microsoft.com/windowsserver2003/iis/diagnostictools/default.mspx

Friday, March 16, 2007

Stored Procedure Parameters - String limitation

In some scenario, we need to pass long string to the stored procedure.
My case is that we need to pass selected checkbox options value to the stored procedure.
Sometimes when the list of checkbox is very long, the parameter string value can exceed varchar(8000 characters).

Example of the parameter : '(''chkvalue1'',''chkvalue2'',''chkvalue3''.......)'

My solution :
1. Instead of constructing the values for the query, we change the strategy to use xml string format for the parameter.
Example of the new parameter : '<root><param value="chkvalue1" /><param value="chkvalue2" /><param value="chkvalue3" />.....</root>'
2. Change the data type for the parameter from varchar(8000) to text
3. Use OPENXML to get the values from the xml :
SELECT value
INTO ##temptable
FROM OPENXML (@idoc, '/root/param',1)
WITH (value varchar(100))
4. Use this temp table to achieve the same result