可以';t调用我的方法:无法从';字符串';到';System.DateTime';
本文关键字:字符串 DateTime System 方法 调用 我的 可以 | 更新日期: 2023-09-27 18:29:40
我制作了一个文本框,允许用户输入他们的出生日期。作为注册页面的一部分,我还创建了一个链接到此文本框的数据库。我创建了一个名为Members的类,它包含两个方法,其中一个方法有参数。我的问题是我无法在Web用户控件中转换日期时间的文本框!
这是类Members.cs
中的两种方法
public string Register()
{
if (Add())
return "User Added successfully";
else
return "User not added, please change username and try again!";
}
public string Register(string username, string password, string name, string phone, string gender, System.DateTime dateofbirth, string email, string company, string question, string answer)
{
this.username = username;
this.password = password;
this.name = name;
this.email = email;
this.phone = phone;
this.company = company;
this.gender = gender;
this.dateofbirth = dateofbirth;
this.question = question;
this.answer = answer;
return Register();
}
我的Web用户控件中的代码:
protected void btnRegister_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(3000);
Members M = new Members();
lblMsg.Text = M.Register(txtUser.Text, txtPassword.Text, txtName.Text,txtEmail.Text, txtPhone.Text, txtCo.Text, rbnGender.SelectedValue, Convert.ToDateTime(txtDOB.Text), txtQuestion.Text, txtAnswer.Text);
}
您的方法采用以下参数:
username, password, name, phone, gender, dateofbirth, email, company, question, answer
但你以错误的顺序通过这些:
txtUser.Text, txtPassword.Text, txtName.Text, txtEmail.Text, txtPhone.Text, txtCo.Text, rbnGender.SelectedValue, Convert.ToDateTime(txtDOB.Text), txtQuestion.Text, txtAnswer.Text
应该是:
lblMsg.Text = M.Register(txtUser.Text, txtPassword.Text, txtName.Text, txtPhone.Text, rbnGender.SelectedValue, Convert.ToDateTime(txtDOB.Text), txtEmail.Text, txtCo.Text, txtQuestion.Text, txtAnswer.Text);
但是,您可能应该使用DateTime.Parse
或DateTime.ParseExact
来转换您的出生日期字段。