Simple ASP Database... Hmmm

Status
Not open for further replies.
Working with ASP, I have a simple database of names and addresses.

Here is my access database:

ID | Name | Address | City | State | Zip

I want to use a call to ASP such as:

www.whatever.com/myscript.asp?ID=1

then have the script print out the HTML using a template and plugging in ID 1's info

Example, if in the database under ID number 33, I have Bill Smith, 202 Main St. etc then if the call to the script includes ?ID=33 in the URL, then it would display Bill Smiths info in a little HTML template I have in the script...

This is really simple but I cannot find any sample coding to do this. If anyone out there knows how, any help would be GREATLY appreciated! Thanks, Shawn!!
 
I don't know much about Access, but here's some programming with an SQL statement.

code:<HR>
Code:
<%<P>str = Request.QueryString("ID")<P>sqlQuery = "SELECT * FROM table WHERE ID = '" + str + "'"<P>%>
You'll have to worry about database connections and whatnot yourself though.

You might want to take a look at asp101.com. They've got some great stuff there, and a nice big asp messageboard in which to ask questions such as this.
 

Coleman

Ars Legatus Legionis
16,790
Subscriptor
well..until you get a repsonse from elsewhere or get a book, I'll give you a brief version that'll do it. The connections are important. My example is going to use an OLEDB DSN-less connection, you may have to install a newer MDAC to make it work. You may want to use ODBC instead if it's easier for you...
I also left a lot of the error checking out that I would normally do...

code:<HR>
Code:
<%
Option Explicit<P>DIM SQL
DIM Conn, Cmd
DIM rs<P>' Create & Open connection
Set Conn = Server.CreateObject("ADODB.Connection")
' you need to configure this
Conn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=C:\yourAccessDBhere.mdb"
Conn.Open<P>'Create and configure command object
Set Cmd = Server.CreateObject("ADODB.Command")
Set Cmd.ActiveConnection = Conn<P>' Create Recordset object and configure...
Set rs = Server.CreateObject("ADODB.Recordset")
Set rs.Source = Cmd<P>SQL = "SELECT name, address, city, state, zip FROM yourTableHere WHERE ID=" & Request.QueryString("ID")<P>Cmd.CommandText = SQL<P>rs.Open
IF NOT rs.EOF THEN
  Response.Write("Name:" & rs("Name").Value & "
" & vbCrLf)
  ' repeat and format as you want...
Else
  Response.Write("User not found...
" & vbCrLf)
End If
rs.Close
Conn.Close<P>Set rs = Nothing
Set Cmd = Nothing
Set Conn = Nothing
%>

Oh...and I was a lot more explicit in that I created the command and recordset objects explicitly. You could get away with just creatine the connection object and using it, but it just creates the command and recordset implicitly which leaves you with a lot less control over things.

Hope that helps.
-Jason
 
Jason,

The script worked like a charm when I put it on my own computer. But when I started using it on my ASP enabled web hosting account, I ran into problems, specifically with this line:

Conn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=mydatabase.mdb"

This is what I received:
ADODB.Connection.1 error '80004005'

SQLState: IM002
Native Error Code: 0
[INTERSOLV][ODBC lib] Data source name not found and no default driver specified


/tester.asp, line 11


Now I don't know if its just a problem of not pointing it to my database, or something more. I tried /usr/bin/myaccount/database.mdb and just database.mdb and it can never seem to find the source. Any suggestions?

Thanks
 

Coleman

Ars Legatus Legionis
16,790
Subscriptor
Sorry I'm catching up on old threads now.

My example used a pretty current OLEDB driver for JET. Try updating your data access libraries to MDAC 2.5. One thing to watch out for, if you update to newer then 2.5, JET is no longer updated as part of MDAC starting with v2.6, you have to add the JET drivers seperately for those versions.

See http://www.microsoft.com/data/
-Jason
 
To get ASP to point to your Access DB when you are on a server instead of specifically stating where the DB file is located (i.e. C:\dbname.mdb) use Server.MapPath like so...


strDBPath = Server.MapPath("yourdbname.mdb")
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & ";"

In this example the db file is located in the same directory/folder that the ASP page is in.

-H
 
Status
Not open for further replies.