troubleshooting Question C# hit counter

Avatar of Codeaddict7423
Codeaddict7423Flag for United States of America asked on
7 Comments1 Solution2331 ViewsLast Modified:

I have attempted to write a simple hit counter using both *.txt files and *.xml files.  
The hit counters seem to work locally, however, when I upload these files to my production server, the code does not seem to recognize the *.txt files or the *.xml files.

My code for the *.txt files follow:
<form id="form1" runat="server">
    <table border="1">
    <td> Visitor Count: </td>
    <td valign="top" align="center" style="background-color:Aqua;" >
       <asp:Label ID="Label1" runat="server" Text="Visitor Count:"></asp:Label>

My codebehind follows:

using System;
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.IO;

namespace HSCO_Sheriff_ExternalWeb
    public partial class WebForm15 : System.Web.UI.Page

        protected void Page_Load(object sender, EventArgs e)
            Label1.Text = gethitcounts();
        public string gethitcounts()
            string lastcount = "";
                StreamReader SR = File.OpenText(System.Web.HttpContext.Current.Server.MapPath("counter/hitcounter_webform15.txt"));              
                string getcount = null;

                while ((getcount = SR.ReadLine()) != null)
                    lastcount = lastcount + getcount;
                long newcount = Convert.ToInt64(lastcount);
                TextWriter TxtWtr = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("counter/hitcounter_webform15.txt"));
                SR = File.OpenText(System.Web.HttpContext.Current.Server.MapPath("counter/hitcounter_webform15.txt"));
                getcount = null;
                lastcount = "";
                while ((getcount = SR.ReadLine()) != null)
                    lastcount = lastcount + getcount;

            catch (Exception ex)
                TextWriter TxtWtr = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("counter/hitcounter_webform15.txt"));
                lastcount = "1";
            return lastcount;


Any help in getting this to work OR any code examples that function to provide a simple page count would be greatly appreciated.

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros