Click here to Skip to main content
15,867,835 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: User information Pin
Gaurav Dudeja India4-Apr-10 19:40
Gaurav Dudeja India4-Apr-10 19:40 
GeneralRe: User information Pin
BK Komal5-Apr-10 23:39
BK Komal5-Apr-10 23:39 
GeneralRe: User information Pin
Gaurav Dudeja India5-Apr-10 23:46
Gaurav Dudeja India5-Apr-10 23:46 
AnswerRe: User information Pin
meeram3954-Apr-10 20:15
meeram3954-Apr-10 20:15 
GeneralRe: User information Pin
BK Komal5-Apr-10 23:40
BK Komal5-Apr-10 23:40 
Questiondynamic controls Pin
Nilesh Warude4-Apr-10 19:22
Nilesh Warude4-Apr-10 19:22 
AnswerRe: dynamic controls Pin
Gaurav Dudeja India4-Apr-10 19:42
Gaurav Dudeja India4-Apr-10 19:42 
AnswerRe: dynamic controls Pin
Farraj7-Apr-10 0:23
Farraj7-Apr-10 0:23 
ive used the same idea once. so this is the code.. try to use it for ur own

C# code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CurrentExam : System.Web.UI.Page
{
    private int res = 0; // the result of every exam starts with a zero value
    bool[] serialAns; // a serial correct answers
    Exam myE;
    protected void Page_Load(object sender, EventArgs e)
    {
        int num = 1;
        this.SmartNavigation = true;
        string connectionStr = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("DataBase.mdb");


        if (IsPostBack==false)
        {
            this.RdioBtnLst.SelectedIndex = 1;
           
           Session["currentQ"] = 0;// saves in session the current question number
           Session["currentQ"] = num;
           this.lblNum.Text = num.ToString();
           Session["nextQ"] = 0; //saves in session the next question number
           myE = new Exam(connectionStr);
            serialAns=new bool[this.myE.TotalQ]; // creating an array for serialAns that contains all the question 
            for (int j = 0; j < serialAns.Length; j++)
            {
                serialAns[j] = false; // Gives a false value for every question in the array


            }

            Session["myE"] = myE; // saves in session the current exam the user is doing
            Session["serialAns"] = serialAns;// saves in session the serial of answers

            BindExam(0); // shows the exam with the first question in the database (0)

           
        int i = (int)Session["currentQ"];   //"i" takes Theme value from the Session of the current Question

    }
         
         }
    protected int calc(bool[] a)// a method that calculate the number of the correct answers
    {
        int count = 0;
        for (int i = 0; i < a.Length; i++)
        {
            if (a[i]) count++;
        }
        return count;
    }
    protected void btnNext_Click(object sender, EventArgs e)// a method that goes to the next question 
    {
        this.lblbaqmsg.Text = " ";
        myE = (Exam)Session["myE"]; // saves in session the current exam of a user
        int x = (int)Session["currentQ"] + 1; // gives the X the previes value of the current exam+1
        if (x > myE.TotalQ-1)// checks if the next question number has came to the end and disables the next and check button
        {
            x = myE.TotalQ-1;
            this.btnNext.Enabled = false;
          this.btnCheck.Enabled = false;
        }
        Session["currentQ"] = x;
        this.lblNum.Text = x.ToString();
      
        BindExam(x); // shows the exam with the last number of question


    }
    protected void btnBaq_Click(object sender, EventArgs e)// goes back and discrease "1" from the number of the current question 
    {
        
        int x = (int)Session["currentQ"] - 1;
        int num = x;
        this.lblNum.Text = num.ToString();
        if (x < 0)//checks if the user has came to the first question and disables the back button
        {
            x = 0;

            this.lblbaqmsg.Text = "אין אפשרות להשלים את הפעולה";
        }
        
        Session["currentQ"] = x;

        BindExam(x);// shows the table of the exam with the last number of question that is saved in the session
        

    }
    protected void BindExam(int i)// this method bind the exam and shows the questions
    {
        myE = (Exam)Session["myE"];
        this.lblQues.Text = myE.TblExam.Rows[i]["ques"].ToString();
        this.lblAns1.Text = myE.TblExam.Rows[i]["ans1"].ToString();
        this.lblAns2.Text = myE.TblExam.Rows[i]["ans2"].ToString();
        this.lblAns3.Text = myE.TblExam.Rows[i]["ans3"].ToString();
        this.lblAns4.Text = myE.TblExam.Rows[i]["ans4"].ToString();
        this.lblCorrectAns.Text = myE.TblExam.Rows[i]["correctAns"].ToString();
    }

    protected void Other1_Load(object sender, EventArgs e)
    {

    }
    protected void btnCheck_Click(object sender, EventArgs e)// this method check if the answer is correct and adds it to the correct answers array
    {
        myE = (Exam)Session["myE"];
        int x = (int)Session["currentQ"];
        string stCorrect = myE.TblExam.Rows[x]["correctAns"].ToString();
        int y = int.Parse(this.RdioBtnLst.SelectedValue);// saves the selected value in "Y"
        string stAns="";
        if (y==1)// if value is 1 then the answer (stAns) =ans1 (in the db)
        {
            stAns = this.myE.TblExam.Rows[x]["ans1"].ToString();
        }
        else if (y == 2)// if value is 2 then the answer (stAns) =ans2 (in the db)
        {
            stAns = this.myE.TblExam.Rows[x]["ans2"].ToString();
        }
        else if (y == 3)// if value is 3 then the answer (stAns) =ans3 (in the db)
        {
            stAns = this.myE.TblExam.Rows[x]["ans3"].ToString();
        }
        else if (y == 4)// if value is 4 then the answer (stAns) =ans4 (in the db)
        {
            stAns = this.myE.TblExam.Rows[x]["ans4"].ToString();
        }
        // if the selected value text is the same as the correct ans
        if (stCorrect.CompareTo(stAns)==0)
        {
            this.serialAns = (bool[])Session["serialAns"];// gives the serialAns the values that the user answerd them  and that are saved in the session
            this.serialAns[x] = true;
            Session["serialAns"] = this.serialAns;
            
        }


         
    }
    protected void btnFinish_Click(object sender, EventArgs e)// finish the exam and add it
    {
        string connectionStr = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("DataBase.mdb");

        this.serialAns = (bool[])Session["serialAns"];
        int c = this.calc(this.serialAns);
        res = (int)Session["c"];
        //insert into database
        Exam exam = new Exam(connectionStr);

        string stuser = (string)Session["username"];
        exam.InsertUsersGrades(stuser,res,DateTime.Now);
        //

        //go to report
        Session["c"] = c;
        Response.Redirect("Result.aspx");

    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}


this is the aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CurrentExam.aspx.cs" Inherits="CurrentExam" %>

<%@ Register Src="Controls/Other.ascx" TagName="Other" TagPrefix="uc1" %>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body bgcolor="#69869B">
    <form id="form1" runat="server">
    <div>
        &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;
       <table align="right" style="z-index: 108; left: 0px; position: absolute; top: 0px"><tr align="right"><td align="right" style="width: 3px"><asp:Label ID="lblQues" runat="server" align="right" Font-Bold="True" Font-Size="Large"
            Font-Underline="True" Style="z-index: 100; left: 224px; position: absolute; top: 440px"
            Width="536px"></asp:Label></td></tr>
      <tr align="right"><td align="right" style="width: 3px">  <asp:Label ID="lblAns4" runat="server" Style="z-index: 101; left: 320px; position: absolute;
            top: 640px" Text="Label" Width="400px" Height="32px"></asp:Label></td></tr>
   <tr align="right"><td align="right" style="width: 3px">     <asp:Label ID="lblAns3" runat="server" Style="z-index: 102; left: 320px; position: absolute;
            top: 592px" Text="Label" Width="400px" Height="32px"></asp:Label></td></tr>
   <tr align="right"><td align="right" style="width: 3px">     <asp:Label ID="lblAns2" runat="server" Style="z-index: 103; left: 320px; position: absolute;
            top: 544px" Text="Label" Width="400px" Height="32px"></asp:Label></td></tr>
    <tr align="right"><td align="right" style="width: 3px">    <asp:Label ID="lblAns1" runat="server" Font-Bold="False" Style="z-index: 104; left: 320px;
            position: absolute; top: 496px" Text="Label" Width="400px" Height="32px"></asp:Label></td></tr>
   <tr align="right"><td align="right" style="width: 3px">     <asp:Button ID="btnNext" runat="server" Style="z-index: 105; left: 384px; position: absolute;
            top: 768px" Text="<--הבא" Width="56px" OnClick="btnNext_Click" /></td></tr>
   <tr align="right"><td align="right" style="width: 3px">     <asp:Button ID="btnBaq" runat="server" Style="z-index: 106; left: 616px; position: absolute;
            top: 769px" Text="חזרה-->" OnClick="btnBaq_Click" Width="48px" /></td></tr>
     <tr align="right"><td align="right" style="width: 3px">   <asp:Label ID="lblCorrectAns" runat="server" Style="z-index: 108; left: 320px; position: absolute;
            top: 696px" Text="Label" Visible="False" Width="400px" Height="32px"></asp:Label></td></tr>
      
    </table> 
        <asp:RadioButtonList ID="RdioBtnLstans" runat="server" Height="184px" Style="z-index: 100;
            left: 744px; position: absolute; top: 488px">
            <asp:ListItem Value="1">(1)</asp:ListItem>
            <asp:ListItem Value="2">(2)</asp:ListItem>
            <asp:ListItem Value="3">(3)</asp:ListItem>
            <asp:ListItem Value="4">(4)</asp:ListItem>
        </asp:RadioButtonList>
        <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="X-Large" Style="z-index: 101;
            left: 480px; position: absolute; top: 384px" Text="...בהצלחה"></asp:Label>
        &nbsp;
        <asp:Button ID="btnCheck" runat="server" OnClick="btnCheck_Click" Style="z-index: 102;
            left: 480px; position: absolute; top: 744px" Text="אשר תשובה" />
        &nbsp; &nbsp;&nbsp; &nbsp;
        <asp:Label ID="lblbaqmsg" runat="server" Font-Bold="True" ForeColor="Red" Style="z-index: 103;
            left: 704px; position: absolute; top: 800px" Width="184px"></asp:Label>
        <asp:Label ID="Label2" runat="server" Height="24px" Style="z-index: 104; left: 792px;
            position: absolute; top: 395px" Text="שאלה מספר  " Width="80px"></asp:Label>
        <asp:Button ID="btnFinish" runat="server" OnClick="btnFinish_Click" Style="left: 376px;
            position: relative; top: 776px; z-index: 105;" Text="סיים בחינה" Width="90px" />
        <asp:Label ID="lblNum" runat="server" Height="24px" Style="z-index: 109; left: 760px;
            position: absolute; top: 392px" Width="16px"></asp:Label>
        &nbsp;<uc1:Other
                ID="Other1" runat="server" />
        <asp:RadioButtonList ID="RdioBtnLst" runat="server" Height="184px" Style="z-index: 107;
            left: 768px; position: absolute; top: 488px">
            <asp:ListItem Value="1">(1)</asp:ListItem>
            <asp:ListItem Value="2">(2)</asp:ListItem>
            <asp:ListItem Value="3">(3)</asp:ListItem>
            <asp:ListItem Value="4">(4)</asp:ListItem>
        </asp:RadioButtonList>
    </div>
    </form>
</body>
</html>


this is the Exam class in C#

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

/// <summary>
///This Class is to run everything that depends on the system Exams
/// </summary>
    public class Exam
	{
        private string connectionStr;
		private int totalQ; //number of questions
		private DataTable tblExam;// table of the exam
        private bool result;// saves the results of the user in the current exam

		
        public int TotalQ
         {
            get { return this.totalQ; }
         }

        public bool Result
        {
            get { return this.result; }
            set { this.result = value; }
        }
		
		public DataTable TblExam
		{
			get {return this.tblExam;}	
		}

    public Exam(string connectionStr)
    {

        this.totalQ = 30;// Can be changed to any number of questions
        this.connectionStr = connectionStr;
        string sql = "select ques,ans1,ans2,ans3,ans4,correctAns from questions";
        OleDbConnection conn = new OleDbConnection(connectionStr);
        OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);


      
        //Creating columns for the data table
        this.tblExam = new DataTable();
        DataColumn c1 = new DataColumn("ques");
        this.tblExam.Columns.Add(c1);
        DataColumn c2 = new DataColumn("ans1");
        this.tblExam.Columns.Add(c2);
        DataColumn c3 = new DataColumn("ans2");
        this.tblExam.Columns.Add(c3);
        DataColumn c4 = new DataColumn("ans3");
        this.tblExam.Columns.Add(c4);
        DataColumn c5 = new DataColumn("ans4");
        this.tblExam.Columns.Add(c5);
        DataColumn c6 = new DataColumn("correctAns");
        this.tblExam.Columns.Add(c6);

        //Creating Rows for the data table
        for (int i = 0; i < this.totalQ; i++)
        {
            DataRow row = this.tblExam.NewRow();

            row["ques"] = ds.Tables[0].Rows[i]["ques"];
            row["ans1"] = ds.Tables[0].Rows[i]["ans1"];
            row["ans2"] = ds.Tables[0].Rows[i]["ans2"];
            row["ans3"] = ds.Tables[0].Rows[i]["ans3"];
            row["ans4"] = ds.Tables[0].Rows[i]["ans4"];
            row["correctAns"] = ds.Tables[0].Rows[i]["correctAns"];
            this.tblExam.Rows.Add(row);


        }
    }
        //Insert results with this method to database
		public void InsertUsersGrades(string username,int grade,DateTime time)
		{
			string queryStr="INSERT INTO exams_results values('"+username+"','";
			queryStr+=grade+"','";
			queryStr+=time+"')";
			OleDbConnection connectObj=new OleDbConnection(this.connectionStr);
			OleDbCommand myCommandObj=new OleDbCommand(queryStr,connectObj);
			connectObj.Open();
			myCommandObj.ExecuteNonQuery();
			connectObj.Close();


		
		}

        public int getAvg(string user)
        {
            string st;
            int sum = 0;
            Page p = new Page();
            string connectionStr = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + p.Server.MapPath("DataBase.mdb");
            string sqlStr = "Select grade FROM memResults WHERE username='" + user + "'";
            OleDbDataAdapter daObj = new OleDbDataAdapter(sqlStr, connectionStr);
            DataSet dsObj = new DataSet();
            daObj.Fill(dsObj);
            DataTable dataTable = dsObj.Tables[0];
            int count = dsObj.Tables[0].Rows.Count;
            foreach (DataRow r in dsObj.Tables[0].Rows)
            {
                st = r["grade"].ToString();
                sum += int.Parse(st);


            }

            if (sum == null) return -1;


            else return sum / count;


        }

  }



you have to create a table of questions

according to my code it is:
ques
ans1
ans2
ans3
ans4
correctAns
Questiondynamic controls Pin
Nilesh Warude4-Apr-10 19:21
Nilesh Warude4-Apr-10 19:21 
QuestionHow to use SCORM component in asp.net? Pin
ankiasd4-Apr-10 18:33
ankiasd4-Apr-10 18:33 
QuestionCalender and text box Pin
future38394-Apr-10 18:01
future38394-Apr-10 18:01 
AnswerRe: Calender and text box Pin
test-094-Apr-10 18:32
test-094-Apr-10 18:32 
AnswerRe: Calender and text box Pin
Gaurav Dudeja India4-Apr-10 19:43
Gaurav Dudeja India4-Apr-10 19:43 
GeneralRe: Calender and text box Pin
future38394-Apr-10 19:48
future38394-Apr-10 19:48 
GeneralRe: Calender and text box Pin
Gaurav Dudeja India4-Apr-10 19:52
Gaurav Dudeja India4-Apr-10 19:52 
GeneralRe: Calender and text box Pin
future38395-Apr-10 1:03
future38395-Apr-10 1:03 
GeneralRe: Calender and text box Pin
Gaurav Dudeja India5-Apr-10 1:08
Gaurav Dudeja India5-Apr-10 1:08 
AnswerRe: Calender and text box Pin
T M Gray5-Apr-10 10:11
T M Gray5-Apr-10 10:11 
AnswerRe: Calender and text box Pin
Mohan H Prajapati5-Apr-10 23:42
Mohan H Prajapati5-Apr-10 23:42 
QuestionAccessing SOAP message response elements Pin
parakyar4-Apr-10 8:35
parakyar4-Apr-10 8:35 
QuestionLearning Test Driven Development in VS 2008 - ASP.NET Pin
Nadia Monalisa4-Apr-10 2:00
Nadia Monalisa4-Apr-10 2:00 
AnswerRe: Learning Test Driven Development in VS 2008 - ASP.NET Pin
N a v a n e e t h4-Apr-10 2:40
N a v a n e e t h4-Apr-10 2:40 
GeneralRe: Learning Test Driven Development in VS 2008 - ASP.NET Pin
Nadia Monalisa4-Apr-10 2:54
Nadia Monalisa4-Apr-10 2:54 
GeneralRe: Learning Test Driven Development in VS 2008 - ASP.NET Pin
N a v a n e e t h4-Apr-10 3:55
N a v a n e e t h4-Apr-10 3:55 
GeneralRe: Learning Test Driven Development in VS 2008 - ASP.NET Pin
Nadia Monalisa4-Apr-10 15:06
Nadia Monalisa4-Apr-10 15:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.