Posts By Category

Posts By Date

Resources:

C# Books
ASP.NET Books DotNet4All








If you like to support this site, feel free to make a donation to support improvements.

Thank you!

Monetize Your Blog

Track your visitors IP & Host Info, but make sure to catch any exceptions

In my previous article, How to track visitors to your site in ASP.NET & C#, I discussed how you can log the Host Info for each page visitor on your site using Dns.GetHostByAddress function.

One thing I did not stress out was the need to catch all exceptions from that method call. There are cases when this call:

string ipAddress = Dns.GetHostByAddress(ipAddress).HostName;

Will not succeed, and you will get the following Exception:


Server Error in '/' Application.


The requested name is valid, but no data of the requested type was found

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.Net.Sockets.SocketException: The requested name is valid, but no data of the requested type was found

Source Error:

Line 21:             // Track Visitors
Line 22:             string ipAddress = IpAddress();
Line 23:             string hostName = Dns.GetHostByAddress(ipAddress).HostName;

From what I gathered, this happens with users connect from machines that are not listed on the Active Directory, or if their network settings is not "use NETBIOS over TCPIP". In all cases, not catching the exception on your side can mean your site is not going to work for these users.

To avoid this, I modified the code as follows:

try

{

    hostName = Dns.GetHostByAddress(ipAddress).HostName;

}

catch (Exception ex)

{

    Logger.EmailError(ex);

    hostName += ex.Message;

}

 

This will let your site surfer in even if we can't resolve their machine host name.

Putting the above code, I got so many cases with users visiting my site generating this error, well at least now they can get in, where before, the web app would crash.

Thanks to my good friend John Sonmez who pointed out the error :)

Happy Programming!

kick it on DotNetKicks.com

Feedback

Posted on 7/24/2012 12:41:37 PM

In my case, error was due Windows Firewall, after turn it off, everything worked like a charm

Posted on 8/23/2011 3:42:13 AM

Server Error in '/Allcargoglobal.net' Application.
--------------------------------------------------------------------------------

The requested name is valid, but no data of the requested type was found
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.Net.Sockets.SocketException: The requested name is valid, but no data of the requested type was found

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[SocketException (0x2afc): The requested name is valid, but no data of the requested type was found]
System.Net.Dns.GetHostByAddress(IPAddress address) +264
System.Net.Dns.GetHostByAddress(String address) +65
AGL.Dac.ITHelp.WebUserControl1.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\IThelp\UrlLinkList.ascx.cs:36
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Control.LoadRecursive() +98
System.Web.UI.Page.ProcessRequestMain() +750

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2379; ASP.NET Version:1.1.4322.2379

The code is

System.Net.IPHostEntry host;
host = System.Net.Dns.GetHostByAddress(Request.ServerVariables["REMOTE_ADDR"]) ;
Label5.Text = host.HostName.Replace(".acmho.com","");
Label1.Text=DateTime.Now.ToLongTimeString().ToString();
UserName=Page.User.Identity.Name;
Connection oCon = new Connection();
Label3.Text = oCon.GetUserName("Get_UserName","@UserDomainName`",Page.User.Identity.Name+"`");
LinkList.DataSource=Connection.DataSet("Get_URLList","","");
LinkList.DataBind();
Datalist1.DataSource=Connection.DataSet("Get_SiteList","","");
Datalist1.DataBind();

I get the error when i click the link then it get's the above error message.

Posted on 8/5/2011 1:22:48 AM

the requested name is valid but no data of the requested type was found

try
{

System.Net.IPHostEntry host;
host = System.Net.Dns.GetHostByAddress(Request.ServerVariables["REMOTE_ADDR"]) ;
Label5.Text = host.HostName.Replace(".acmho.com","");
Label1.Text=DateTime.Now.ToLongTimeString().ToString();
UserName=Page.User.Identity.Name;
Connection oCon = new Connection();
Label3.Text = oCon.GetUserName("Get_UserName","@UserDomainName`",Page.User.Identity.Name+"`");
LinkList.DataSource=Connection.DataSet("Get_URLList","","");
LinkList.DataBind();
Datalist1.DataSource=Connection.DataSet("Get_SiteList","","");
Datalist1.DataBind();
}
catch(Exception ex)
{
host += ex.Message;
}

Posted on 7/17/2009 4:57:11 AM

Thank you man. İts very helpfull. You are great ;)...

Posted on 3/19/2008 7:17:45 AM

Hi,
It helped alot .Thank you

Posted on 4/3/2007 8:20:32 PM

Great article!

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 


Copyright © 2007 Yousef Mannaa. All material on this site is copyrighted.
Do not publish or reproduce any of this material without written permission from the Author