priya dharshini

priya dharshini

  • NA
  • 11
  • 4.7k

i get a error as input string was not in correct format

Mar 2 2015 11:34 PM
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"];
if (eventArg == "time")
{
getNextQuestion();
}
}
}

protected void Button1_Click(object sender, EventArgs e)
{

//txtName.Visible = false;
Button1.Visible = false;
Panel1.Visible = true;
//lblName.Text = "Name : " + txtName.Text;
int score =Convert.ToInt32(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"] = (dtr["Correctans"].ToString());
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"] == 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)
{

}
catch (SystemException syex)
{

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

}
catch (SystemException syex)
{

}
}
#endregion

}
}

Answers (3)