Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 转换值时System.FormatException异常_C#_Asp.net_.net - Fatal编程技术网

C# 转换值时System.FormatException异常

C# 转换值时System.FormatException异常,c#,asp.net,.net,C#,Asp.net,.net,我正在接受这个错误 System.FormatException 输入人员详细信息后单击按钮1时 public partial class _Default : Page { protected void Button1_Click(object sender, EventArgs e) { Response.Write("You have successfully added a Person!"); Person p = new

我正在接受这个错误

System.FormatException

输入人员详细信息后单击
按钮1

public partial class _Default : Page
{    
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("You have successfully added a Person!");    
        Person p = new Person(TextBox1.Text, Convert.ToInt32(TextBox2.Text), 
        TextBox3.Text, TextBox4.Text, Convert.ToBoolean(DropDownList1.SelectedValue), TextBox5.Text);    
        Label1.Text = (p.PresentPerson());
    }
}
人员类别:

class Person
{
    public int Age { get { return Age; } set { Age = value; } }
    public string Name { get { return Name; } set { Name = value; } }
    public string DOB { get { return DOB; } set { DOB = value; } }
    public string TelNo { get { return TelNo; } set { TelNo = value; } }
    public bool Gender { get { return Gender; } set { Gender = value; } }
    public string Address { get { return Address; } set { Address = value; } }
    public string enterPerson;

    public Person(string name, int age, string dob, string telNo, bool gender, string address)
    {
        Name = name;
        Age = age;
        DOB = dob;
        TelNo = telNo;
        Gender = gender;
        Address = address;
    }

    public string PresentPerson()
    {
        return enterPerson = "Name: " + Name + "\n" + "Age: " + Age + "\n" +  "Date of Birth: "
        + DOB + "\n" + "Telephone Number: " + TelNo + "\n" + "Gender: " + "\n" + "Address: "
        + Address;
    }
}

简单的解决方案是检查性别值,然后设置适当的布尔值:

public partial class _Default : Page
{

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("You have successfully added a Person!");
        bool gender = (DropDownList1.SelectedValue == "Male") ? true:false;
        Person p = new Person(TextBox1.Text, Convert.ToInt32(TextBox2.Text), 
        TextBox3.Text, TextBox4.Text, gender, TextBox5.Text);

        Label1.Text = (p.PresentPerson());
    }
}
仅当值为男性时,此选项才会将性别设置为true

更高级的一点是使用枚举:

也许像这样的转换语句,你可以轻松地处理其他问题:

public partial class _Default : Page
    {

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Write("You have successfully added a Person!");
            Person.Genders gender = Person.Genders.NotSet;
            switch(DropDownList1.SelectedValue)
            {
               case "Male": gender = Person.Genders.Male; break;
               case "Female": gender = Person.Genders.Female; break;
            }
            Person p = new Person(TextBox1.Text, Convert.ToInt32(TextBox2.Text), 
            TextBox3.Text, TextBox4.Text, gender, TextBox5.Text);

            Label1.Text = (p.PresentPerson());
        }
    }

要避免错误,请使用文本框的验证程序,如regularexpression或custom。 例如:
\d+
适用于您的年龄文本框。 这将帮助您最大限度地减少错误。由于您对男性和女性的表示是布尔值,因此您可以执行以下操作:

bool gender = DropDownList1.SelectedValue == "Male"

并将其传递给构造函数。

请提供整个异常消息,包括回溯。您正在盲目地将TextBox2.Text的值转换为整数,将DropDownList的SelectedValue转换为布尔值。显然,Convert.toxxx所期望的不是其中一个。如果您花一点时间学习如何使用调试器,这些错误将立即得到解决。问题:是:mscorlib.dll中发生了“System.FormatException”类型的异常,但未在用户代码中处理。其他信息:字符串未被识别为有效的布尔值。如果SelectedValue为null怎么办?然后它将设置为错。这是因为这个解决方案“简单”,不适用于更复杂的情况。当然,我们不知道这是否是OP想要的。我的评论只是给你一个提示,让你对你的答案做一些澄清。
bool gender = DropDownList1.SelectedValue == "Male"