Tuesday, January 6, 2009

get the ip and find the what countre from where it comming

private void SetUserCountry(Order newOrder)
{
string strIPaddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

//strIPaddress = "220.136.40.211";
string[] arrIPNumber = strIPaddress.Split('.');

//IP Number = A x 16777216 + B x 65536 + C x 256 + D
Int64 A = Int64.Parse(arrIPNumber[0]) * 16777216;
Int64 B = Int64.Parse(arrIPNumber[1]) * 65536;
Int64 C = Int64.Parse(arrIPNumber[2]) * 256;
Int64 D = Int64.Parse(arrIPNumber[3]);


Int64 iPNumber = A + B + C + D;

//lblError.Text = "IP :" + strIPaddress + " ipnumber : " + iPNumber;

string strUserCountry = newOrder.GetUserCountry(iPNumber);

if (strUserCountry != string.Empty)
{
//ddlBillingCountry.SelectedValue = ddlBillingCountry.Items.FindByText(strUserCountry).Value;

}



}



public string GetUserCountry(Int64 IPNumber)
{
try
{
string strCountry = string.Empty;

Database db = DatabaseFactory.CreateDatabase(Constants.Database_Connection_Name);
DbCommand dbCommand = db.GetStoredProcCommand(Constants.SP_GetUserCountry);
db.AddInParameter(dbCommand, "@nIPNumber", DbType.String, IPNumber);

DataSet dsUserCountry = db.ExecuteDataSet(dbCommand);

if (dsUserCountry != null)
{
if (dsUserCountry.Tables[0].Rows.Count > 0)
{
strCountry = dsUserCountry.Tables[0].Rows[0]["Country"].ToString();


}
}

return strCountry;

}

catch (Exception ex)
{
ex.Data.Add("BusinessLayerException", GetType().ToString() + Constants.Error_Seperator + "public DataTable GetDeliveryCities()");
throw ex;

}


}
//Stored procedure
-----
ALTER PROCEDURE [dbo].[uspGetUsercountry]

@nIPNumber BIGINT

AS

BEGIN

SELECT Country
FROM dbo.tblIPToCountry
WHERE (ToIPAddress >= @nIPNumber) AND (FromIPAddress <= @nIPNumber)




END

No comments: