错误:输入字符串的格式不正确。在此代码中 [英] Error:Input string was not in a correct format.in this code

查看:77
本文介绍了错误:输入字符串的格式不正确。在此代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

    }
}

推荐答案

如果为方法提供的输入字符串无法转换为整数,则每个Convert.Int32都会抛出您遇到的错误...

使用调试器检查你使用的实际值,看看它失败的原因......
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...


更改代码如下



Change Code as below

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





在下面的代码行中你实现了错误的逻辑。



In below line of code ur implementing wrong logic .

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





进一步使用Visual Studio debbugger,您可以在其中找出解决方案。



For further use Visual studio debbugger where you can identify the solution .


这篇关于错误:输入字符串的格式不正确。在此代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆