help needed in Form collectin to write for a Data base in ASP | Newbie | | Join Date: Sep 2009 Location: India
Posts: 9
| |
Hi I have Problem in ASP.
I have created a Multi choice Question page in ASP with Submit button.
When I submit my page the User Selected values will be taken to the other page where validation will happen.
My problem is, the number of question will be dynamic and I need to validate the scores and give the final scores.
I tried to use a form collection via for each and validate with my answers but could nt successful
I have tried even writing my selected answers in data base but even that was also not successful .
Can any one know how to validate the answers with database dynamically?
If that’s not possible I want to write the form collections in to data base then validate.
I am attaching my code also. -
<!--#include file="Reload.asp"-->
-
-
-
-
<%
-
-
-
'WriteScore= Session("Score")
-
-
usname =Session("name")
-
dim chapid
-
chapid=11
-
-
Set conn = Server.CreateObject("ADODB.Connection")
-
-
'Connect to the database
-
strMDBpath = Server.MapPath("eLearning.mdb")
-
conn.open "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & strMDBPath
-
set rs = server.CreateObject ("ADODB.Recordset")
-
-
conn.Close
-
Set conn = Nothing
-
Set rs = Nothing
-
%>
-
-
-
-
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
-
<html>
-
<head>
-
<title>Validate</title>
-
</head>
-
<body>
-
-
-
<%
-
-
-
NOTmyWishArray = array("xpage", "EPQ_Submit")
-
for each i in Request.form
-
isfound = false
-
for j = 0 to Ubound(NOTmyWishArray)
-
if lcase(NOTmyWishArray(j)) = lcase(i) then
-
isfound = true
-
exit for
-
end if
-
next
-
if isfound = false then
-
strBody = Request(i)
-
'response.write Request(i)
-
'response.write i
-
-
rs.Open "SELECT * FROM EvaluationTbl where UserName='"& Session("name") &"' AND ChapterID="&chapid&" AND QuestionID="&i&" "), conn, 1
-
If rs.recordcount = 0 then
-
strSql = "INSERT INTO EvaluationTbl (UserName, ChapterID, QuestionID, UserAnswer) VALUES ('"&Session("name")& "', '"&chapid& "', '"&i&"','"&Request(i)&"')"
-
conn.Execute(strSql)
-
-
rs.Close
-
conn.close
-
set rs=nothing
-
Set conn = nothing
-
-
-
else
-
conn.Execute("UPDATE EvaluationTbl SET UserAnswer ='" &Request(i)& "' WHERE ChapterID = " & chapid & " AND UserName = '" & Session("name") & "'AND QuestionID="&i&" ")
-
-
Response.Redirect("eLearning.asp")
-
close database
-
rs.Close
-
conn.close
-
set rs=nothing
-
Set conn = nothing
-
-
end if
-
-
end if
-
-
-
next
-
-
-
-
%>
-
-
-
-
-
-
</body>
-
</html>
| |
best answer - posted by CroCrew |
Hello deepunarayan,
Sorry about that. Here is a new "Page2.asp" to use instead of the one above that should do everything you’re looking to do (I think). -
<%
-
NumberOfQuestionAsking = Request("xNumberOfQuestionAsking")
-
i = 1
-
Do Until (i = NumberOfQuestionAsking)
-
QuestionsAsked = QuestionsAsked & Request("xQuestion" & i & "KeyID") & ", "
-
i = (i + 1)
-
Loop
-
-
Set adoCon = Server.CreateObject("ADODB.Connection")
-
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("Relative path to your Access database")
-
Set rsAnswers = Server.CreateObject("ADODB.Recordset")
-
-
strSQL = "SELECT * FROM tblQuestion WHERE KeyID IN (" & QuestionsAsked & "0)"
-
rsAnswers.CursorType = 2
-
rsAnswers.LockType = 3
-
rsAnswers.Open strSQL, adoCon
-
-
i = 1
-
-
Function GetAnswer(QuestionKeyID, SubmittedAnswer)
-
rsAnswers.MoveFirst
-
Do Until (rsAnswers.EOF)
-
If (rsAnswers("KeyID").value = QuestionKeyID) Then
-
Response.Write(rsAnswers("Question").value & " ")
-
Response.Write("Answer:" & SubmittedAnswer & " ")
-
-
if (Trim(UCase(SubmittedAnswer)) = Trim(UCase(rsAnswers("Answer").value))) Then
-
Response.Write("<b>Correct</b><br />")
-
Else
-
Response.Write("<b>Incorrect</b><br />")
-
End If
-
Exit Function
-
End IF
-
i = (i + 1)
-
rsAnswers.MoveNext
-
Loop
-
End Function
-
%>
-
<html>
-
<head>
-
<title>Example Page 2</title>
-
</head>
-
<body>
-
<%
-
Do Until (i = NumberOfQuestionAsking)
-
GetAnswer(Request("xQuestion" & i & "KeyID"), Request("x" & i & "Answer"))
-
i = (i + 1)
-
Loop
-
%>
-
</body>
-
</html>
-
Hope this helps,
CroCrew~
|  | Expert | | Join Date: Jan 2008 Location: Michigan
Posts: 338
| | | re: help needed in Form collectin to write for a Data base in ASP
Hello deepunarayan,
Can you elaborate more on your 3rd paragraph? What do mean by “the number of question will be dynamic”?
Is it that you will have a database and within that database there is a table full of questions? So that when a person goes to the “Questions” page only a few random questions from that table are used.
| | Newbie | | Join Date: Sep 2009 Location: India
Posts: 9
| | | re: help needed in Form collectin to write for a Data base in ASP
Thnaks CroCrew,
ya actually till now i have not included that logic but in future it will be there. even the number of question will vary with the user. so the total submited values also vary.
if its fixed then i can easily hard code with the variable and write.. but i wanted to code for n number of question and verify the answers.
| | Newbie | | Join Date: Sep 2009 Location: India
Posts: 9
| | | re: help needed in Form collectin to write for a Data base in ASP
initially i coded for fixed number of question and the code was like this
<!--#include virtual="/adovbs.inc"-->
<!--#include file="connt.asp"-->
<!--#include file="Reload.asp"-->
<%
Set contEP = Server.CreateObject("ADODB.Connection")
contEP.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.MapPath ("eLearning.mdb")
SqlGet1="SELECT * FROM QuestionTbl where ChapterID=11 "
Set RecordsetEP =contEP.Execute(SqlGet1)
TheArray = RecordsetEP.Getrows
contEP.Close
Set contEP = Nothing
Set RecordsetEP = Nothing
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Verify Assessment Evaluatio Process</title>
</head>
<body body bgcolor="#dadfde" oncontextmenu="return false;">
<%
epQ1 = Request.Form("1")
epQ2 = Request.Form("2")
epQ3 = Request.Form("3")
epQ4 = Request.Form("4")
epQ5 = Request.Form("5")
%>
<%
function Score ()
Score=0
if (epQ1 = TheArray(7,0) ) Then
Score = Score+1
end if
if (epQ2 = TheArray(7,1) ) Then
Score = Score+1
end if
if (epQ3 = TheArray(7,2)) Then
Score = Score+1
end if
if (epQ4 = TheArray(7,3) ) Then
Score = Score+1
end if
if (epQ5 = TheArray(7,4) ) Then
Score = Score+1
end if
Session.Contents.Remove("Score")
Session("Score") = Score
end function
%>
<%
DIM mySQL, objRS
mySQL = "SELECT Count(*) AS intTotal FROM QuestionTbl"
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open mySQL, objConn
' Display result
Response.Write ("Youre Score is "& Score & "out of ")
Response.Write objRS("intTotal")
objRS.Close
Set objRS = Nothing
objConn.Close
Set objConn = Nothing
%>
<hr>
<form action="EvaluationDbWirteEP.asp" name="Content1" method="post" target="contentFrame">
<input type="submit" name="Submit_Scores" value="Submit Your Score"/> <input type="reset" name="Reset" />
</form>
</body>
</html>
now i wanted for n number of question as its difficult hard code when questions increase or decrease
|  | Expert | | Join Date: Jan 2008 Location: Michigan
Posts: 338
| | | re: help needed in Form collectin to write for a Data base in ASP
Check back later today I will have something posted for you.
CroCrew~
| | Newbie | | Join Date: Sep 2009 Location: India
Posts: 9
| | | re: help needed in Form collectin to write for a Data base in ASP
it will be really helpful for me.. thanks a lot
|  | Expert | | Join Date: Jan 2008 Location: Michigan
Posts: 338
| | | re: help needed in Form collectin to write for a Data base in ASP
Hello deepunarayan,
Sorry for not getting back to you sooner but, I have an emergency. Below is a start to your solution. I can see that you’re using Microsoft Access as your database so I will try to use that in the solution.
First the database needs a table called “tblQuestion” and within that table there needs to be three fields. First field is called “KeyID” and that needs to have a data type of “AutoNumber”. The tow other fields are called “Question” and “Answer” and they need a data type of “Text”. After creating the table in your database; create about ten records for you testing.
Now create the two files/pages (Page1.asp and Page2.asp) below and give it a try. This should give you a start.
Page1.asp: -
<%
-
NumberOfQuestionAsking = 5
-
-
Set adoCon = Server.CreateObject("ADODB.Connection")
-
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("Relative path to your Access database")
-
Set rsQuestions = Server.CreateObject("ADODB.Recordset")
-
-
strSQL = "SELECT TOP " & NumberOfQuestionAsking & " KeyID, Question FROM tblQuestion ORDER BY rnd(KeyId)"
-
rsQuestions.CursorType = 2
-
rsQuestions.LockType = 3
-
rsQuestions.Open strSQL, adoCon
-
-
i = 1
-
%>
-
<html>
-
<head>
-
<title>Example Page 1</title>
-
</head>
-
<body>
-
<form name="xForm" action="Page2.asp" method="post">
-
<input type="hidden" name="xNumberOfQuestionAsking" value="<%Response.Write(NumberOfQuestionAsking)%>">
-
<%Do Until (rsQuestions.EOF)%>
-
<input type="hidden" name="xQuestion<%=i%>KeyID" value="<%Response.Write(rsQuestions("KeyID").value)%>">
-
<%Response.Write(rsQuestions("Question").value)%> Answer:<input type="text" name="x<%=i%>Answer"><br />
-
<%i = (i + 1)%>
-
<%rsQuestions.MoveNext%>
-
<%Loop%>
-
-
<input type="submit" value="Submit" name="xSubmit">
-
</form>
-
</body>
-
</html>
-
Page2.asp -
<%
-
NumberOfQuestionAsking = Request("xNumberOfQuestionAsking")
-
i = 1
-
Do Until (i = NumberOfQuestionAsking)
-
QuestionsAsked = QuestionsAsked & Request("xQuestion" & i & "KeyID") & ", "
-
i = (i + 1)
-
Loop
-
-
Set adoCon = Server.CreateObject("ADODB.Connection")
-
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("Relative path to your Access database")
-
Set rsAnswers = Server.CreateObject("ADODB.Recordset")
-
-
strSQL = "SELECT * FROM tblQuestion WHERE KeyID IN (" & QuestionsAsked & "0)"
-
rsAnswers.CursorType = 2
-
rsAnswers.LockType = 3
-
rsAnswers.Open strSQL, adoCon
-
-
i = 1
-
%>
-
<html>
-
<head>
-
<title>Example Page 2</title>
-
</head>
-
<body>
-
<%
-
Do Until (rsAnswers.EOF)
-
Response.Write(rsAnswers("Question").value & " ")
-
Response.Write("Answer:" & Request("x" & i & "Answer") & " ")
-
-
if (Trim(UCase(Request("x" & i & "Answer"))) = Trim(UCase(rsAnswers("Answer").value))) Then
-
Response.Write("<b>Correct</b><br />")
-
Else
-
Response.Write("<b>Incorrect</b><br />")
-
End If
-
i = (i + 1)
-
rsAnswers.MoveNext
-
Loop
-
%>
-
</body>
-
</html>
-
I have not tested this out yet so please let me know if something is not working and I will be happy to help out more.
Hope this helps,
CroCrew~
| | Newbie | | Join Date: Sep 2009 Location: India
Posts: 9
| | | re: help needed in Form collectin to write for a Data base in ASP
hay CroCrew, i am really thanksful for your kind reply.. thanks for u r time.
Page1.asp there is no problem. once i submit the form in Page2.asp it is going to a an infinite loop.
i tried to rectify this but failed. in Screen i am able to see few data being to posted. please help in clearing this bug so once i am able view u r output in Page2.asp then it will help me a lot in understanding.
the error is
Error Type:
Active Server Pages, ASP 0113 (0x80004005)
The maximum amount of time for a script to execute was exceeded. You can change this limit by specifying a new value for the property Server.ScriptTimeout or by changing the value in the IIS administration tools.
/Page2.asp
|  | Expert | | Join Date: Jan 2008 Location: Michigan
Posts: 338
| | | re: help needed in Form collectin to write for a Data base in ASP
Sorry,
Change line 4 in "Page2.asp
From: - Do Until (i = NumberOfQuestionAsking)
To: - Do Until (i => NumberOfQuestionAsking)
| | Newbie | | Join Date: Sep 2009 Location: India
Posts: 9
| | | re: help needed in Form collectin to write for a Data base in ASP
i have changed code to
Do Until (i >= NumberOfQuestionAsking)
but still it is in Infinite loop..
the error reads the same
Please try the following:
* Click the Refresh button, or try again later.
* Open the localhost home page, and then look for links to the information you want.
HTTP 500.100 - Internal Server Error - ASP error
Internet Information Services
Technical Information (for support personnel)
* Error Type:
Active Server Pages, ASP 0113 (0x80004005)
The maximum amount of time for a script to execute was exceeded. You can change this limit by specifying a new value for the property Server.ScriptTimeout or by changing the value in the IIS administration tools.
/Page2.asp
* Browser Type:
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
* Page:
POST 180 bytes to /Page2.asp
* POST Data:
xNumberOfQuestionAsking=5&xQuestion1KeyID=7&x1Answ er=&xQuestion2KeyID=4&x2Answer=&xQuestion3KeyID=5& x3Answer=&xQuestion4KeyID=2&x4Answer=&xQuestion5Ke yID=3&x5Answer=&xSubmit=Submit
* Time:
Monday, October 12, 2009, 5:48:42 PM
* More information:
Microsoft Support
|  | Expert | | Join Date: Jan 2008 Location: Michigan
Posts: 338
| | | re: help needed in Form collectin to write for a Data base in ASP
Insert this after line 8 and before line 9: -
Response.Write(QuestionsAsked)
-
Response.End
-
Then tell me what you get when you run it.
|  | Expert | | Join Date: Jan 2008 Location: Michigan
Posts: 338
| | | re: help needed in Form collectin to write for a Data base in ASP
ok, found the problem.
Change line 4 in "Page2.asp
From: - Do Until (i => NumberOfQuestionAsking)
To: - Do Until (i => int(NumberOfQuestionAsking))
Don't do the: -
Response.Write(QuestionsAsked)
-
Response.End
-
| | Newbie | | Join Date: Sep 2009 Location: India
Posts: 9
| | | re: help needed in Form collectin to write for a Data base in ASP
Thanks a lot CroCrew,
Your solution really worked. I thank you for your time and patience.
The solution was very perfect. The only thing I changed is that in Page1.asp as you are selecting the top 5 question in random order and in page2.asp you just select answers with order by “KeyID” and compare with form collection so the result will be incorrect for many right answers.
Now am just using all the questions in DB to display in order so I just selected all (select *) question in Page one and compared there answers in page2.asp with question order itself. But I have to implement the random order. If you have any idea then please share with me. I have one logic that, if we submit the answers as an hidden field in the page1.asp with same KeyID and compare in page2.asp will it work out. I am trying on this.
Sorry for the late reply. I once again thank you for your kind help.
|  | Expert | | Join Date: Jan 2008 Location: Michigan
Posts: 338
| | | re: help needed in Form collectin to write for a Data base in ASP
Hello deepunarayan,
Sorry about that. Here is a new "Page2.asp" to use instead of the one above that should do everything you’re looking to do (I think). -
<%
-
NumberOfQuestionAsking = Request("xNumberOfQuestionAsking")
-
i = 1
-
Do Until (i = NumberOfQuestionAsking)
-
QuestionsAsked = QuestionsAsked & Request("xQuestion" & i & "KeyID") & ", "
-
i = (i + 1)
-
Loop
-
-
Set adoCon = Server.CreateObject("ADODB.Connection")
-
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("Relative path to your Access database")
-
Set rsAnswers = Server.CreateObject("ADODB.Recordset")
-
-
strSQL = "SELECT * FROM tblQuestion WHERE KeyID IN (" & QuestionsAsked & "0)"
-
rsAnswers.CursorType = 2
-
rsAnswers.LockType = 3
-
rsAnswers.Open strSQL, adoCon
-
-
i = 1
-
-
Function GetAnswer(QuestionKeyID, SubmittedAnswer)
-
rsAnswers.MoveFirst
-
Do Until (rsAnswers.EOF)
-
If (rsAnswers("KeyID").value = QuestionKeyID) Then
-
Response.Write(rsAnswers("Question").value & " ")
-
Response.Write("Answer:" & SubmittedAnswer & " ")
-
-
if (Trim(UCase(SubmittedAnswer)) = Trim(UCase(rsAnswers("Answer").value))) Then
-
Response.Write("<b>Correct</b><br />")
-
Else
-
Response.Write("<b>Incorrect</b><br />")
-
End If
-
Exit Function
-
End IF
-
i = (i + 1)
-
rsAnswers.MoveNext
-
Loop
-
End Function
-
%>
-
<html>
-
<head>
-
<title>Example Page 2</title>
-
</head>
-
<body>
-
<%
-
Do Until (i = NumberOfQuestionAsking)
-
GetAnswer(Request("xQuestion" & i & "KeyID"), Request("x" & i & "Answer"))
-
i = (i + 1)
-
Loop
-
%>
-
</body>
-
</html>
-
Hope this helps,
CroCrew~
|  | Similar ASP / Active Server Pages bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|