kapozade
kapozade

Reputation: 137

Datetime2 to smalldatetime conversion in EF

  protected void ButtonSave_Click(object sender, EventArgs e) {
         using (IntranETEntities context = new IntranETEntities())
         {
                CUSTOMER customer = context.CUSTOMERs.FirstOrDefault<CUSTOMER>(p => p.CODE == TxtName.Text);
                CUSTOMER_EXTENSION customerextension = null;
                IE_CUSTOMER_INFO customerInfo = null;
                ACADEMY academy = new ACADEMY();
                JOB job = new JOB();

                if (customer == null)
                {
                    customer = new CUSTOMER();
                    context.AddToCUSTOMERs(customer);

                    customerextension = new CUSTOMER_EXTENSION();
                    customer.CUSTOMER_EXTENSION.Add(customerextension);


                    customerInfo = new IE_CUSTOMER_INFO();
                    customer.IE_CUSTOMER_INFO.Add(customerInfo);
                }
                else
                {
                    customer.CUSTOMER_EXTENSION.Load();
                    customer.IE_CUSTOMER_INFO.Load();

                    customerextension = customer.CUSTOMER_EXTENSION.FirstOrDefault<CUSTOMER_EXTENSION>();
                    customerInfo = customer.IE_CUSTOMERINFO.First<IE_CUSTOMER_INFO>();

                    if (customerextension == null)
                    {
                        customerextension = new CUSTOMER_EXTENSION();
                        customer.CUSTOMER_EXTENSION.Add(customerextension);
                    }
                    else
                        customerextension.UPDATESEQ++;

                    if (customerInfo == null)
                    {
                        customer;Info = new IE_CUSTOMER_INFO();
                        customer.IE_CUSTOMER_INFO.Add(customerInfo);
                    }

                    ....
                    customerextension.CREATE_DATE = DateTime.Now;  
                    ....
                    context.SaveChanges();//Error is occuring here.
                } }

I'm using EntityFramework which I'm newer. When I debugged this code I'm getting an error which says that "The conversion of a datetime2 data type to a smalldatetime data type resulted in an out-of-range value.\r\nThe statement has been terminated." I don't have permission to change the type of a column in database. And the column is not null. How can I fix it?

Thanks for your reply!!!

Upvotes: 1

Views: 2586

Answers (2)

Myk Willis
Myk Willis

Reputation: 12879

You mark the exception as happening at the point of the assignment:

 ....
 customerextension.CREATE_DATE = DateTime.Now; //Error is occuring here.
 ....

But I would expect that this exception to actually occur during the SaveChanges() call?

I suspect that there is another DateTime propery (other than the CREATE_DATE you point out) somewhere in the object graph being saved, and that other property is not being initialized. The default value of a DateTime property is outside the range required by smalldatetime, and so if you create a new EF object with a default DateTime property it will throw this exception.

Upvotes: 1

tschmit007
tschmit007

Reputation: 7800

smalldatetime is between 1900-01-10 and 2079-06-06. If your date is out of this range (default value for datetime in .Net is 0001-01-01) you will have this error.

Upvotes: 0

Related Questions