2016-12-08 32 views
2

我目前正在尝试使用Visual Studio在c#中创建一个asp.net web应用程序。我有一个页面充当父母或孩子的注册页面,具体取决于您选择的单选按钮。注册孩子时,您需要从三个单独的下拉列表中输入DOB。现在,我将DOB数据类型设置为varchar,这意味着05/05/2005的DOB在表中保存为'552005'如何在SQL Server中使用'date'数据类型

当我的数据类型设置为datedatetime它抛出这个错误:

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: Conversion failed when converting date and/or time from character string.

这是否意味着我必须将字符串解析成一个int某处的代码?如果是这样,我的代码中究竟应该如何以及在哪里?我将包含一些截图以及我的代码。

提前致谢!

Showing how my form looks and how DOB currently stores in table

protected void submitBtn_Click(object sender, EventArgs e) 
{ 
    SqlConnection connect = new SqlConnection("Data Source=THEBEAST;Initial Catalog=newregDB;Integrated Security=True;Pooling=False"); 

    if (parentRadBtn.Checked) 
    { 
     if (firstNameBox.Text == "" || surnameBox.Text == "" || postcodeBox.Text == "" || teleBox.Text == "" || emailBox.Text == "" || userBox.Text == "" || passwordBox.Text == "") 
     { 
      Response.Write("<script>alert('Please ensure all fields have an entry');</script>"); 
      successLabel.Text = (""); 
      userBox.Text = ""; 
      firstNameBox.Text = ""; 
      surnameBox.Text = ""; 
      postcodeBox.Text = ""; 
      teleBox.Text = ""; 
      emailBox.Text = ""; 
      passwordBox.Text = ""; 
     } 
     else 
     { 
      SqlCommand pa = new SqlCommand("INSERT INTO parent(parentID, firstname, surname, postcode, telephone, email, password) VALUES (@parentID, @firstname, @surname, @postcode, @telephone, @email, @password)", connect); 
      pa.Parameters.AddWithValue("@parentID", userBox.Text); 
      pa.Parameters.AddWithValue("@firstname", firstNameBox.Text); 
      pa.Parameters.AddWithValue("@surname", surnameBox.Text); 
      pa.Parameters.AddWithValue("@postcode", postcodeBox.Text); 
      pa.Parameters.AddWithValue("@telephone", teleBox.Text); 
      pa.Parameters.AddWithValue("@email", emailBox.Text); 
      pa.Parameters.AddWithValue("@password", passwordBox.Text); 

      connect.Open(); 
      pa.ExecuteNonQuery(); 
      connect.Close(); 
     } 

     if (IsPostBack) 
     { 
      userBox.Text = ""; 
      firstNameBox.Text = ""; 
      surnameBox.Text = ""; 
      postcodeBox.Text = ""; 
      teleBox.Text = ""; 
      emailBox.Text = ""; 
      passwordBox.Text = ""; 
     } 
    }   
    else if (childRadBtn.Checked) 
    { 
     if (firstNameBox.Text == "" || dayDobList.Text == "" || monthDobList.Text == "" || yearDobList.Text == "" || genderList.Text == "" || userBox.Text == "" || passwordBox.Text == "") 
     { 
      Response.Write("<script>alert('Please ensure all fields have an entry');</script>"); 
      successLabel.Text = (""); 
      userBox.Text = ""; 
      firstNameBox.Text = ""; 
      dayDobList.Text = ""; 
      monthDobList.Text = ""; 
      yearDobList.Text = ""; 
      genderList.Text = ""; 
      passwordBox.Text = ""; 
     } 
     else 
     { 
      SqlCommand ca = new SqlCommand("INSERT INTO children(childID, firstname, dob, gender, password) VALUES (@childID, @firstname, @dob, @gender, @password)", connect); 
      ca.Parameters.AddWithValue("@childID", userBox.Text); 
      ca.Parameters.AddWithValue("@firstname", firstNameBox.Text); 
      ca.Parameters.AddWithValue("@dob", dayDobList.Text + monthDobList.Text + yearDobList.Text); 
      ca.Parameters.AddWithValue("@gender", genderList.Text); 
      ca.Parameters.AddWithValue("@password", passwordBox.Text); 

      connect.Open(); 
      ca.ExecuteNonQuery(); 
      connect.Close(); 
     } 

     if (IsPostBack) 
     { 
      userBox.Text = ""; 
      firstNameBox.Text = ""; 
      dayDobList.Text = ""; 
      monthDobList.Text = ""; 
      yearDobList.Text = ""; 
      genderList.Text = ""; 
      passwordBox.Text = ""; 
     }    
    } 
} 
+1

你怎么知道从2016年11月2日2016年1月12日? – Hogan

+0

为什么你必须把日期作为nvarchar?为什么不“约会”?您输入的值将采用格式“2014-01-01”。 – Auguste

+0

Auguste,正如我在原始问题中所说的,当我将其更改为键入'date'或'datetime'时,我收到一条错误消息。我认为通过改变日期类型,它会自动接受输入'552015',并使其5/5/2015或沿着它们行。 – ACostea

回答

0

你的问题是,你必须与旧格式'552005'您的表中的一些数据,所以当你想改变列类型,你会得到错误。所以你有两种选择:

1-从该表中删除任何东西,更改列类型并开始保存日期为datetimedatetime2

2-将所有当前数据转换为日期字符串,例如。 '05/05/2005 00:00:00',然后将列类型更改为datetime

UPDATE:

也不要忘记为数据类型添加到您的SqlParameters:

SqlParameter dob = new SqlParameter("@sinceDateTime", SqlDbType.DateTime); 
dob.Value = new DateTime(Int32.Parse(yearDobList.Text), Int32.Parse(monthDobList.Text), Int32.Parse(dayDobList.Text)); 
ca.Parameters.Add(dob); 
+0

每次测试表格时,我都会立即删除条目。我有一个清晰的表格,将类型更改为日期或日期时间,并在尝试将值提交到表格时出现错误。 :( – ACostea

+0

您也错过了数据类型,请参阅更新@ACostea – Yaser

+0

没有需要3个字符串的DateTime构造函数,也许你的意思是这个?https://msdn.microsoft.com/en-us/library/ xcfzdy4x%28v = vs.110%29.aspx – Hogan

0

需要一个日期,让日期:

new DateTime(int.Parse(dayDobList.Text), 
      int.Parse(monthDobList.Text), 
      int.Parse(yearDobList.Text)) 
+0

@Siyual - 完全...上面的代码将通过替换“ca.Parameters.AddWithValue(”@ dob“,dayDobList.Text + monthDobList.Text + yearDobList.Text);'这显然是错误的,并会失败。因为它正在创建一个字符串而不是日期。我相信你知道;() – Hogan

+0

那么我会把新的DateTime部分的代码?我认为它应该在我开始创建SqlConnection的时候开始,但它会抛出错误。我很欣赏我的帮助,我只是不知道该怎么处理它。 – ACostea

+0

我想这不是很有帮助。 @Yaser举了一个关于如何将这个参数用作你的sql调用的例子。 – Hogan

相关问题