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)
    {

    }
}

ただし、そのような行をすべてコメントしても、例外はスローされません。

問題とその可能な解決策は何ですか?

4

1 に答える 1

0

それ以外の

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

試す

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

それで例外が処理される場合は、そのフィールドの問題に対処することを検討してください。

于 2011-04-29T20:24:47.603 に答える