Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using entity framework 5 for saving data from application to SQL Server 2014 with a webservice.It is a winforms Application.

entity class is having date declared as below

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities.Commen;
namespace Entities.Masters
{
   public class ChittyRecieptEntity:Utility
    {
        public DateTime ChittyRepaymentDate { get; set; }
       ///...........
       ///.........
    }
}


and web services class is created like below

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Entities.Masters;
using System.Data.Entity;
using Data;
namespace Operations.Masters
{
   public class ChittyRecieptOperations
    {
       public ChittyRecieptEntity DoSaveChittyReciept(ChittyRecieptEntity entity)
        {
            try
            {
                using (JMFLEntities ctxt = new JMFLEntities())
                {
                    TblChittyReciept tblobj = new TblChittyReciept();
                ///.....
                ///....
                    tblobj.ChittyRepaymentDate = entity.ChittyRepaymentDate;
                    ctxt.SaveChanges();
                 }
            }
            catch (Exception ex)
            {
            }
            return entity;
        }


database field is ChittyRepaymentDate ,declared as date and not null

server is in US (godaddy)

this date field is passing from application (using datetime.now) ,but after saving in date field on sql server it is saved as previous date value.

can you tell me what might i have done wrong.

Thanks in advance
George

What I have tried:

while using service from local machine and database on live server, date is saving correctly.

a previous date selected in date picker of application saves correctly

selecting date on datepicker twice (other than current date then current date)
saves correctly

date passing from application with datetime.now and default date of date picker(current date) saves incorrect or previous date

while using break point in debugger can see value of date passing from application is correct and saving is incorrect on live server.
Posted
Updated 9-Aug-16 19:54pm

Hi,
Did you try to call context.AcceptAllChanges() ?
The following link c# - SaveChanges vs. AcceptAllChanges in Entity Framework - Stack Overflow[^] contains the use for both methods.

C#
using (JMFLEntities ctxt = new JMFLEntities())
                {
                    TblChittyReciept tblobj = new TblChittyReciept();
                ///.....
                ///....
                    tblobj.ChittyRepaymentDate = entity.ChittyRepaymentDate;
//Edit: as you're creating a new EF object, I think you should attach it to context such as: 
ctxt.TblChittyReciepts.AddObject(tblobj); 

                    ctxt.SaveChanges();
                    ctxt.AcceptAllChanges(); 
                 }
 
Share this answer
 
v2
Comments
george4986 10-Aug-16 1:56am    
doesn't worked in my case.Thanks for sparing your time ;-)
I have resolved the issue by passing converted time using timezone within the application.Since the application's usage is within India.I have used like this

C#
public static DateTime toISTDate(DateTime input)
       {
           return TimeZoneInfo.ConvertTime(input.Date, TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"));
       }


and converted date input as

C#
Entity.ChittyRepaymentManualDate = FormUtility.toISTDate(dtpManualRcptDate.Value);


This resolved my issue.I think while using entity framework,the value of datetime.now passing from application is changed to the hosted spaces location based time.
 
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