Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » XML in C# » 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.

Page Views : 8912
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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...!

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
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.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.