C# 无法解决为注册引发的异常错误

C# 无法解决为注册引发的异常错误,c#,asp.net,C#,Asp.net,我现在正在做一个注册,如果用户说他们不到18岁-在提交注册后会出现一个错误,说你必须至少18岁。但我一直在提交表的调用堆栈中抛出这个异常错误,表示未将字符串识别为有效的。我已经输入了BirthDate.Text=DateTime.Now.ToString(“dd/MM/yyyy”)尝试并修复该解决方案,但该错误仍然作为异常出现。我会错过什么吗 所讨论的错误是 if (DateTime.Parse(BirthDate.Text).AddYears(18) > DateTime.Now) {

我现在正在做一个注册,如果用户说他们不到18岁-在提交注册后会出现一个错误,说
你必须至少18岁。
但我一直在提交表的调用堆栈中抛出这个异常错误,表示未将
字符串识别为有效的
。我已经输入了
BirthDate.Text=DateTime.Now.ToString(“dd/MM/yyyy”)
尝试并修复该解决方案,但该错误仍然作为异常出现。我会错过什么吗

所讨论的错误是

if (DateTime.Parse(BirthDate.Text).AddYears(18) > DateTime.Now) {
                errorList.Add("You must be at least 18 years of age.");
代码

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

namespace website {
    public partial class Register : Page {

        string ConnectionString = "Server=localhost\\SQLEXPRESS;Database=HC;Trusted_Connection=True;";

        protected void Page_Load(object sender, EventArgs e) {

             BirthDate.Text = DateTime.Now.ToString("dd/MM/yyyy");

            if (!IsPostBack) {
                string QueryString = "select * from home";

                SqlConnection myConnection = new SqlConnection(ConnectionString);
                SqlDataAdapter myCommand = new SqlDataAdapter(QueryString, myConnection);
                DataSet ds = new DataSet();
                myCommand.Fill(ds, "House");

            }
        }

        protected void submit(object sender, EventArgs e) {

            List<string> errorList = new List<string>();

            if (BirthDate.Text == "") {
                LiteralControl birthDate = new LiteralControl("Birth date is required!");
                BirthDateRequired.Controls.Add(birthDate);
                errorList.Add(birthDate.Text);
            }

            if (DateTime.Parse(BirthDate.Text).AddYears(18) > DateTime.Now) {
                errorList.Add("You must be at least 18 years of age.");
            }

            if (errorList.Count > 0) {
                foreach (string s in errorList)
                    ErrorList.Controls.Add(new LiteralControl("* " + s));
            }

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统数据;
使用System.Data.SqlClient;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
名称空间网站{
公共部分类寄存器:第页{
string ConnectionString=“Server=localhost\\SQLEXPRESS;Database=HC;Trusted_Connection=True;”;
受保护的无效页面加载(对象发送方、事件参数e){
BirthDate.Text=DateTime.Now.ToString(“dd/MM/yyyy”);
如果(!IsPostBack){
string QueryString=“选择*从主页”;
SqlConnection myConnection=新的SqlConnection(ConnectionString);
SqlDataAdapter myCommand=新的SqlDataAdapter(QueryString,myConnection);
数据集ds=新数据集();
myCommand.Fill(ds,“房屋”);
}
}
受保护的无效提交(对象发送者、事件参数e){
List errorList=新列表();
如果(BirthDate.Text==“”){
LiteralControl出生日期=新的LiteralControl(“需要出生日期!”);
需要birthDate.Controls.Add(birthDate);
errorList.Add(birthDate.Text);
}
if(DateTime.Parse(BirthDate.Text).AddYears(18)>DateTime.Now){
错误列表。添加(“您必须至少18岁。”);
}
如果(errorList.Count>0){
foreach(错误列表中的字符串s)
ErrorList.Controls.Add(新的LiteralControl(“*”+s));
}
}
}
}

您可以使用以下重载的
ParseExact
方法-

public static DateTime ParseExact(string s, string format, IFormatProvider? provider);
您可以将指定的日期格式作为字符串传递给第二个参数

因此,如果您的
BirthDate.Text
dd/MM/yyyy
格式输入,则将代码更改为-

if (DateTime.ParseExact(BirthDate.Text, "dd/MM/yyyy", null).AddYears(18) > DateTime.Now)
{
    errorList.Add("You must be at least 18 years of age.");
}
另外,您不需要设置
BirthDate.Text=DateTime.Now.ToString(“dd/MM/yyyy”)