Results 1 to 7 of 7

Thread: code question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    outerspace
    Posts
    126

    code question

    Code:
    Dim dblcount As Double
            Dim mystreamreader As New System.IO.StreamReader("C:\INETPUB\WWWROOT\COUNTER\COUNTER.txt")
    
            dblcount = mystreamreader.ReadLine
            dblcount = dblcount + 1
            mystreamreader.Close()
    
            Dim mystreamwriter As New System.IO.StreamWriter("C:\INETPUB\WWWROOT\COUNTER\COUNTER.txt")
    
            mystreamwriter.Write(dblcount)
            mystreamwriter.Close()
            Label1.Text = dblcount.ToString
    I have this code as a hit counter on my page, is this the most efficient way to make a hit counter,
    and 2 for some reason the counter got reset to 0 what would of caused this since it was working fine.
    "All those who wonder are not lost" -j.r.r tolkien

  2. #2
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    You might want to ensure that the streamwriter is overwriting the file. At some point it looks like it could not read anything from your file and a double will default to 0 in that case.
    I personally use a database to record hits. I only have one .aspx page and a placeholder which loads a usercontrol depending on the pageid in the querystring.
    So effectively all my webpages are usercontrols that are indexed in a database with an id and a location to the user control.
    I also have a hitcount table which records the date and pageid of the hit as well as the hits.
    When someone browses to my website I have one stored procedure that takes an id and returns the user control to load and increments the hit table (or inserts a new record if a hitcounter is not in the table for that date and pageid).

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    outerspace
    Posts
    126
    yeah that must of happend. Sounds logical i did build a db hit counter control but I just thought that this way would be simpler to do it that way is there anyway to assure that i dont mess up my file with my code I dont think i can lock the file or anything? Thanks for the help i apperciate
    Last edited by robdotnet00; Oct 9th, 2002 at 02:38 PM.
    "All those who wonder are not lost" -j.r.r tolkien

  4. #4
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    I would suggest you use put the value in your web.config file:-


    <configuration>
    <appSettings>
    <add key="hits" value="0" />
    </appSettings>
    <system.web>
    'the other web.config stuff follows

    Now just use this procedure to increment it:-

    Public Sub AddHit
    Dim XmlR As New System.Xml.XmlTextReader(Server.MapPath("/web.config"))
    Dim Configdoc As New System.Xml.XmlDocument()
    Configdoc.Load(XmlR)
    XmlR.Close()
    Dim xnAppSettings, xnHits As System.Xml.XmlNode
    xnAppSettings = Configdoc.SelectSingleNode("//appSettings")
    xnHits = xnAppSettings.SelectSingleNode("//add[@key='hits']")
    Dim hitcount As Integer = Int32.Parse(xnHits.Attributes("value").Value)
    hitcount += 1
    xnHits.Attributes("value").Value = CStr(hitcount)
    Configdoc.Save(Server.MapPath("/web.config"))
    End

    This loads the web.config into an xmldocument and finds the hits node, gets the value and adds one to it. I've parsed it to string to avoid any conversion errors, added one to it and saved the file, converting the integer back to a string.
    This function assumes the web.config is in your wwwroot folder. If not change the "/web.config" to whatever it should be - "/mywebsite/web.config"
    The code uses xpath to find the node. I find the appsettings node first then the hits node but not being an expert in xpath I'm sure theres a way to find the hits node in one line of code. One for another day.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    outerspace
    Posts
    126
    Ill have to give that a try but it sounds pretty good
    "All those who wonder are not lost" -j.r.r tolkien

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    outerspace
    Posts
    126
    thanks for the help im new to the web config file. I know how to use the security in it but thats about it do u know any other neat tricks involved with it. This is what is known as serialization right?
    "All those who wonder are not lost" -j.r.r tolkien

  7. #7
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    Serialization is the conversion of an object to and from a storage medium - to a file, a database etc. and to get it back exactly the way you saved/sent it.
    XML is very much tied up with serialization and what I've shown you is just a small part of it but you wouldn't normally consider it in those terms.
    The web.config file is used by the asp.net worker process to decide various application parameters and will default to certain values if the web.config does not provide those values.
    The <appSettings> feature allows you to store custom values that you can use. The framework provides a direct way of reading these values but not of writing to them. The idea being that you would normally edit the file manually to change settings.
    So the method above is a way of writing to the file directly from within your application. The direct way to read the values provided by the framework is:-

    ConfigurationSettings.AppSettings("hits")

    This will return the value of the <add.. node with a key of "hits". It does not provide a way to set the value, hence the need for a custom procedure to do so.
    I use the web.config to store my database connection strings. I develop on my home machine and deploy to my website and the connection strings are different for each but I have code that will work in both instances because the code decides what machine it is on and gets the relevent connection string.
    The security side of web.config is very useful too. The ability to redirect all requests for a page to a custom login page for example is very useful. You could have a sub folder called admin and you can place a second web.config in that folder which sets the security to point to a login page. It will inherit all other settings from the web.config file in the root folder so only the authentication setting is required in the second .config file.
    All in all I highly recommend you read up on the configuration side of things. You will find many uses for it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width