473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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 2118
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

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

Similar topics

4
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like...
1
by: Robert V | last post by:
Hi all, I could use some help programming on of my Perl script to handle different submit buttons within the same form. Here is what I have so far. A user goes to a Web form and inputs some data...
6
by: Code4u | last post by:
I need to design data storage classes and operators for an image processing system that must support a range of basic data types of different lengths i.e. float, int, char, double. I have a...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
6
by: xenophon | last post by:
I have a grid with checkboxes in it. When a checkbox is un/checked, I want to set a true-false value in an array. Then on PostBack I can work with that array. I know I need to use...
7
by: c#2006user | last post by:
Hi everyone. i know there is a lot of code and ive reduced it as much as i could, its a bit much to ask but i am really stuck! i've been at this for a week! this is converted from vb to c# and...
5
by: Ming Yeung | last post by:
I was wondering if .NET had the equivalent of Frames, Global Objects, and most importantly DataModules like in Delphi?
4
by: Brad Isaacs | last post by:
I am working with ASP.NET 2.0 and using an SQL Server 2000 database. I am using Visual Studio 2005 and developing on my Local machine. I am working with Login controls ASP.Configuration, I...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.