<script runat="server" language="vb">
Function pCount()
Const adOpenKeyset = 1
Const adLockPessimistic = 2
Const adCmdText = &H0001
Dim hCount As Integer
Dim sPage = Request.FilePath.Remove(0,Request.FilePath.LastIndexOf("/")
+1)
Dim sCmd = "SELECT * FROM page_count WHERE pagename='" & sPage & "';"
Dim dSource As String = Server.MapPath("counters.mdb"), sTest As String =
""
Dim rsCounter = Server.CreateObject("ADODB.Recordset")
rsCounter.Open(sCmd, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
dSource & ";", _
adOpenKeyset, adLockPessimistic, adCmdText)
If rsCounter.EOF Then
sTest = "EOF has been reached."
'rsCounter.AddNew() 'uncommenting this line will throw an error
'rsCounter.Fields("pagename").Value = sPage
End If
rsCounter.Close()
rsCounter = nothing
Dim rTxt = "<p>The database path is: " & dSource & "</p><p>" & sTest &
"</p><p>The page is: " & sPage & "</p>"
Return rTxt
End Function
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Hit Counter Test</title>
</head>
<body>
<%Response.write(pCount())%>
</body>
</html>
=========================================
This produces the correct output, however if I uncomment the line
rsCounter.AddNew()
it starts throwing the following error
System.Runtime.InteropServices.COMException: Cannot update. Database or
object is read-only.
Could someone please tell me what is wrong? I've been at this since
yesterday.
PS: There will be line wrap issues in the above code and I have only
included that part of the If statement to the point of failure, the
rsCounter.update() etc. still has to be added in.
Thanks
BD
Second, even if you persist each hit to the database, I am not sure why you
are adding new rows. It is a single value updated over and over again,
right?
Third, why are you using old style ADO objects instead of ADO.NET objects.
There is really no need for this line:
Server.CreateObject("ADODB.Recordset")
other than familiarity with the old. ADO.NET will be faster and easier to
debug than a COM object, as you have full exposure of what is going on. When
you use an ADODB.Recordset, the COM errors are trapped and .NET exceptions
thrown, which obfuscates what is actually going on.
Finally, you are writing your ASP.NET like ASP, which is not the optimal way
to do something like this.
As for the error, what error are you recieving? One of the most common
errors comes from opening the Access database in Access while you are trying
to hit it with the web page. I forget the error number, but the error
message is completely unrelated to what is actually happening, in most
cases. You are probably not seeing this error anyway, as .NET is trapping it
and throwing its own exception.
--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA
Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss
or just read it:
http://gregorybeamer.spaces.live.com/
*************************************************
| Think outside the box!
|
*************************************************
"BD" <Jus...@nothome.net> wrote in message
news:eWuKA9Es...@TK2MSFTNGP04.phx.gbl...
"Cowboy (Gregory A. Beamer)" <NoSpamM...@comcast.netNoSpamM> wrote in
message news:OQqc0lFs...@TK2MSFTNGP03.phx.gbl...
1. Permissions on the file
2. Permissions on the folder
3. You have the file open while you are running your application
The last is unique to file type databases, like Access, and will only
require a bit of discipline on your part.
If you have an ISP that has a server type database, I would aim that
direction before using Access, personally. Many provide either SQL Express
or mySQL. You can download both for free, so there is no cost issue here.
That is a bit overkill for a simple page counter, however. I will post back
something shortly.
--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA
Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss
or just read it:
http://gregorybeamer.spaces.live.com/
*************************************************
| Think outside the box!
|
*************************************************
"BD" <Jus...@nothome.net> wrote in message
news:Ome6vsGs...@TK2MSFTNGP05.phx.gbl...
"Cowboy (Gregory A. Beamer)" <NoSpamM...@comcast.netNoSpamM> wrote in
message news:OtRHbDYs...@TK2MSFTNGP06.phx.gbl...
> Here is a zip file of a counter application. I did not have time to move
> teh dataset creation and save routines to global.asax, so there is still
> some work to be done. It is C#. I tried to do VB, but it takes more time
> than I have for me to think in VB anymore. I apolgize up front. One of the
> easiest ways to translate is publish the website, which compiles it, and
> look at the code in Reflector (Lutz Roeder), a mandatory tool for .NET
> devs, IMO.
>
> Here are some things to note:
> 1. Open Counters.xml and see that only one of the two pages has a counter
> 2. Hit both pages. You can go back and forth and see how the counters
> work - If they saved back to the XML file, you would also see that you can
> quit the application. I am not sure when I will find more time, so you can
> lookup Application_Start and counters and try yourself. It will not work
> with pagePath, as it is a Uri, not a regular path. There is a routine for
> getting application path in .NET. I just don't have it at the top of my
> head.
>
> The application is designed to add new pages. All you have to do to see
> the counter is place it on a label or something. If you want a prettier
> counter, I would create a user control that pulls images 1-10 and turn the
> count into a string and read each character. Something like:
>
> <!-- in page -->
> <asp:Panel ID="HitCounterPanel" runat="Server"/>
>
> //In Code
> HitCounterPanel.Controls.Clear();
> string hitsAsString = hits.ToString();
> char[] hitsAsArray = hitsAsString.ToCharArray();
>
> for(int i=0;i<hitsAsArray.Length;i++)
> {
> Image img = new Image();
> img.ImageUrl = "images/" + hitsAsArray[i] + ".gif";
> HitCounterPanel.Coutrols.Add(img);
> }
>
> I may have a typo in that. If I get time, I can show you how easy it can
> be to use a database. I will likely do it on my blog so it has a wider
> audience.
>
> --
> Gregory A. Beamer
> MVP, MCP: +I, SE, SD, DBA
>
> Subscribe to my blog
> http://gregorybeamer.spaces.live.com/lists/feed.rss
>
> or just read it:
> http://gregorybeamer.spaces.live.com/
>
> *************************************************
> | Think outside the box! |
> *************************************************
> "Cowboy (Gregory A. Beamer)" <NoSpamM...@comcast.netNoSpamM> wrote in
> message news:eMXBxjXs...@TK2MSFTNGP06.phx.gbl...