简体   繁体   中英

Issues with DateTime.Now C# When adding records to database

I am having a strange issue with my project. I have an upload excel file that automatically updates a table. The upload part is irrelevant, just adding it for clarity. When using this I call the dateTime as CreatedDate = System.DateTime.Now . It does not get this from the excel file. I get a Out of range error - dateTime2 to dateTime conversion. The table has a dateTime for the column. I am scratching my head because I have the same exact code in another controller for another table and it works fine. Tables where created at the same time in SQL. So the code and the tables are exact. One gives the error and the other does not. Date comes back fine with a Breakpoint. Is there something in the code below that you can see that would be causing this?

[HttpPost]
    public ActionResult Add(HttpPostedFileBase FileUpload)
    {
        GeneralEntities objEntity = new GeneralEntities();
        string data = "";
        if (FileUpload != null)
        {
            if (FileUpload.ContentType == "application/vnd.ms-excel" || FileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                string filename = FileUpload.FileName;
                if (filename.EndsWith(".xlsx"))
                {
                    string targetpath = Server.MapPath("~/Documents/DetailFormatInExcel/");
                    FileUpload.SaveAs(targetpath + filename);
                    string pathToExcelFile = targetpath + filename;
                    string sheetName = "Sheet1";
                    var excelFile = new ExcelQueryFactory(pathToExcelFile);
                    var Equipment = from a in excelFile.Worksheet<AirFloatTablesExcelModel>(sheetName) select a;

                    foreach (var a in Equipment)
                    {
                        var ishaveModel = db.EquipmentModels.Where(d => d.Title == a.EquipmentModel).FirstOrDefault();
                        
                        
                        AirFloatTables at = new AirFloatTables
                        {
                            AirFloatTableId = Guid.NewGuid(),
                            EquipmentModelId = ishaveModel.EquipmentModelId,
                            Size = a.Size,
                            Type = a.Type,
                            TiltHeight = a.TiltHeight,
                            DWGRef = a.DWGRef,
                            Price = a.Price,
                            SortOrder = a.SortOrder,
                            CreatedBy = User.Identity.Name,
                            CreatedDate = System.DateTime.Now
                        };
                        db.AirFloatTables.Add(at);
                        db.SaveChanges();

                    }
                }
                else
                {

                    data = "This file is not valid format";
                    TempData["Message"] = data;
                }

                return RedirectToAction("Index");
            }
            else
            {
                data = "Only Excel file format is allowed";
                TempData["Message"] = data;
                return RedirectToAction("Index");
            }
        }
        else
        {
            if (FileUpload == null)
            {
                data = "Please choose Excel file";
            }
            TempData["Message"] = data;
            return RedirectToAction("Index");
        }
    }

数据表

UPDATE: It might be worth it to say that I took the created date out of my code and then in SQL for the column I put GetDATE() . The issue is still there So It is not happening on the CreatedDate . If by chance the UpdatedDate is the issue I do not know why that would be. When debugging I do see that UpdatedDate does come back with zero's However my code does not call for an insertion of UpdatedDate So why would it try to insert it anyway? Or is it just a code thing where it sees it and flags that it is not correct. If this is the case, then why does the code in my other controller, that is exactly the same, not do the same thing?

You do not store a value for UpdatedDate , which is nullable, true, but this error happens mostly when for any reason a DateTime of year, month, day, etc. having a value of 0 is attempted to be inserted/updated as a value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM