Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello my friend.
My project have a big mistake. when i insert a new data, my Datetime.now is returned bad values. Example, when insert: 05/20/2015. It will be back 2 day: 05/18/2015.
This is my Code.

Data:
C#
   public bool add(Bill_DTO bill_dto) 
{ 
    try 
    {
        SqlCommand _cmd = new SqlCommand('INSERT INTO BILL('ID_BILL,TIMECOMING') VALUES(@ID_BILL,@TIMECOMING)')
        _cmd.Parameters.Add("ID_BILL", SqlDbType.NVarChar).Value = bill_dto.ID_Bill;
        _cmd.Parameters.Add("TIMECOMING", SqlDbType.Datetime).Value = bill_dto.TimeComing; 
        dp = new DataProvider(); 

        return dp.ExecuteNonQuery(_cmd) > 0;
    } 
    catch (Exception ex) 
    { 
        throw ex; 
    } 
    finally 
    { 
        dp.CloseConn(); 
    } 


Business:

C#
Bill_DTO bill_dto = new Bill_DTO();
Bill_DATA bill_data = new Bill_DATA();
bill_dto.TimeComing= DateTime.now;
bill_data.add(bill_dto);


Thanks for your help !!!
Posted

DateTime.Now gets the date and time on your/client's computer. Are you sure that the computer you are using is in current date and time?
 
Share this answer
 
Comments
huy cao 19-May-15 21:31pm    
Thanks for ur help.
But my project is working on server. And Datime.now in my server not wrong.
I think user use Client PC to working on my project is unaffected because my code put in server and it will get DateTime.now on Server Time to process !
Dianne Ramos 19-May-15 21:36pm    
You can create a stored procedure for the insert process instead. Then use the date.now in sql rather than in c#. At least you can be sure that sql will be using the server's date and time values.
huy cao 19-May-15 21:42pm    
What's different from Datime.now on C# and SQL ?
Thank you !
Dianne Ramos 19-May-15 22:01pm    
Well, it is actually kind of the same but in SQL, you can have the option to use the server's date and time value if you use GETDATE() in stored procedure.
huy cao 19-May-15 22:17pm    
Okie. I'll try this. Thanks again !
change your sql statement to get sql server date and insert as below
SQL
INSERT INTO BILL('ID_BILL,TIMECOMING') VALUES(@ID_BILL,GETDATE())

note that now only ID_BILL need to set as parameter.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900