Using asp.net to construct online examination system

Source: Internet
Author: User
Tags tostring trim access database
asp.net with the popularization of computer network, the B/s online examination system based on database is widely used, and the ASP.net (C #) +sql server (or access) is illustrated to illustrate the method of developing online examination system.

First, the design of the database:

Establish the database Netexam, add examinee Information table Stuinfo in the library, establish the following fields: Test number Examid (c) (primary key), candidate name Name (c), whether to login for examination Logyn (c), score score (c), randomly generated question answer MCA (c) ( Note: Here take multiple-choice as an example, single selection, the same as the judgment question. Add Multiple-choice item Bank table MC, establish the following fields: Title question (c), four selected Choice1 (c), Choice2 (c), Choice3 (c), Choice4 (c), answer Answer (c) (Note: Multiple-choice answers with 0 for not selected, 1 for Choice , such as selecting Abd in 1101.

Second, examinee login:

Here to insert the candidate information into the table stuinfo, here to prevent candidates to repeat the login! To specify a unique test number, and the Test number field Examid (c) Set as the primary key, when repeated login, with catch catch error, give the corresponding prompts, part of the code is as follows (Login.aspx):

private void Butok_click (object sender, System.EventArgs e)
{
...
SqlConnection stuconn=new SqlConnection ("Data source=localhost;integrated Security=sspi;
Initial Catalog=netexam ");
SqlCommand Logincmd=stuconn.createcommand (); Different database connections can be selected depending on the situation
...
logincmd.commandtext= "Insert into Stuinfo (examid,name) VALUES (' +txtid.text.trim () +" ', ' "+txtname.text.trim () +") " ;
Insert the test number and name into the corresponding field, where Txtid,txtname is the text box for entering the test number and the name, respectively.
Try
{
...
session["Id"]=txtid.text.trim ();
Stuconn.open ();
Logincmd.executereader ();
Response.Redirect ("test.aspx");
}
catch (Exception)//catch the corresponding error
{
Response.Write ("<script language=\" javascript\ ">" + "\ n");
Response.Write ("alert (\") can not repeat login, or test number, name, password is wrong! \ ")" + "\n</script>");
}
...
}


Third, the question generation:

1, prevent candidates to refresh the page:

Because the test page will be loaded from the database randomly selected questions, so should prevent candidates to refresh the face page (refresh the page will regenerate new questions). The method is to set the default value of the Logyn field in table Stuinfo to 0, load the question after set to 1, the test paper score set to 2, load the question page to check accordingly to prevent the page from refreshing, the code is as follows (Test.aspx):

private void Page_Load (object sender, System.EventArgs e)
{
if (! IsPostBack)
{
...
questcmd.commandtext= "Select Logyn from Stuinfo where examid= '" + session["Id"]. ToString () + "'";
Questconn.open ();
SqlDataReader Questrd=questcmd.executereader ();
Questrd.read ();
if (questrd["Logyn"]. ToString (). Trim (). Equals ("1") | | questrd["Logyn"]. ToString (). Trim (). Equals ("2"))
Determine if the question has been loaded or has been graded
{
...
Response.Write ("<script language=\" javascript\ ">" + "\ n");
Response.Write ("alert (\") cannot be refreshed! Contact your administrator to sign in again. \ ")" + "\n</script>");
...
}
Else
{
...
questcmd.commandtext= "Update stuinfo set logyn= ' 1 '"; The test has been successfully loaded
Questcmd.executereader ();
...
}
}
...
}
2. Randomly generated questions:

The key of online examination system is the random generation of questions, that is, for different computer access system, different questions will be randomly extracted from the question bank.

Here we can use the SQL statement "SELECT top N * out MC ORDER by NEWID ()" To randomly extract n records from the question bank, where NEWID () generates uniqueidentifier values (if the Access database uses "select top n * from MC ORDER by RND (ID), where ID is AutoNumber field).

Place a panel container control on the Test.aspx page to dynamically generate the control that is bound to the question, and write a randomly generated question answer from the table MC to the MCA field in the Table Stuinfo table, as follows (test.aspx):

private void Page_Load (object sender, System.EventArgs e)
{
if (! IsPostBack)
{
...
questcmd.commandtext= "SELECT top * to MC ORDER by NEWID ()";//to randomly generate 10-way title case
Questconn.open ();
Questrd=questcmd.executereader ();
while (Questrd.read ())
{
Literal littxt=new Literal ();
Literal litbl=new Literal ();
CheckBoxList chkmc=new CheckBoxList ();
Chkmc.id= "CHKMC" +i.tostring ();
Littxt.text=i.tostring () + "," +server.htmlencode (questrd["question"). ToString ()) + "<BR><Blockquote>";
litbl.text= "</Blockquote>";
chkmc.font.size=11;
for (int j=1;j<=4;j++)
{
CHKMC.ITEMS.ADD (Server.HTMLEncode (questrd["Choice" +j.tostring ()). ToString ()));
CHKMC.ITEMS[J-1]. Value=j.tostring ();
}
mcstr+=questrd["Answer"]. ToString (). Trim (); MCSTR is a string variable that stores the answers to randomly generated questions
MYPANEL.CONTROLS.ADD (Littxt);
MYPANEL.CONTROLS.ADD (CHKMC);
MYPANEL.CONTROLS.ADD (LITBL);
i++;
}
...
questcmd.commandtext= "Update stuinfo set mca= ' +mcstr+" ' where examid= ' "" +session["Id"].   ToString () + "'"; Write randomly generated question answers to data tables
Questconn.open ();
Questrd=questcmd.executereader ();
...
}
...
}

Four, the grading of handing in:

When the user clicks on the paper button, the user should be the answer to the test results and answers to the match, and give the corresponding points to write the data table, and finally the table Stuinfo in the Logyn field set to 2, showing the test scores, the code is as follows (Test.aspx):

private void Butsend_click (object sender, System.EventArgs e)
{
...
for (int i=1;i<=10;i++)//due to randomly generated 10 questions, so Loop 10 times
{
for (int j=0;j<4;j++)
if (request.form["CHKMC" +i.tostring () + ":" +j.tostring ()]!=null)
mcs+= "1"; The MCS is the string variable that stores the candidate's chosen answer, which has been selected as "1", and "0" is not selected
Else
mcs+= "0";
}
...
questcmd.commandtext= "Select MCA from Stuinfo where examid= '" +session["Id"]. ToString () + "'";
Questconn.open ();
SqlDataReader Questrd=questcmd.executereader ();
Questrd.read ();
int stuscore=0; The variable that stores the score
for (i=0;i<10;i+=4)
{
if (questrd["MCA"]. ToString (). Substring (i,4). Equals (MCS. Substring (i,4))
stuscore+=2; Match four characters at a time from the field MCA and the MCS, such as equal plus 2 points
}
...
questcmd.commandtext= "Update stuinfo set score=" +stuscore.tostring () + ", logyn= ' 2 ' where examid= '" + session["Id"]. ToString () + "' and logyn= ' 1 '"; Set the marking Mark
Questrd=questcmd.executereader ();
...
Response.Redirect ("score.aspx"); Show Test Score Page
...
}
Limited to space, here only the realization of the online exam system of several points, the reader can be based on their own needs to further improve the corresponding data validation, background management and interface design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.