Click here to Skip to main content
15,895,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace student_management.studpro
{
    public partial class WebForm6 : System.Web.UI.Page
    {
        public static SqlConnection sqlconn;
        protected string PostBackStr;

        protected void Page_Load(object sender, EventArgs e)
        {
            sqlconn = new SqlConnection(ConfigurationManager.ConnectionStrings["aloproConnectionString"].ConnectionString);
            PostBackStr = Page.ClientScript.GetPostBackEventReference(this, "time");
            if (IsPostBack)
            {
                string eventArg = Request["__EVENTARGUMENT"].ToString();
                if (eventArg == "time")
                {
                    getNextQuestion();
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            //txtName.Visible = false;
            Button1.Visible = false;
            Panel1.Visible = true;
            string score = txtscore.Text;
            lblscore.Text = "Score : " + Convert.ToString(score);
            Session["counter"] = "1";
            Random rnd = new Random();
            int i = rnd.Next(1, 10);//Here specify your starting slno of question table and ending no.
            //lblQuestion.Text = i.ToString();
            getQuestion(i);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            getNextQuestion();
        }
        public void getQuestion(int no)
        {
            string str = "select * from addsubject where Subjectno=" + no + "";
            SqlDataAdapter da2 = new SqlDataAdapter(str, sqlconn);
            DataSet ds2 = new DataSet();
            da2.Fill(ds2, "Question");
            if (ds2.Tables[0].Rows.Count > 0)
            {
                DataRow dtr;
                int i = 0;
                while (i < ds2.Tables[0].Rows.Count)
                {
                    dtr = ds2.Tables[0].Rows[i];
                    Session["Answer"] =Convert.ToString(Convert.ToInt32(dtr["Correctans"].ToString())-1);
                    lblqs.Text = "Q." + Session["counter"].ToString() + "  " + dtr["Questions"].ToString();
                    RblOption.ClearSelection();
                    RblOption.Items.Clear();
                    RblOption.Items.Add(dtr["Answera"].ToString());
                    RblOption.Items.Add(dtr["Answerb"].ToString());
                    RblOption.Items.Add(dtr["Answerc"].ToString());
                    RblOption.Items.Add(dtr["Answerd"].ToString());
                    i++;
                }
            }
        }
        public void getNextQuestion()
        {
            if (Convert.ToInt32(Session["counter"].ToString()) < 10)//10 is a counter which is used for 10 questions
            {
                if (RblOption.SelectedIndex >= 0)
                {
                    if (Session["Answer"].ToString() == RblOption.SelectedIndex.ToString())
                    {
                        int score = Convert.ToInt32(txtscore.Text) + 1;// 1 for mark for each question
                        txtscore.Text = score.ToString();
                        lblscore.Text = "Score : " + Convert.ToString(score);
                    }
                }

                Random rnd = new Random();
                int i = rnd.Next(1, 10);
                //lblQuestion.Text = i.ToString();
                getQuestion(i);
                Session["counter"] = Convert.ToString(Convert.ToInt32(Session["counter"].ToString()) + 1);

            }
            else
            {
                Panel2.Visible = false;
                //code for displaying after completting the exam, if you want to show the result then you can code here.
            }
        }

        #region Connection Open
        public void ConnectionOpen()
        {
            try
            {
                if (sqlconn.State == ConnectionState.Closed) { sqlconn.Open(); }
            }
            catch (SqlException ex)
            {
                Response.Write(ex);
            }
            catch (SystemException syex)
            {
                Response.Write(syex);
            }
        }
        #endregion
        #region Connection Close
        public void ConnectionClose()
        {
            try
            {
                if (sqlconn.State != ConnectionState.Closed) { sqlconn.Close(); }
            }
            catch (SqlException ex)
            {
                Response.Write(ex);
            }
            catch (SystemException syex)
            {
                Response.Write(syex);
            }
        }
        #endregion

    }
}
Posted
Updated 9-Mar-15 18:57pm
v2
Comments
StM0n 10-Mar-15 1:25am    
Are you aware, that this is not a question?
WaseemAmin 10-Mar-15 1:36am    
mention the event where this error occurs

Every Convert.Int32 can threw the error you experience if the input string provided for the method can not be converted to an integer...
Use debugger to check the actual values you use and see why it fails...
 
Share this answer
 
Change Code as below

C#
Session["counter"] = Convert.ToString(Convert.ToInt32(Session["counter"])+1);


In below line of code ur implementing wrong logic .
C#
Session["counter"] = Convert.ToString(Convert.ToInt32(Session["counter"].ToString()) + 1);


For further use Visual studio debbugger where you can identify the solution .
 
Share this answer
 
v2
Comments
Member 11480813 10-Mar-15 7:53am    
i got a error in line
Session["Answer"] =Convert.ToString(Convert.ToInt32(dtr["Correctans"].ToString())-1);.
here it shows the input string is not correct

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900