GetHostByAddress

G

Guest

I wrote a method which takes an ip address and returns hostname as shown below.
It works fine.. only problem i have is when the passed ip address is not
found in the database the method throws this error message :
The requested name is valid and was found in the database, but it does not
have the correct associated data being resolved for

How can i prevent this error message and instead return "-" ?

Many Thanks.

public string IPToHostname(string IPAddress)
{
IPHostEntry HostInfo = Dns.GetHostByAddress(IPAddress);


if(HostInfo.HostName.ToString()!="")
{
string[] arrHostname = HostInfo.HostName.Split('.');
return arrHostname[0].ToLower();
}
else
{
return "-";
}
}
 
G

Guest

If it is throwing a exception you can catch the exception and return '-'. If
your 'if' condition return statement returns this message may be you should
add additional if condition to check the length of return message or some
thing like that
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top