0

カレンダー コントロールから選択した日付を取得し、DB の列に格納しようとしています。

次のエラーが表示されます。

SqlDateTime オーバーフロー。1753 年 1 月 1 日 12:00:00 AM から 9999 年 12 月 31 日 11:59:59 PM の間である必要があります。

生年月日DB列:

ここに画像の説明を入力

VB.登録ページのコード:

        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: 

更新: RegistrationDateDOBを修正したため、新しいエラー メッセージを追加しました。今、私は新しいエラーメッセージを受け取っています:

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

どんな助けでも大歓迎です!

4

2 に答える 2

1

これは役に立つかもしれません
http://jwcooney.com/2012/09/10/asp-net-choosing-parameters-add-or-parameters-addwithvalue/

特に 2 番目のポイント 暗黙のデータ変換がデータ エラーを引き起こす

..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

この質問も参照してください。

SQL Server 2005 でストアド プロシージャにパラメータを追加する場合の違い

于 2013-03-27T18:17:43.637 に答える
0

パラメータに割り当てると、どのような値なりますか??DOB@p9

それともRegistrationDate犯人かも?パラメータに割り当てると、その変数はどのような@p18になりますか??

このエラーは通常、SQL Server に列があり、.NET アプリケーションからDATETIME列に挿入しようとしている場合に発生します。NULL01/01/0001DATETIME

したがって、私の推奨事項: SQL Server 2008以降で(DATE日付のみが必要な場合-時間なし)またはDATETIME2(日付と時刻の両方が必要な場合)を使用します(これらの値の範囲の制限はありませDATETIME2DATETIME

于 2013-03-27T17:51:39.043 に答える