Database issue

  • Thread starter Thread starter BD
  • Start date Start date
B

BD

I'm trying to build a hit counter that does what I want using an Access
database named counters.mdb which contains 2 tables. The only one involved
here is page_count. Here is the code for the aspx test page.
===============================
<%@ Page Language="VB" Debug="true" runat="server"%>
<%@ import Namespace="System.Data.OLEDB" %>

<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
 
First, I am not sure that a counter is best implemented in a database. There
are plenty of examples on the web of counters persisted in memory and spun
down and up as the application is started and stopped. You can also persist
hits to an XML file, if you would rather head that direction.

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!
|
*************************************************
 
Thanks for the reply Cowboy.
First off, I'm very new to ASP.Net and to ASP in general. I'm used to doing
my page code in straight HTML using FrontPage 2003.
I really don't have any idea how to proceed with this and I'm just trying to
muddle my way through it. The reason for using the database was that there
are several pages that will be tracked in that table and the DB also has
another table that I want to implement later for tracking downloads of zip
and exe files which are available on the site.
The error is that the database is read only which I understand has to do
with folder permissions. I can probably solve that on IIS 6 which I use for
testing only. The pages finally end up on another server which is 2003 and
I have no way (AFAIK) of setting folder permissions there.
I'm open to any code that you think will do what I want but I warn you in
advance that this will be a "Hold my hand" operation.
Again, my gratitude for your help here.
BD
 
The read only can come from a couple of things:

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!
|
*************************************************
 
Thanks again Cowboy. I'll be checking your blog. Really all I need it the
ability to track the info. Displaying it on the pages is not a requirement.
It's just so that I can open a private page and see what the stats are. I
don't really want to use the separate counter file pages for each page that
the user sees. I'm also looking at the possibility of tracking it with XML
or an INI file.
BD

Cowboy (Gregory A. Beamer) said:
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 + ".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) said:
The read only can come from a couple of things:

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!
|
*************************************************
 
Back
Top