Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Charts
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Network - Performing a DNS query using ASP.NET 2.0 and C# .NET
Performing a DNS query using ASP.NET 2.0 and C# .NET

ASP.Net 4.0 Tutorials V4
Server Intellect Cloud Hosting

ASP.NET Network Tutorial

This tutorial will show you how to perform a DNS query using the .NET System.Net class, ASP.NET 2.0 and C#.NET

The .NET Framework offers a number of types that makes accessing networked resources easy.

To perform a DNS query, we will need to first import the System.Net namespace. The System.Net namespace contains the Dns.GetHostByName method, IPHostEntry container class and IPAddress class we will use to perform our DNS query.

using System.Net;

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

We'll put our code in a subroutine called DNSLookup() and call it from the btnSubmit_Click() event.

When the btnSubmit_Click() event fires it calls our DNSLookup() function. This function creates a new instance of the IPHostEntry container class and assigns it a host entry returned from the Dns.GetHostByName method. It then create an array of IPAddress objects using the AddressList of our host entry object. After that, it iterates through each ip and adds it to the text box.

protected void DNSLookup(string domain)
{
try
{
//performs the DNS lookup
IPHostEntry he = Dns.GetHostByName(domain);
IPAddress[] ip_addrs = he.AddressList;
txtIPs.Text = "";
foreach (IPAddress ip in ip_addrs)
{
txtIPs.Text += ip + "\n";
}
}
catch (System.Exception ex)
{
lblStatus.Text = ex.ToString();
}
}

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

We have two textboxes, a submit button, and a label on the front end for user interaction. The front end .aspx page looks something like this:

<table>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1">Domain Name:</td>
<td bgcolor="#FFFFFF"><asp:textbox ID="txtDomain" runat="server"></asp:textbox><br />
<asp:button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" /></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">IP Address(es)
Assigned to Domain Name:</td>
<td bgcolor="#FFFFFF"> &nbsp;<br />
<asp:textbox ID="txtIPs" runat="server"></asp:textbox><br />
<br />
<asp:label ID="lblStatus" runat="server"></asp:label></td>
</tr>
</table>

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

The flow for the code behind page is as follows:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
DNSLookup(txtDomain.Text);
}
protected void DNSLookup(string domain)
{
try
{
//performs the DNS lookup
IPHostEntry he = Dns.GetHostByName(domain);
IPAddress[] ip_addrs = he.AddressList;
txtIPs.Text = "";
foreach (IPAddress ip in ip_addrs)
{
txtIPs.Text += ip + "\n";
}
}
catch (System.Exception ex)
{
lblStatus.Text = ex.ToString();
}
}
}

Looking for the VB.NET 2005 Version? Click Here!

a href="/tutorials/">Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!



 
  Developer Resources







Server Intellect Rocks