hjælp min connect virker ikke

Tags:    asp

fejl meddelelsen er conn. variablen er ikke defineret

connect.asp
----------

<%

Response.Buffer = True

mode = Request.QueryString("mode")

'Åbner connection til databasen
Set conn = Server.CreateObject("ADODB.Connection")
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ="&Server.MapPath("gbog.mdb")
%>

-------
gbog.asp
------------

<% OPTION EXPLICIT %>
<!-- #include file="connect.asp" -->
<html><head><title>Gæstebog</title><head><body
<%
dim strAction

strAction=request("action")

select case strAction
case "write"
'vis formular til indtastning
ShowOptions
ShowForm
case "show"
'vis indhold
ShowOptions
ShowList request("fromid")
case "insert"
'indsæt resultat af formuler

WriteForm
ShowOptions
ShowList 0
case else
'vis valgmuligheder
ShowOptions
end select

'---------------------------------------------
'funktioner
'---------------------------------------------

sub ShowForm

%>
<form action="guestbook.asp?action=insert" method="post">
Navn: <input type="text" name="guestname" size="20"><br>
Email: <input type="text" name="guestemail" size="20"><br>
<textarea anme="guestmessage" rows="1" cols="20"> </textarea><br>
<input type="submit" value="indsend">
<%
end sub

sub ShowList (intformID)
'viser indlæg i gæstebogen
'10 indlæg vises ad gangen
dim strSQL, objRS, strWhere
dim arrRows,i
if intFromID > 0 then
strWhere = " WHERE guestid <= " & intFromID & " "
else
str = ""
end if

strSQL = " SELECT guestid, guestname, " & _
"guestemail, guestmassage FROM GuestBook " & _
strWhere & _
"ORDER BY datesubmitted DESC"

Set objRS = server.creatobject("ADODB.Recordset")
with objRS
.open strSQL, strConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
if not .EOF then
arrRows = .GetRows(10)
end if
.close
end with
set objRS = Nothing


if isArray(arrRows) then
intFormID = arrRows(0,ubound(arrRows,2))
for i = o to ubound(arrRows,2)
response.write arrRows(0,i) & ". " & _
arrRows(1,i) & _
"<br>" & arrRows(2,i) & _
"<br>" & arrRows(3,o) & "<hr>"
next
response.write _
"<a href='guesbokk.asp?action=show&formid= " & intFormID+10 & "'>Tilbage</a> " & _
"<a href='guesbokk.asp?action=show&formid= " & intFormID - 1 & "'>Frem</a>"
else
response.write "<p>ikke flere indlæg i gæstebogen.</p>"
end if
end sub

sub WriteForm
' indlægget skrives til databasen.
dim strSQL, objCMD
srtSQL = "INSERT INTO GuestBook " & _
"(guestname, guestemail, guestmessage, datesubmitted) " & _
"VALUES (" & _
"'" & request("guestname") & "', " & _
"'" & request("guestemail") & "', " & _
"'" & request("guestmessage") & "', " & _
"getdate() " & _
")"


set objCMD = server.createobject("ADODB.Command")
with objCMD
.ActiveConnection = strConnect
.CommandType = adCmdText
.CommandText = strSQL
.execute
end with
set cbjCMD = Nothing
end sub

sub showOptions
'Vises, hvilket muligheder der er .
%>
<h1>Gæstebog</h1>
<p>Hej du kan skrive i min gæstebog, hvis du har lyst.
<br>
<a href="guestbook.asp?action=write">Skriv i gæstebog</a><br>
<a href="guestbook.asp?action=show">Se hvad andre har skrevet</a>
</p>
<%
end sub

%>
</body></html>



3 svar postet i denne tråd vises herunder
1 indlæg har modtaget i alt 1 karma
Sorter efter stemmer Sorter efter dato
fejl meddelelsen er conn. variablen er ikke defineret

connect.asp
----------

<%

Response.Buffer = True

mode = Request.QueryString("mode")

'Åbner connection til databasen
Set conn = Server.CreateObject("ADODB.Connection")
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ="&Server.MapPath("gbog.mdb")
%>

-------
gbog.asp
------------

<% OPTION EXPLICIT %>
<!-- #include file="connect.asp" -->
<html><head><title>Gæstebog</title><head><body
<%
dim strAction

strAction=request("action")

select case strAction
case "write"
'vis formular til indtastning
ShowOptions
ShowForm
case "show"
'vis indhold
ShowOptions
ShowList request("fromid")
case "insert"
'indsæt resultat af formuler

WriteForm
ShowOptions
ShowList 0
case else
'vis valgmuligheder
ShowOptions
end select

'---------------------------------------------
'funktioner
'---------------------------------------------

sub ShowForm

%>
<form action="guestbook.asp?action=insert" method="post">
Navn: <input type="text" name="guestname" size="20">
Email: <input type="text" name="guestemail" size="20">
<textarea anme="guestmessage" rows="1" cols="20"> </textarea>
<input type="submit" value="indsend">
<%
end sub

sub ShowList (intformID)
'viser indlæg i gæstebogen
'10 indlæg vises ad gangen
dim strSQL, objRS, strWhere
dim arrRows,i
if intFromID > 0 then
strWhere = " WHERE guestid <= " & intFromID & " "
else
str = ""
end if

strSQL = " SELECT guestid, guestname, " & _
"guestemail, guestmassage FROM GuestBook " & _
strWhere & _
"ORDER BY datesubmitted DESC"

Set objRS = server.creatobject("ADODB.Recordset")
with objRS
.open strSQL, strConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
if not .EOF then
arrRows = .GetRows(10)
end if
.close
end with
set objRS = Nothing


if isArray(arrRows) then
intFormID = arrRows(0,ubound(arrRows,2))
for i = o to ubound(arrRows,2)
response.write arrRows(0,i) & ". " & _
arrRows(1,i) & _
"" & arrRows(2,i) & _
"" & arrRows(3,o) & "<hr>"
next
response.write _
"<a href='guesbokk.asp?action=show&formid= " & intFormID+10 & "'>Tilbage</a> " & _
"<a href='guesbokk.asp?action=show&formid= " & intFormID - 1 & "'>Frem</a>"
else
response.write "<p>ikke flere indlæg i gæstebogen.</p>"
end if
end sub

sub WriteForm
' indlægget skrives til databasen.
dim strSQL, objCMD
srtSQL = "INSERT INTO GuestBook " & _
"(guestname, guestemail, guestmessage, datesubmitted) " & _
"VALUES (" & _
"'" & request("guestname") & "', " & _
"'" & request("guestemail") & "', " & _
"'" & request("guestmessage") & "', " & _
"getdate() " & _
")"


set objCMD = server.createobject("ADODB.Command")
with objCMD
.ActiveConnection = strConnect
.CommandType = adCmdText
.CommandText = strSQL
.execute
end with
set cbjCMD = Nothing
end sub

sub showOptions
'Vises, hvilket muligheder der er .
%>
<h1>Gæstebog</h1>
<p>Hej du kan skrive i min gæstebog, hvis du har lyst.

<a href="guestbook.asp?action=write">Skriv i gæstebog</a>
<a href="guestbook.asp?action=show">Se hvad andre har skrevet</a>
</p>
<%
end sub

%>
</body></html>


Hej,

Der er ca. 15 fejl i denne kode (stavefejl , kommandoer, osv.)

Du skal erstatte ALT kode i filen "gbog.asp". Filen "connect.asp" kan du godt slette, der er ikke brug for den.

Har er koden:

<%

Response.Buffer = True

Set conn = Server.CreateObject("ADODB.Connection")
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ="&Server.MapPath("gbog.mdb")
%>

<html>
<head>
<title>Gæstebog</title>
<head>
<body>

<%
dim strAction

strAction=request("action")

select case strAction
case "write"
'vis formular til indtastning
ShowOptions
ShowForm
case "show"
'vis indhold
ShowOptions
ShowList request("fromid")
case "insert"
'indsæt resultat af formuler

WriteForm
ShowOptions
ShowList 0
case else
'vis valgmuligheder
ShowOptions
end select

'---------------------------------------------
'funktioner
'---------------------------------------------

sub ShowForm

%>
<form action="gbog.asp?action=insert" method="post">
Navn: <input type="text" name="guestname" size="20"><br>
Email: <input type="text" name="guestemail" size="20"><br>
<textarea name="guestmessage" rows="1" cols="20"> </textarea><br>
<input type="submit" value="indsend">
<%
end sub

sub ShowList (intformID)
'viser indlæg i gæstebogen
'10 indlæg vises ad gangen
dim strSQL, objRS, strWhere
dim arrRows,i
if intFromID > 0 then
strWhere = " WHERE guestid <= " & intFromID & " "
else
str = ""
end if

strSQL = "SELECT * FROM GuestBook " & _
strWhere & _
"ORDER BY datesubmitted DESC"

Set objRS = server.createobject("ADODB.Recordset")
with objRS
.open strSQL, Conn, 1,3
if not .EOF then
arrRows = .GetRows(10)
end if
.close
end with
set objRS = Nothing


if isArray(arrRows) then
intFormID = arrRows(0,ubound(arrRows,2))
for i = o to ubound(arrRows,2)
response.write arrRows(0,i) & ". " & _
arrRows(1,i) & _
"<br>" & arrRows(2,i) & _
"<br>" & arrRows(3,o) & "<hr>"
next
response.write _
"<a href='gbog.asp?action=show&formid= " & intFormID+10 & "'>Tilbage</a> " & _
"<a href='gbog.asp?action=show&formid= " & intFormID - 1 & "'>Frem</a>"
else
response.write "<p>ikke flere indlæg i gæstebogen.</p>"
end if
end sub

sub WriteForm
' indlægget skrives til databasen.
dim strSQL, objCMD
strSQL = "INSERT INTO GuestBook (guestename, guestemail, guestmassage, datesubmitted) VALUES (" & _
"'" & request.form("guestname") & "', " & _
"'" & request.form("guestemail") & "', " & _
"'" & request.form("guestmessage") & "', '" & now() & "'" & _
")"


set objCMD = server.createobject("ADODB.Command")
with objCMD
set .ActiveConnection = Conn
.CommandText = strSQL
.execute
end with
set cbjCMD = Nothing
end sub

sub showOptions
'Vises, hvilket muligheder der er .
%>
<h1>Gæstebog</h1>
<p>Hej du kan skrive i min gæstebog, hvis du har lyst.
<br>
<a href="gbog.asp?action=write">Skriv i gæstebog</a><br>
<a href="gbog.asp?action=show">Se hvad andre har skrevet</a>
</p>
<%
end sub

%>
</body></html>


Mvh.

- Kim Pedersen



Hej Martin Lund,
fjern <% OPTION EXPLICIT %>.
Så burde det virke!

Husk at OPTION EXPLICIT betyder at du definerer alle din variabler med "Dim variabel", inden du kan bruge dem. Det kan hjælpe på hastigheden af scriptet... dog ikke vildt meget.

/Martin Wolf



gjore ingen ting



t