Error Catching in C# ASP.NET Application

BigDeer
BigDeer used Ask the Experts™
on
I have a simple landing page for helpdesk support requests that automatically populates a user's information from Active Directory as well as their machine name. Everything works perfectly as long as the app is able to perform the DNS lookup on the computer name to populate that particular field. What I'm running into is the inability to catch the error on the lookup and allow the form to just show a blank field if it's unable to do the lookup. My attempts at using a try/catch have failed.

I've attached the CS code and an image of the error received in the browser.
image001.png
helpdesk.cs.txt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:
Based on the error screenshot looks like you running into error at method call "GetComputer_LanIP()" and not "PingHost()"

Author

Commented:
Yeah, it gets the error querying DNS. I just want it to keep going if it gets the error and leave that part of the form blank if there's an error. Or any part for that matter.

I just haven't been able to get the catch to work.
Top Expert 2011
Commented:
Please try code as attached:
private string GetComputer_LanIP()
        {
            
            try{
            string strHostName =  (Dns.GetHostEntry(Request.ServerVariables["REMOTE_ADDR"]).HostName).Split('.')[0];;
            IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
            foreach (IPAddress ipAddress in ipEntry.AddressList)
            {
                if (ipAddress.AddressFamily.ToString() == "InterNetwork")
                {
                    return ipAddress.ToString();
                }
            }
            }
           catch(Exception ex)
          {
                // log your error here
                return = ex.Message.ToString();
           } 
            return "-";
        }

Open in new window

Author

Commented:
Looks, good. I've deployed it with that in there and we'll see how it works. Thanks for the assistance.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial