Friday, February 15, 2008

check if url is valid and accessible .NET

This is a common problem for most of ASP.NET developer when we try to check if the url is valid or not. Here is a tip for you. :)

public static bool IsValidUrl(string url)
{
HttpWebRequest httpReq =
(HttpWebRequest)WebRequest.Create(url);
httpReq.AllowAutoRedirect =
false;
httpReq.Method = "HEAD";
httpReq.KeepAlive =
false;
httpReq.Timeout = 2000;
HttpWebResponse httpRes;
bool exists =
false;
try
{
httpRes = (HttpWebResponse)httpReq.GetResponse();
if
(httpRes.StatusCode == HttpStatusCode.OK)
{
exists =
true;
}
}
catch (Exception ex)
{
}
return exists;
}

Sunday, February 10, 2008

Google map program using .net

You can create a custom google map by using Google Maps Api which example found in http://code.google.com/apis/maps/documentation/examples/


However, those example is written in java script and it is not so stright forward to be used in ASP.NET code.... Anyway, to work with java script code with ASP.NET keep these in mind


let txtLongitude is a control which can be used inside javascript, u can called the txtLongitude by
""

txtLatitude.ClientID is used because is require if you are using ASP.NET master pages.

Thursday, February 7, 2008

confusing file path for web developer .net

Are you one of the developer always confuse whith this kind of path e "games/alien.jpg" or "../games/alien.jpg" or "/games/alien.jpg" ? Even if you know the meaning, i am sure you still have problem of this because the path is constructed as it depends how the caller is located. Seem to be over complicated here. :)

I am one of the developer that alwasys confuse with file path that looks like <*img src=".../love/app/basic.jpg" /*>". i really do not like because it can be problematic especially if you are ASP.NET developer that uses master pages across the pages.

if you are one of those guy that always experince broken links, I have an elegant solution to this problem.

Given a virtual directory named site, which can be accessibled as http://localhost/site and you have a file which located at http://localhost/site/games/alien.jpg

to effectively locate the alien.jpg, you got to use this

ResolveUrl("~/games/alien.jpg")

In this way, you won't be confused how the file is located. ~ represents the parent virtual path.

Ok, if you wish to upload the file to the server using the ResolveUrl don't give you the real path... in this case, replace the ResolveUrl method to MapPath method.

e.g:

MapPath("~/games/alien.jpg"). The file will be uploaded to the location of '~'.


Got it?

Different between web application and windows application

Obviously, by the name itself web application is built for web and can be viewed by internet browser. Whereas windows application is a program that runs under windows. People choose web application because web app can relatively easy to deloy and once deloy it is readily widely available to anywhere and anytime. The design itself is distrbuted and programmers do not need to worry so much about complicated deloyment consideration. Sometimes, programmers choose to write in windows application is because somethings cannot be possibly done by having web application. For example, extensive use of processing power such as graphic randering program, e.g 3D designer program. A special program that deals with hardware such as printer driver, have to be written in windows application. Usually "designer program" oftens built as windows application. Whereas business application that requires multi users such as inventory system is oftenly built as web application.

If you are unsure of which is the best choice to go with, please follow my guildline, -

first think of if the program can be easily built and deployed by having web application, if the answer meet these criteria, then go for web application.

Otherwise go for windows application. or perhaps a combination of web and windows which known as smart application which combinethe best of both.

As my own preference, i like web application because it is relatively easy to manage the communication between modules and because it support css which makes styling much easier and faster.

I had been building windows application by using the composite application block CAB... The framework is cool but deployment can be much hassle because you have to worry the security issues ( you do not want your files be hacked by the user because he got your files).

Last but not least, go for web if possible for business realated appplication.

Post me a comment if you are disaggree with me :)

Monday, February 4, 2008

Split camel casing using regex .NET

ever wonder any better apporoch to convert "BeMyLover" to "Be My Lover"? Here the function in C#.


private string SplitCamelCasing(string input)
{
string output =
"";
return output =
System.Text.RegularExpressions.Regex.Replace(
input,
"([A-Z])",
"
$1",
System.Text.RegularExpressions.RegexOptions.Compiled).Trim();
}

Sunday, February 3, 2008

An easy way to manipulate database using Subsonic

I had been using various tools to manipulate database. The only tool that i find very simple so far .net web development is subsonic. http://www.subsonicproject.com/.

Subsonic is really powerful to CRUD your database. SQL is not nessary and it is all object oriented. The best object realtional mapper for .net 2.0

Give a try :)