System.FormatException:输入字符串的格式不正确

发布于 2024-11-04 04:36:53 字数 2177 浏览 4 评论 0原文

在下面的代码中,当我尝试将项目添加到 ASP DropDownList 时,抛出 System.FormatException: 输入字符串的格式不正确。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class ScheduleExam : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String connection = System.Configuration.ConfigurationManager.ConnectionStrings["TYCConnection"].ConnectionString;

        String branch = Request.Form["ctl00$ctl00$MainContent$AdminMainContent$BranchDropDownList"];
        if (!String.IsNullOrWhiteSpace(branch))
        {
            if (!branch.Equals("00"))
            {
                SqlConnection sqlConn = new SqlConnection(connection);
                String semQuery = "select totalSem from branchTable where branchId='" + branch + "'";
                SqlCommand semCommand = new SqlCommand(semQuery, sqlConn);

                sqlConn.Open();
                SqlDataReader semReader = semCommand.ExecuteReader();
                semReader.Read();
                int totalSem = Int32.Parse(semReader["totalSem"].ToString());
                SemesterDropDownList.Items.Clear();
                SemesterDropDownList.Enabled = true;
                //ListItem list = new ListItem("Select");
                SemesterDropDownList.Items.Add(new ListItem("select"));
                for (int sem = 1; sem <= totalSem; sem++)
                {
                    //SemesterDropDownList.Items.Add(sem.ToString());
                }
                sqlConn.Close();
            }
            else
            {
                SemesterDropDownList.Items.Clear();
                SemesterDropDownList.Enabled = false;
                //SemesterDropDownList.Items.Add("First Select Branch");
            }
        }
        else
        {
            SemesterDropDownList.Enabled = false;
            //SemesterDropDownList.Items.Add("First Select Branch");
        }
    }
    protected void RegisterButton_Click(object sender, EventArgs e)
    {

    }
}

然而,当我评论所有这些行时,没有抛出异常。

问题可能是什么及其可能的解决方案?

In the following code, when I try to add an Item to an ASP DropDownList, System.FormatException: Input string was not in a correct format, is thrown.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class ScheduleExam : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String connection = System.Configuration.ConfigurationManager.ConnectionStrings["TYCConnection"].ConnectionString;

        String branch = Request.Form["ctl00$ctl00$MainContent$AdminMainContent$BranchDropDownList"];
        if (!String.IsNullOrWhiteSpace(branch))
        {
            if (!branch.Equals("00"))
            {
                SqlConnection sqlConn = new SqlConnection(connection);
                String semQuery = "select totalSem from branchTable where branchId='" + branch + "'";
                SqlCommand semCommand = new SqlCommand(semQuery, sqlConn);

                sqlConn.Open();
                SqlDataReader semReader = semCommand.ExecuteReader();
                semReader.Read();
                int totalSem = Int32.Parse(semReader["totalSem"].ToString());
                SemesterDropDownList.Items.Clear();
                SemesterDropDownList.Enabled = true;
                //ListItem list = new ListItem("Select");
                SemesterDropDownList.Items.Add(new ListItem("select"));
                for (int sem = 1; sem <= totalSem; sem++)
                {
                    //SemesterDropDownList.Items.Add(sem.ToString());
                }
                sqlConn.Close();
            }
            else
            {
                SemesterDropDownList.Items.Clear();
                SemesterDropDownList.Enabled = false;
                //SemesterDropDownList.Items.Add("First Select Branch");
            }
        }
        else
        {
            SemesterDropDownList.Enabled = false;
            //SemesterDropDownList.Items.Add("First Select Branch");
        }
    }
    protected void RegisterButton_Click(object sender, EventArgs e)
    {

    }
}

However, when I comment all such lines there is no exception thrown.

What could be the problem and its possible solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海夜未眠 2024-11-11 04:36:53

不要

int totalSem = Int32.Parse(semReader["totalSem"].ToString());

尝试

int totalSem;
Int32.TryParse(semReader["totalSem"].ToString(),totalSem);

,如果这样可以解决异常,那么就考虑解决该领域的问题。

Instead of

int totalSem = Int32.Parse(semReader["totalSem"].ToString());

try

int totalSem;
Int32.TryParse(semReader["totalSem"].ToString(),totalSem);

and if that takes care of the exception then look into addressing the issues with that field.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文