2013-03-27 127 views
0

我試圖從日曆控件中獲取所選日期並將其存儲到數據庫中的列。ASP.NET - VB.NET - SQL Server Express 2012 - 存儲日期

,我發現了以下錯誤:

SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

出生DB列日期:

enter image description here

VB.Code註冊頁面:

 Public Sub register() 



     Dim Username As String = txtUsername.ToString 
     Dim Surname As String = txtSurname.ToString 
     Dim Password As String = txtPassword.ToString 
     Dim Name As String = txtName.ToString 
     Dim Address1 As String = txtAddress1.ToString 
     Dim Address2 As String = txtAddress2.ToString 
     Dim City As String = txtCity.ToString 
     Dim Email As String = txtEmail.ToString 
     Dim Country As String = drpCountry.ToString 
     Dim DOB As Date = calDOB.SelectedDate.Date 
     Dim Occupation As String = txtOccupation.ToString 
     Dim WorkLocation As String = txtWorkLocation.ToString 
     Dim Age As Integer = "20" 

     lblDOB.Text = DOB.ToString 
     lblDOB.Visible = True 


     Dim ProjectManager As String = "test" 
     Dim TeamLeader As String = "test" 
     Dim TeamLeaderID As Integer = "1" 
     Dim ProjectManagerID As Integer = "1" 

     Dim RegistrationDate As Date = DateTime.Today 
     Dim ContractType As String = "test" 
     Dim ContractDuration As Integer = 6 
     Dim Department As String = "test" 

     Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True") 
     Dim registerSQL As SqlCommand 
     Dim sqlComm As String 

     sqlComm = "INSERT INTO users(Username, Password, Name, Surname, Address1, Address2, " + 
      "City, Country, date_of_birth, age, Occupation, department, work_location, " + 
      "project_manager,team_leader, team_leader_id, project_manager_id, " + 
      "date_registration, contract_type, contract_duration) " + 
      "VALUES(@p1, @p2,@p3,@p4,@p5,@p6,@p7,@p8,@p9,@p10,@p11,@p12,@p13,@p14,@p15," + 
      "@p16,@p17,@p18,@p19,@p20)" 

     conn.Open() 
     registerSQL = New SqlCommand(sqlComm, conn) 
     registerSQL.Parameters.AddWithValue("@p1", Username) 
     registerSQL.Parameters.AddWithValue("@p2", Password) 
     registerSQL.Parameters.AddWithValue("@p3", Name) 
     registerSQL.Parameters.AddWithValue("@p4", Surname) 
     registerSQL.Parameters.AddWithValue("@p5", Address1) 
     registerSQL.Parameters.AddWithValue("@p6", Address2) 
     registerSQL.Parameters.AddWithValue("@p7", City) 
     registerSQL.Parameters.AddWithValue("@p8", Country) 
     registerSQL.Parameters.AddWithValue("@p9", DOB) 
     registerSQL.Parameters.AddWithValue("@p10", Age) 
     registerSQL.Parameters.AddWithValue("@p11", Occupation) 
     registerSQL.Parameters.AddWithValue("@p12", Department) 
     registerSQL.Parameters.AddWithValue("@p13", WorkLocation) 
     registerSQL.Parameters.AddWithValue("@p14", ProjectManager) 
     registerSQL.Parameters.AddWithValue("@p15", TeamLeader) 
     registerSQL.Parameters.AddWithValue("@p16", TeamLeaderID) 
     registerSQL.Parameters.AddWithValue("@p17", ProjectManagerID) 
     registerSQL.Parameters.AddWithValue("@p18", RegistrationDate) 
     registerSQL.Parameters.AddWithValue("@p19", ContractType) 
     registerSQL.Parameters.AddWithValue("@p20", ContractDuration) 




     registerSQL.ExecuteNonQuery() 


    End Sub 



End Class 

異常跟蹤:

System.Data.SqlClient.SqlException was unhandled by user code 
    Class=16 
    ErrorCode=-2146232060 
    HResult=-2146232060 
    LineNumber=1 
    Message=String or binary data would be truncated. 
The statement has been terminated. 
    Number=8152 
    Procedure="" 
    Server=BRIAN-PC\SQLEXPRESS 
    Source=.Net SqlClient Data Provider 
    State=13 
    StackTrace: 
     at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) 
     at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) 
     at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) 
     at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) 
     at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) 
     at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite) 
     at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) 
     at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) 
     at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() 
     at Registration.register() in C:\Users\Brian\Google Drive\Dropbox\Dropbox\THESIS\ThesisApp\Registration.aspx.vb:line 89 
     at Registration.btnRegister_Click(Object sender, EventArgs e) in C:\Users\Brian\Google Drive\Dropbox\Dropbox\THESIS\ThesisApp\Registration.aspx.vb:line 14 
     at System.Web.UI.WebControls.Button.OnClick(EventArgs e) 
     at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) 
     at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) 
     at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) 
     at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 
     at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 
    InnerException: 

UPDATE:增加了新的錯誤消息,因爲我固定RegistrationDateDOB。現在我收到一條新的錯誤信息:

String or binary data would be truncated. 
The statement has been terminated. 

任何幫助表示讚賞!

+0

@marc_s我仍然得到同樣的錯誤! – Brian 2013-03-27 17:45:22

+0

當你將它分配給參數R時,「** DOB」有什麼**值?另外:這裏還有其他涉及'DATETIME'類型的列嗎?是否有表上的觸發器可能會在DATETIME列中插入一個新的審計表? – 2013-03-27 17:47:47

+0

@marc_s Dim RegistrationDate As Date = Today,但在DOB上停止 – Brian 2013-03-27 17:51:03

回答

0

什麼確定DOB當您將其分配給參數@p9

或可能RegistrationDate是罪魁禍首?什麼價值做這個變量時,你將它分配給參數@p18 ??

,如果你有在SQL Server DATETIME列,你想從.NET應用程序到其插入NULL通常會出現此錯誤 - 這被轉換爲01/01/0001這是DATETIME值範圍之外。

因此我的建議: - 或DATETIME2(兩個日期和時間),在SQL Server 2008和更高DATETIME2有這些值的範圍限制爲DATETIME(沒有時間,如果你只需要日期)使用DATE有)

+0

因此'Dim DOB As Date = calDOB.SelectedDate'然後'registerSQL.Parameters.AddWithValue(「@ p9」,DOB)'應該可以'開箱即用'? – Brian 2013-03-27 17:57:04

+2

@Brian:肯定 - 但**什麼值**是'calDOB.SelectedDate'在你的情況?這是一個真正的價值,或者它是否爲NULL? – 2013-03-27 17:57:44

+0

'calDOB.SelectedDate \t#3/15/2013#\t日期' – Brian 2013-03-27 18:00:47

1

這可能是一些使用
http://jwcooney.com/2012/09/10/asp-net-choosing-parameters-add-or-parameters-addwithvalue/

esp.the第二點的隱式數據轉換導致數據錯誤

..You can accidentally introduce data errors if you use the Parameters.AddWithValue syntax to send a date datatype to your database, then you are at the mercy of the database to identify how it will store your date value. This can lead to problems since there are numerous ways of entering dates: mm/dd/yyyy in North America, dd/mm/yyyy in the UK, and yyyy-mm-dd as an upcoming standard. The database will look at the date value of 1/2/2012 that you are sending and can handle the ’1′ as either the day or as the month.

我會建議使用:

registerSQL.Parameters.Add("@p9", SqlDbType.DateTime).Value = DOB.Date 

,而不是registerSQL.Parameters.AddWithValue("@p9", DOB)

確保DOB不是DateTime.MinValueDateTime.MaxValue

也可參考這個問題:

Difference between adding parameters to stored procedure in SQL Server 2005

+1

我回應了這個答案 - 我在使用AddWithValue時遇到過這樣的問題。 – 2013-03-27 18:18:44

相關問題