Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

Định dạng DateTime trong SQL và C#

22:49 03-10-2018 1.344 lượt xem 2 bình luận 15:46 04-10-2018

Em đang học gặp lỗi này. Mò mãi nhưng không được mong mọi ngừoi giúp đỡ.

 private void capnhatdulieu()
        {
            moketnoi();
            cmd = new SqlCommand("sp_capnhat", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@ten", SqlDbType.NVarChar).Value = ten;
            cmd.Parameters.Add("@ns", SqlDbType.DateTime).Value = ngaysinh;
            cmd.Parameters.Add("@cmnd", SqlDbType.Int).Value = cmnd;
            cmd.Parameters.Add("@sd", SqlDbType.Float).Value = sodien;
            cmd.Parameters.Add("@gia", SqlDbType.Money).Value = giaphong;
            cmd.Parameters.Add("@mp", SqlDbType.Int).Value = mp;

            cmd.ExecuteNonQuery();
            dongketnoi();
        }

Lỗi:

System.Data.SqlTypes.SqlTypeException occurred
  HResult=-2146232016
  Message=SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
  Source=System.Data
  StackTrace:
       at System.Data.SqlClient.TdsParser.TdsExecuteRPC(SqlCommand cmd, _SqlRPC[] rpcArray, Int32 timeout, Boolean inSchema, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, Boolean isCommandProc, Boolean sync, TaskCompletionSource`1 completion, Int32 startRpc, Int32 startParam)
       at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
       at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
       at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
       at Demo.Form1.capnhatdulieu() in E:\PRJ_DEMO_PT\Demo\Demo\Form1.cs:line 72
  InnerException:

 

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
K9 SuperAdmin, KquizAdmin, KquizAuthor đã bình luận 15:46 04-10-2018

và 1 lưu ý là data nó phải trong khoản Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

thành ra phải chekc datetime vào trước ngoài khoảng thì lấy max hoặc min trong đó mà sài

Nguyễn Thái Hải đã bình luận 09:08 04-10-2018

format lại ngaysinh cho đúng định dạng fortmat datetime của hệ thống nha (máy tính bạn đó)

Câu hỏi mới nhất