Wednesday, April 25, 2012

Locating All details of IP Adress (Method 2)

In my previous post I have explained about how to locate the IP Address country it self. Please go through the before post : http://moretoc.blogspot.in/2012/04/locating-ipadress-using-aspnet-with-c.html. In Yhis post I am going to Explain about how to locate the whole address of the IP. To do this first download the htmlagilitypack from the following URL : http://htmlagilitypack.codeplex.com/releases/view/44954. Which will help us to read the html content.
After Downloading then create a new project and add the downloaded dll in to our solution.e; Right click on references and add the dll.

Then add the New Aspx page with the following code:


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <style type="text/css">
        body
        {
        font: normal 11px auto "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;  
        background-color: #ffffff;
        color: #4f6b72;     
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Panel ID="panelLoc" runat="server">
                    <asp:TextBox ID="txtIP" runat="server"></asp:TextBox>
                    <asp:Button ID="btnGetLoc"
                        runat="server" Text="Get IP Details" onclick="btnGetLoc_Click" />
                    <br />
                    <div id="kk" runat="server"></div>

                </asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>
      
        <asp:UpdateProgress ID="updProgress"
        AssociatedUpdatePanelID="UpdatePanel1"
        runat="server">
            <ProgressTemplate>          
            <img alt="progress" src="images/progress.gif"/>       
            </ProgressTemplate>
        </asp:UpdateProgress>

    </div>
    </form>
</body>
</html>


After creating Aspx page then code behind and add the following namespaces.

using HtmlAgilityPack;


Then add the following code in the code behind file :


protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnGetLoc_Click(object sender, EventArgs e)
        {
            String url = String.Empty;

            if (txtIP.Text.Trim() != String.Empty)
            {
                url = String.Format("http://iplocationtools.com/ip_query2.php?ip={0}", txtIP.Text.Trim());
                HtmlWeb hw = new HtmlWeb();
                HtmlDocument doc = hw.Load(url);
                HtmlNode node = doc.DocumentNode.SelectSingleNode("//div[@align='center']/table[@cellspacing='0'][@width='650']");
                kk.InnerHtml = node.OuterHtml;
              
            }  
        }

Here I am sending the request to http://iplocationtools.com/ip_query2.php and downloading the data and then I am formating the output table in the Div kk.

Output:

 

Locating IPAddress using ASP.NET with C# (Method 1)

In This post I am going to explain about locating the IP Address using ASP.NET with C# programming i.e; If we know the IP address of the system then locating the system where this IP belongs. To do this first we need to download the 3rd party IP Adress Extension DLL.
Please download IP Address extension dll from http://ipaddressextensions.codeplex.com/

After Downloading then create a new project and add the downloaded dll in to our solution.e; Right click on references and add the dll.

Then add the New Aspx page with the following code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        IP Address :
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            style="height: 26px" Text="Locate" />
   
    </div>
    </form>
</body>
</html>


After creating Aspx page then code behind and add the following namespaces.

using System.Net;
using WorldDomination.Net;


Then add the following code in the code behind file

protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string userHostIpAddress = TextBox1.Text;
            IPAddress ipAddress;
            if (IPAddress.TryParse(userHostIpAddress, out ipAddress))
            {
                string country = ipAddress.Country();//To Get Country of Ip Adress
                string iso3166TwoLetterCode = ipAddress.Iso3166TwoLetterCode();//To Get ISO Two Letter Code of Ip Adress

                string iso3166ThreeLetterCode=  ipAddress.Iso3166ThreeLetterCode();//To Get ISO Three Letter Code of Ip Adress

                string registry = ipAddress.Registry();//To Get registry of Ip Adress


                Response.Write("Country     : " + country + "<br/>" + "ISO Two Letter Code    : " + iso3166TwoLetterCode + "<br/>" + "ISO Three Letter Code    :" + iso3166ThreeLetterCode + "<br/>" + "registry    :" + registry);
            }
        }

Output :



Determine whether a table is exist or not in Sql Server database

In this post I am going to explain about “How to check whether a table is exist or not in Sql Server database”. In many situations we might need to identify whether a table is exist or not in a sql server database. This is very simple .
To Do this create one table with the following script :

USE Practice
GO
CREATE TABLE Test
(ID INT,
COL VARCHAR(100))
GO
INSERT INTO Test
SELECT 1,'First'
UNION ALL
SELECT 2,'Second'
GO

Now we have created a table with two rows and the Table name is “Test”. To check whether table is created or not or table is existed or not uses the following query:

Use Practice
IF EXISTS (SELECT 1
          FROM INFORMATION_SCHEMA.TABLES
          WHERE TABLE_TYPE='BASE TABLE'
          AND TABLE_NAME='Test')
              SELECT 'table exists.'
      ELSE
              SELECT 'table does not exist.'

Output: table exists.

Now drop the Table “Test “ using the following drop statement.


Use Practice Drop table Test



Now the Table test is deleted and use the same Query to test the Table exist or not .Then you will get the output is 'table does not exist.

Output : table does not exist.