2016-12-08 35 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

相關問題