Sunday, June 28, 2009

Geographic information of the User on your page!!!

Wanted to show the geographical information of the visitor on homepage of your site. Here is a great API to your rescue
http://ipinfodb.com/ip_query2.php?ip=(YourMachinesIP)
I have written this sample code for using this API for your reference have a look :
protected void Page_Load(object sender, System.EventArgs e)
{
string userip;
string url;
userip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (userip== null)
{
userip = Request.ServerVariables["REMOTE_ADDR"];
}
url = String.Format("
http://ipinfodb.com/ip_query2.php?ip=%7B0%7D", userip);
WebClient wc = new WebClient();
Stream data = wc.OpenRead(url);
String str;
using (StreamReader sr = new StreamReader(data))
{
str = sr.ReadToEnd();
data.Close();
}
//Use data is str variable to display on web page….
}

Happy coding!!!

Vinod

Happy coding!!!!!

Tech Tip:Tostring() vs Convert.Tostring()

We normally use the .Tostring() or Convert.Tostring() functions in day to day coding. Although the function of two methods are same but It’s important to understand the behavior of the methods in determining which to use.

When using the Convert.ToString() on a string value it simply returns the value, the ToString() on a sting returns this (a reference to the string itself).
When called on an object, the Convert.ToString() will try to convert it an IConvertible interface first, if this fails and the object is not null it will call the ToString on the object. If the object reference is null it returns an empty string.

The default behavior of the ToString method on an object is to return the name of the type (this.GetType().ToString()) . Calling the ToString() method directly has less overhead but you need to perform a null test prior to calling any methods on the object.

So, it’s clearly advantageous to use the Convert.Tostring() instead of ToString() as it will eliminate need to check for null prior to conversion.


Happy coding !!!!
Vinod

SCOPE_IDENTITY() vs. @@IDENTITY

As we all know the use of @@IDENTITY to retrieve the identity of the most recently added row in a table. Although this is one of those common practices in SQL Server 7, which does not have a SCOPE_IDENTITY() function, it is no longer the accepted way to get the identity of the most recently added row in a table in SQL Server 2000 and SQL Server 2005.
@@IDENTITY returns the most recently created identity for your current connection, not necessarily the identity for the recently added row in a table. You could have a situation where there is a trigger that inserts a new record in a Logs Table, for example, when your Stored Procedure or INSERT SQL Statement inserts a record in the Orders Table. If you use @@IDENTITY to retrieve the identity of the new order, you will actually get the identity of the record added into the Log Table and not the Orders Table, which will create a nasty, nasty bug in your data access layer.

To avoid the potential problems associated with someone adding a trigger later on, always use SCOPE_IDENTITY() to return the identity of the recently added row in your INSERT SQL Statement or Stored Procedure.

Happy Coding!!!!