priya dharshini

priya dharshini

  • NA
  • 11
  • 4.5k

Input string was not in a correct format. error in this code

Mar 10 2015 12:56 AM
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

    }
}

Answers (2)