468,510 Members | 1,590 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,510 developers. It's quick & easy.

help needed in Form collectin to write for a Data base in ASP

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.
Expand|Select|Wrap|Line Numbers
  1.  <!--#include file="Reload.asp"-->
  2.  
  3.  
  4.  
  5. <% 
  6.  
  7.  
  8. 'WriteScore= Session("Score") 
  9.  
  10. usname =Session("name")
  11. dim chapid
  12. chapid=11
  13.  
  14. Set conn = Server.CreateObject("ADODB.Connection")
  15.  
  16. 'Connect to the database
  17. strMDBpath = Server.MapPath("eLearning.mdb")
  18. conn.open "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & strMDBPath
  19. set rs = server.CreateObject ("ADODB.Recordset")
  20.  
  21.  conn.Close
  22.  Set conn = Nothing
  23.  Set rs = Nothing
  24.  %>
  25.  
  26.  
  27.  
  28.  
  29. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  30.  
  31. <html>
  32. <head>
  33. <title>Validate</title>
  34. </head>
  35. <body>
  36.  
  37.  
  38. <%
  39.  
  40.  
  41. NOTmyWishArray = array("xpage", "EPQ_Submit")
  42. for each i in Request.form
  43. isfound = false
  44.         for j = 0 to Ubound(NOTmyWishArray)
  45.                 if lcase(NOTmyWishArray(j)) = lcase(i) then
  46.                         isfound = true
  47.                         exit for
  48.                 end if
  49.         next
  50.         if isfound = false then           
  51.         strBody = Request(i)                    
  52.                     'response.write Request(i)    
  53.                     'response.write i 
  54.  
  55.             rs.Open "SELECT * FROM EvaluationTbl where UserName='"& Session("name") &"' AND ChapterID="&chapid&" AND QuestionID="&i&" "), conn, 1 
  56.         If rs.recordcount = 0 then
  57.          strSql = "INSERT INTO EvaluationTbl (UserName, ChapterID, QuestionID, UserAnswer) VALUES ('"&Session("name")& "', '"&chapid& "', '"&i&"','"&Request(i)&"')"
  58.          conn.Execute(strSql)
  59.  
  60.         rs.Close
  61.     conn.close
  62.     set rs=nothing
  63.     Set conn = nothing
  64.  
  65.  
  66.     else
  67.     conn.Execute("UPDATE EvaluationTbl SET UserAnswer ='" &Request(i)& "' WHERE ChapterID = " & chapid & " AND UserName = '" & Session("name") & "'AND QuestionID="&i&" ")
  68.  
  69.     Response.Redirect("eLearning.asp")
  70.     close database
  71.     rs.Close
  72.     conn.close
  73.     set rs=nothing
  74.     Set conn = nothing
  75.  
  76.     end if
  77.  
  78.         end if                            
  79.  
  80.  
  81.    next
  82.  
  83.  
  84.  
  85. %>
  86.  
  87.  
  88.  
  89.  
  90.  
  91. </body>
  92. </html>
Oct 8 '09 #1

✓ answered 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).

Expand|Select|Wrap|Line Numbers
  1. <%  
  2.     NumberOfQuestionAsking = Request("xNumberOfQuestionAsking")  
  3.     i = 1 
  4.     Do Until (i = NumberOfQuestionAsking) 
  5.         QuestionsAsked = QuestionsAsked & Request("xQuestion" & i & "KeyID") & ", " 
  6.         i = (i + 1) 
  7.     Loop 
  8.  
  9.     Set adoCon = Server.CreateObject("ADODB.Connection")  
  10.     adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("Relative path to your Access database")   
  11.     Set rsAnswers = Server.CreateObject("ADODB.Recordset")  
  12.  
  13.     strSQL = "SELECT * FROM tblQuestion WHERE KeyID IN (" & QuestionsAsked & "0)" 
  14.     rsAnswers.CursorType = 2  
  15.     rsAnswers.LockType = 3  
  16.     rsAnswers.Open strSQL, adoCon  
  17.  
  18.     i = 1 
  19.  
  20.     Function GetAnswer(QuestionKeyID, SubmittedAnswer)
  21.         rsAnswers.MoveFirst
  22.         Do Until (rsAnswers.EOF) 
  23.             If (rsAnswers("KeyID").value = QuestionKeyID) Then
  24.                 Response.Write(rsAnswers("Question").value & "&nbsp;&nbsp;&nbsp;") 
  25.                 Response.Write("Answer:" & SubmittedAnswer & "&nbsp;&nbsp;&nbsp;") 
  26.  
  27.                 if (Trim(UCase(SubmittedAnswer)) = Trim(UCase(rsAnswers("Answer").value))) Then 
  28.                     Response.Write("<b>Correct</b><br />") 
  29.                 Else 
  30.                     Response.Write("<b>Incorrect</b><br />") 
  31.                 End If 
  32.                 Exit Function
  33.             End IF
  34.             i = (i + 1) 
  35.         rsAnswers.MoveNext 
  36.         Loop 
  37.     End Function
  38. %>  
  39. <html> 
  40.     <head> 
  41.         <title>Example Page 2</title> 
  42.     </head> 
  43.     <body> 
  44.         <% 
  45.             Do Until (i = NumberOfQuestionAsking) 
  46.                 GetAnswer(Request("xQuestion" & i & "KeyID"), Request("x" & i & "Answer"))
  47.                 i = (i + 1) 
  48.             Loop 
  49.         %>     
  50.     </body> 
  51. </html>
  52.  
Hope this helps,
CroCrew~

13 1902
CroCrew
564 Expert 512MB
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.
Oct 8 '09 #2
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.
Oct 8 '09 #3
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
Oct 8 '09 #4
CroCrew
564 Expert 512MB
Check back later today I will have something posted for you.

CroCrew~
Oct 8 '09 #5
it will be really helpful for me.. thanks a lot
Oct 8 '09 #6
CroCrew
564 Expert 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. <% 
  2.     NumberOfQuestionAsking = 5 
  3.  
  4.     Set adoCon = Server.CreateObject("ADODB.Connection") 
  5.     adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("Relative path to your Access database")  
  6.     Set rsQuestions = Server.CreateObject("ADODB.Recordset") 
  7.  
  8.     strSQL = "SELECT TOP " & NumberOfQuestionAsking & " KeyID, Question FROM tblQuestion ORDER BY rnd(KeyId)" 
  9.     rsQuestions.CursorType = 2 
  10.     rsQuestions.LockType = 3 
  11.     rsQuestions.Open strSQL, adoCon 
  12.  
  13.     i = 1
  14. %> 
  15. <html>
  16.     <head>
  17.         <title>Example Page 1</title>
  18.     </head>
  19.     <body>
  20.         <form name="xForm" action="Page2.asp" method="post">
  21.             <input type="hidden" name="xNumberOfQuestionAsking" value="<%Response.Write(NumberOfQuestionAsking)%>">
  22.             <%Do Until (rsQuestions.EOF)%>
  23.                 <input type="hidden" name="xQuestion<%=i%>KeyID" value="<%Response.Write(rsQuestions("KeyID").value)%>">
  24.                 <%Response.Write(rsQuestions("Question").value)%>&nbsp;&nbsp;&nbsp;Answer:<input type="text" name="x<%=i%>Answer"><br />
  25.                 <%i = (i + 1)%> 
  26.                 <%rsQuestions.MoveNext%>
  27.             <%Loop%>    
  28.  
  29.             <input type="submit" value="Submit" name="xSubmit">
  30.         </form>
  31.     </body>
  32. </html>
  33.  

Page2.asp
Expand|Select|Wrap|Line Numbers
  1. <% 
  2.     NumberOfQuestionAsking = Request("xNumberOfQuestionAsking") 
  3.     i = 1
  4.     Do Until (i = NumberOfQuestionAsking)
  5.         QuestionsAsked = QuestionsAsked & Request("xQuestion" & i & "KeyID") & ", "
  6.         i = (i + 1)
  7.     Loop
  8.  
  9.     Set adoCon = Server.CreateObject("ADODB.Connection") 
  10.     adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("Relative path to your Access database")  
  11.     Set rsAnswers = Server.CreateObject("ADODB.Recordset") 
  12.  
  13.     strSQL = "SELECT * FROM tblQuestion WHERE KeyID IN (" & QuestionsAsked & "0)"
  14.     rsAnswers.CursorType = 2 
  15.     rsAnswers.LockType = 3 
  16.     rsAnswers.Open strSQL, adoCon 
  17.  
  18.     i = 1
  19. %> 
  20. <html>
  21.     <head>
  22.         <title>Example Page 2</title>
  23.     </head>
  24.     <body>
  25.         <%
  26.             Do Until (rsAnswers.EOF)
  27.                 Response.Write(rsAnswers("Question").value & "&nbsp;&nbsp;&nbsp;")
  28.                 Response.Write("Answer:" & Request("x" & i & "Answer") & "&nbsp;&nbsp;&nbsp;")
  29.  
  30.                 if (Trim(UCase(Request("x" & i & "Answer"))) = Trim(UCase(rsAnswers("Answer").value))) Then
  31.                     Response.Write("<b>Correct</b><br />")
  32.                 Else
  33.                     Response.Write("<b>Incorrect</b><br />")
  34.                 End If
  35.                 i = (i + 1)
  36.             rsAnswers.MoveNext
  37.             Loop
  38.         %>    
  39.     </body>
  40. </html>
  41.  
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~
Oct 9 '09 #7
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
Oct 12 '09 #8
CroCrew
564 Expert 512MB
Sorry,

Change line 4 in "Page2.asp

From:
Expand|Select|Wrap|Line Numbers
  1. Do Until (i = NumberOfQuestionAsking) 
To:
Expand|Select|Wrap|Line Numbers
  1. Do Until (i => NumberOfQuestionAsking) 
Oct 12 '09 #9
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
Oct 12 '09 #10
CroCrew
564 Expert 512MB
Insert this after line 8 and before line 9:

Expand|Select|Wrap|Line Numbers
  1. Response.Write(QuestionsAsked)
  2. Response.End
  3.  
Then tell me what you get when you run it.
Oct 12 '09 #11
CroCrew
564 Expert 512MB
ok, found the problem.

Change line 4 in "Page2.asp

From:
Expand|Select|Wrap|Line Numbers
  1. Do Until (i => NumberOfQuestionAsking)
To:
Expand|Select|Wrap|Line Numbers
  1. Do Until (i => int(NumberOfQuestionAsking))

Don't do the:
Expand|Select|Wrap|Line Numbers
  1. Response.Write(QuestionsAsked) 
  2. Response.End 
  3.  
Oct 12 '09 #12
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.
Oct 15 '09 #13
CroCrew
564 Expert 512MB
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).

Expand|Select|Wrap|Line Numbers
  1. <%  
  2.     NumberOfQuestionAsking = Request("xNumberOfQuestionAsking")  
  3.     i = 1 
  4.     Do Until (i = NumberOfQuestionAsking) 
  5.         QuestionsAsked = QuestionsAsked & Request("xQuestion" & i & "KeyID") & ", " 
  6.         i = (i + 1) 
  7.     Loop 
  8.  
  9.     Set adoCon = Server.CreateObject("ADODB.Connection")  
  10.     adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("Relative path to your Access database")   
  11.     Set rsAnswers = Server.CreateObject("ADODB.Recordset")  
  12.  
  13.     strSQL = "SELECT * FROM tblQuestion WHERE KeyID IN (" & QuestionsAsked & "0)" 
  14.     rsAnswers.CursorType = 2  
  15.     rsAnswers.LockType = 3  
  16.     rsAnswers.Open strSQL, adoCon  
  17.  
  18.     i = 1 
  19.  
  20.     Function GetAnswer(QuestionKeyID, SubmittedAnswer)
  21.         rsAnswers.MoveFirst
  22.         Do Until (rsAnswers.EOF) 
  23.             If (rsAnswers("KeyID").value = QuestionKeyID) Then
  24.                 Response.Write(rsAnswers("Question").value & "&nbsp;&nbsp;&nbsp;") 
  25.                 Response.Write("Answer:" & SubmittedAnswer & "&nbsp;&nbsp;&nbsp;") 
  26.  
  27.                 if (Trim(UCase(SubmittedAnswer)) = Trim(UCase(rsAnswers("Answer").value))) Then 
  28.                     Response.Write("<b>Correct</b><br />") 
  29.                 Else 
  30.                     Response.Write("<b>Incorrect</b><br />") 
  31.                 End If 
  32.                 Exit Function
  33.             End IF
  34.             i = (i + 1) 
  35.         rsAnswers.MoveNext 
  36.         Loop 
  37.     End Function
  38. %>  
  39. <html> 
  40.     <head> 
  41.         <title>Example Page 2</title> 
  42.     </head> 
  43.     <body> 
  44.         <% 
  45.             Do Until (i = NumberOfQuestionAsking) 
  46.                 GetAnswer(Request("xQuestion" & i & "KeyID"), Request("x" & i & "Answer"))
  47.                 i = (i + 1) 
  48.             Loop 
  49.         %>     
  50.     </body> 
  51. </html>
  52.  
Hope this helps,
CroCrew~
Oct 15 '09 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by PHPkemon | last post: by
1 post views Thread by Robert V | last post: by
6 posts views Thread by xenophon | last post: by
4 posts views Thread by Brad Isaacs | last post: by
53 posts views Thread by souporpower | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.