Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
LeftbarAd
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » XML .NET » Tracking clicks by IP Address using C# and XML

Tracking clicks by IP Address using C# and XML

This is a very simple and easy article, which gives an idea for track the IP address and for getting the number of cliks from a particular system.

Technologies: .NET 2.0,Visual C# .NET
Total downloads :
Total page views :  4098
Rating :
 5/5
This article has been rated :  2 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
ArticleAd
Become a Sponsor




This is a very basic article describing that tracing an IPAddress and getting the number of clicks for a particular control.  This gives the basic idea for counting the unique clicks for a control or for a page using XML.

This article contains only two pages. One is aspx and another one is XML file.

Problem Description:  I have an image control in my page. I need to display the number of clicks for particular user system. This means for the first time when I click on the control, it should display like "no of clicks is 1" and for the second time onwards the value will vary.

Technologies: C#.Net, VISUAL STUDIO 2008

Pages: noofclicks.aspx, noofclicks.aspx.cs, count.xml

Code for noofclicks.aspx:

<form id="form1" runat="server">

    <div>

        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/middle.jpg" onclick="ImageButton1_Click" />

    </div>

</form>

Code behind for noofclicks.aspx page

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Xml;

using System.Net;

using System.Data;

 

public partial class noofclicks : System.Web.UI.Page

{

    XmlDocument doc = new XmlDocument();

    int i;

    DataSet ds;

    int j;

 

    protected void Page_Load(object sender, EventArgs e)

    {

    }

 

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)

    {

        //retrieve ip address

        string host = System.Net.Dns.GetHostName();

        string hostname = Dns.GetHostEntry(host).HostName;

        IPHostEntry ipEntry = Dns.GetHostEntry(host);

        IPAddress[] addr = ipEntry.AddressList;

 

        //xml

        doc.Load(Server.MapPath("~/count.xml"));

        XmlNodeList list = doc.SelectNodes("//NewDataset/section");

        foreach (XmlNode node in list)

        {

            //checking whether the ip address is there or not

            if (addr[0].ToString() == node.ChildNodes[1].InnerText)

            {

                i = Convert.ToInt32(node.ChildNodes[0].InnerText);

                i += 1;

                node.ChildNodes[0].InnerText = i.ToString();

                doc.Save(Server.MapPath("~/count.xml"));

                j = 1;

            }

            else

            {

                j = 0;

            }

        }

 

        if (j == 0)

        {

            ds = new DataSet();

            ds.ReadXml(Server.MapPath("~/count.xml"));

            DataRow row = ds.Tables[0].NewRow();

            row["count"] = "1";

            row["ip"] = addr[0].ToString();

            ds.Tables[0].Rows.Add(row);

            ds.WriteXml(Server.MapPath("~/count.xml"));

            Response.Write(" no of clicks for this image from ur ip address " + i + 1);

        }

        else

            Response.Write(" no of clicks for this image from ur ip address " + i);

    }

} 

count.xml File

<?xml version="1.0" standalone="yes"?>

<NewDataset>

  <section>

    <count>21</count>

    <ip>192.169.1.100</ip>

  </section>

  <section>

    <count>1</count>

    <ip>192.170.1.100</ip>

  </section>

  <section>

    <count>7</count>

    <ip>192.171.1.100</ip>

  </section>

  <section>

    <count>10</count>

    <ip>192.166.1.104</ip>

  </section>

  <section>

    <count>13</count>

    <ip>192.158.1.100</ip>

  </section>

  <section>

    <count>17</count>

    <ip>192.168.1.105</ip>

  </section>

  <section>

    <count>17</count>

    <ip>192.168.1.100</ip>

  </section>

</NewDataset>

This gives only a basic idea....

Enjoy the day buddies...!


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
harika m

Hai, this is Harika working for a software company in India. And I hav more than 2 years of experience on Visual Studio with c#.net and Sql Server

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved