Sunday, October 28, 2007

Find Country Based on IP Address

I had a requirement to find out Country and set it to a dropdown control. For this I needed to collect the IP of the user and secondly match the IP collected with a database of IP's.

1. Collect the IP of the user. Two IP's are available - one of the proxy's and other the users machines. We must deciede to collect either the proxy IP address or the the machine ip of the user
To collect the IP Address of the Proxy we can use HttpContext.Current.Request.UserHostAddress

To Collect the IP of the user machine(not the proxy server ip),
HttpContext.Current.Request.ServerVariables("HTTP_X_FORWARDED_FOR")

2.Match the collected IP address to an IP Database
As we all know an IP address is in the format xx1.xx2.xx3.xx4.

a)IP Address Table
The IP address table is available as free downloads as text or MS Access DB, contains the following fields:
StartIP,
EndIP,
StartIP as a equivalent number,
EndIP as an equivalent number,
CountryCode and
Country.
If we deciede to keep this table as a table in our database, it needs to be a different table (not to integrate with existing country table), since there are many IP address rows per country.

b) Convert IP Address to its Equivalent Number
We have to convert the collected IP to its equivalent number. The following formulae will convert to its equivalent number - (xx1*16777216) +(xx2*65536)+(xx3*256) +xx4

c) Match the equivalent number of converted IP to that of the IP Database
Match the following formulae to get its equivalent country.

If lngConvertedIP between NumberEquivalentOfStartIP and NumberEquivalentOfEndIP, retrieve countryCode.

Wednesday, October 24, 2007

Custom Format in Datagrid - Multiple Rows and Columns

I needed to display data in Datagrid with a custom format - 3 rows X 4 Column instead of the default single row.

It was done by first adding layouts in a webuser control. Webuser control helps to make maintenance easy, playing in the templates of datagrid. Added the webuser control into the datagrid in a template column. Then bind the data. Let me elaborate it.

1. Add a UserControl and create its layout:

a) Design the layout - I made a three row, 3 column table table and placed label control in the cells.

b)Assign Values to the Controls - I had used labels to display data and used Databinder.Eval to display it in the Text property
< id="lblCountry" runat="server" text="'< %#">'> < /asp:Label >

2. Create DataGrid in the webform and add webusercontrol to the datagrid
So the Webusergrid is done, and created a datagrid in the Webform. In the datagrid, created a template column with autogenerate columns as false. The following is the markup
< id="dbgSearch" runat="server" autogeneratecolumns="False">
<><>< id="MyProvider1" runat="server">< /itemtemplate >< /asp:TemplateColumn >< /columns >
< /asp:DataGrid >
3. Binding Data to the Datagrid
And in the code of webform ( .aspx.vb), done the following code to bind the data

dbgSearch.DataSource = FillCountries
dbgSearch.DataBind()

Note: The datafield passed from to the datasource must match the property defined in the webusercontrols property.

Sunday, October 21, 2007

Start and Stop SQLServer 2005 from Command Prompt

Why always spend time opening services and from Control Panel to start or stop SQL Server. It can be done thru command prompt

To Start SQL Server at the command prompt (Start->Run)
net start MSSQLSERVER

To Stop SQL Server from the command prompt (Start->Run)
net stop MSSQLSERVER

Saturday, October 20, 2007

Dropdownlist - Binding Values, and setting a Value

1. To Bind Data to a DropDownList,
Data can be binded to a dropdownlist from , Arraylist or objectdatasource, and DataSource property can be used.

DropDownList1.Datasource=
DropDownList1.DataTextField= "UserName"
DropDownList1.DataValueField="UserID"
DropDownList1.DataBind()

2. To set values to a dropdown list from code, we have to use SelectedValue property of dropdown control

Dim strUserName as string ="Bill"
DropDownList1.SelectedValue=
DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(strUserName))



AJAX Accordian Control - Object reference not set to an instance of an object

I was getting the error message for all the Standard Controls available in the Toolbar.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

I was wondering why and I found a neat and straight workaround in the following link http://couldbedone.blogspot.com/2007/07/what-wrong-with-accordion-control.html


This workaround worked for me well, by adding

Accordion1.FindControl("nothing")

in page_init handler