Tuesday, June 30, 2009

Find GEO location through IP address

Geo IP Tool is a free service that helps you find geographical location via IP. I was trying to find out some service through which I can found the GEO information of customers via IP address. Below is a small helper code that can query Geo IP Tool against an IP. All you need is to place following helper class some where in your website. As you can see helper class only contains a single method GetLocationByIp of return type string. In fact it returns HTML containing returned information from Geo Ip Tool. Once you get the Geo information against an IP, all you need is to put it some literal control to be rendered.

Here is the GeoIPTool class, you can place it in your App_Code folder of ASP.NET website. Once you are done with this change then you can use user control code posted in next code portion for demo purpose.

using System;

using System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Net;
using
System.IO;

public static class GeoIPTool

{

public static string GetLocationByIP(string ipAddress)

{

string geoiptoolurl = "http://www.geoiptool.com/?IP={0}";

string gflag = "http://www.geoiptool.com/flags/";

string p1 = "<"+"table width=\"300\" height=\"300\"";

string p2 = " border=\"0\" cellpadding=\"4\"";

string p3 = " cellspacing=\"0\" class=\"tbl_style\">";

geoiptoolurl = string.Format(geoiptoolurl, ipAddress);

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(geoiptoolurl);

using (HttpWebResponse response = (HttpWebResponse)(request.GetResponse()))

{

using (StreamReader reader = new StreamReader(response.GetResponseStream()))

{

string htmlResult = reader.ReadToEnd();

if (!string.IsNullOrEmpty(htmlResult))

{

int sindex = htmlResult.IndexOf(p1+p2+p3);

htmlResult = htmlResult.Remove(0, sindex);

sindex = 0;

int eindex = htmlResult.IndexOf("");

string geoLocationTable = htmlResult.Substring(sindex, eindex + 8);

if (geoLocationTable.Contains("/flags/"))

geoLocationTable = geoLocationTable.Replace("/flags/",gflag);

return geoLocationTable;

}

}

}

return string.Empty;

}

}

Following is the demo user control, in order to use this code first you need to create a new user control file with name Sample.ascx in your website and then replace all its contents with following code. Finally replace Your IP Here text in GetLocationByIP call to your desired IP address.

<%@ Control Language="C#" ClassName="Sample" %>

<script runat="server">

protected void Page_Load(object sender, EventArgs e)

{

string geoLocation = GeoIPTool.GetLocationByIP("YOUR IP HERE");

if (!String.IsNullOrEmpty(geoLocation))

GeoLocationHolder.Controls.Add(new LiteralControl(geoLocation));

}

<asp:PlaceHolder ID="GeoLocationHolder" runat="server">asp:PlaceHolder>

Please feel free provide your feedback!

Jump to validation summary on large page

Validation summary is an awesome ASP.NET control that can list validation messages in a collective way. Some time if you have validation summary at bottom of a large page, in this case if some validation error occurs the user has to scroll down towards the bottom to view the pages.

You can enhance user experience with Validation Summary control by automatically jumping to the error messages area when some error occurred. I am just explaining the steps to accomplish this.

In ASP.NET there is a client side variable Page_IsValid that indicates whether the page is currently valid or not. The validation scripts keep this up to date at all times. So all you need is to wrap your validation summary within some div and place some anchor upon that. Finally write a small piece of javascript that checks Page_IsValid and if its set to false simply jump to that named anchor. You can found a very helpful topic about ASP.NET Validation available here

How to calculate column value as summation of all previouse values

This is the demonstration of how to calculate a column value that depends upon sum of all previous values in that column. For example check following two tables
Actual Data:
ProductIdNameQuantity
1Product A10
2Product B10
3Product C10
Required Output:
ProductIdNameCal Quantity
1Product A10
2Product B20
3Product C30

SQL Server Query:

SELECT P.ProductId, P.Name, SUM(PC.Quantity) AS Cal Quantity
FROM Products AS P LEFT OUTER JOIN Products AS PC
ON PC.ProductId <= P.ProductId
GROUP BY P.ProductId, P.Name

Wednesday, March 25, 2009

Free Java MIDlet Manager for Windows Mobile Phones

Some java applications need Java MIDlet Manger to run on your windows mobile devices. For example if you want to install and use Opera Mini, Gmail Client for windows mobiles then you need to have Java MIDlet Manager installed on your device. You can download a free MIDlet Manager avialable here.

Free JVM for Windows Mobile Phones

I was in need to run some java applications on my I-Mate JAM and was having a hard time locating a free or open source JVM for it. After a lot of searching and striking my head against walls finally I found an open source JVM and that is Mysaifu JVM. Its an open source JVM available for free. It doesn't contain any MIDlet Manager means you can not run some applications that require MIDlet Manger for example Opera Mini and Gmail client for mobile phones. But still it could be very helpfull running other java applications on your PDA.

1. How to install
  • Copy the CAB file to \My Documents.
  • Tap the CAB file. 
  • Program is installed under \Program Files\Mysaifu JVM folder.
2. How to uninstall
  • Select "Mysaifu JVM" in "Settings - Remove Programs".

Tuesday, March 24, 2009

How to build Bluetooth Remote Control for desktop machine

Some time ago I was searching for some .NET(CE) stuff and read about the concept of an application that could allow to turn your mobile into a remote control for your desktop machine. I was very impressed by the concept and tried to wrote something similar that could help me control Windows Media Player, PowerPoint slide show and some general windows operations like Logoff User, Shutdown and Restart, Volume Control etc. I am planing to post it with complete source code but currently it needs some finishing steps like removing some unused code and some miner UI improvements. Hopefully I will get some time in near future to get things done.

I developed it for my I-Mate JAM running .NET CE(2.0). Currently .NET does not have any explicit support for bluetooth like java where there is a seperate API (Java Bluetooth API). Microsoft recommends use of sockets for this purpose, using sockets would be really time consuming task because that is relativly a low level programming task so I searched for some open source Bluetooth API that targets microsoft .NET and finally descided to use 32feet.net API. Its an open source API for personal networking.

Hardware Requirements
  1. Develoment Machine capable of running Visual Studio 2005
  2. I-Mate JAM or equvalent PDA device that support Microsoft Bluetooth stack.
  3. Bluetooth dongle for desktop machine, make sure that dongle supports Microsoft Bluetooth stack.
  4. Data cable for PDA
Softwares Requirements
  1. Visual Studio 2005 Professional Edition
  2. .NET CE(2.0) on your PDA (You can install .NET(CE) on I-Mate JAM from here)
  3. Microsoft ActiveSync (You can get it from here)
  4. 32feet.net(InTheHand) API (You can get it from here)
Setting up development environment
  • Install Visual Studio 2005 on development machine
  • Install 32feet.net on development machine. If your hardware is not working with API then you can try following workarund.
  • Now install Microsoft ActiveSync on development machine as well.
  • Finally connect you PDA and machine with datacable and test the connection.
How to control Media Player remotly via Bluetooth
  • First create a Windows form applicaton that will work as server on desktop machine. This applicaton will work as a Bluetooth listner. In 32feet.net samples there is a Remote sample you can use it as guide line.
  • Next you need to find out command messages for Windows Media Player, You can use spy++ for this purpose. For example what is the command message sent against volume up command.
  • Next you need to use import some win32 functions in your C# application for locating the running instance of Winodws Media Player and pass it the command message to performs that action.
  • Finally map some keys that listner recieves via bluetooth and then raises apperoperiate command message for Windows Media Player.
  • Now create Device Application 2.0 for your PDA or you can use the client part of remote sample available with 32feet.net.
  • Once client applicaton is setup properly all you need is to send those keys to desktop machine for wich you define mapping on server component. Once server module will reieve a mapped key for example you have mapped 9 to volume up then it will raise volume up command message for Winodws Media Player and hence will stepup the volume.