'来自&System.SDateTime'的无效广播到' System.TimeSpan'。'

时间:2018-04-27 15:14:09

标签: asp.net sql-server model-view-controller

我使用存储过程将Excel数据导入ASP.NET MVC应用程序中的SQL Server。问题是当我运行应用程序并上传文件时,我得到了上述错误并且光标指向foreach (var a in empDetails)

这是我的控制者:

public ActionResult ExcelUpload()
{
    return View();
}

[HttpPost]
public ActionResult UploadExcel(Réception_camions objEmpDetail, HttpPostedFileBase FileUpload)
{
    Réception_phosphateEntities objEntity = new Réception_phosphateEntities();
    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("~/DetailFormatInExcel/");
                FileUpload.SaveAs(targetpath + filename);
                string pathToExcelFile = targetpath + filename;

                string sheetName = "Sheet1";

                var excelFile = new ExcelQueryFactory(pathToExcelFile);
                var empDetails = from a in excelFile.Worksheet<Réception_camions>(sheetName) select a;

                // The error occurs in this line:
                // Invalid cast from 'System.DateTime' to 'System.TimeSpan'
                foreach (var a in empDetails)
                {
                    if (a.Date_d_arrivée != null )
                    {
                        DateTime Date_d_arrivée = (a.Date_d_arrivée);
                        int Id_qualité = Convert.ToInt32(a.Id_qualité);

                        int result = PostExcelData(a.Date_d_arrivée, a.heure_d_arrivée, a.Poids_cam,/* myBirthdate,*/ a.Id_cam, a.Id_qualité /*a.PostelCode, a.EmailId*/);

                        if (result <= 0)
                        {
                            data = "Hello User, Found some duplicate values! Only unique employee number has inserted and duplicate values(s) are not inserted";
                            ViewBag.Message = data;
                            continue;
                        }
                        else
                        {
                            data = "Successful upload records";
                            ViewBag.Message = data;
                        }
                    }
                    else
                    {
                        data = a.Date_d_arrivée + "Some fields are null, Please check your excel sheet";
                        ViewBag.Message = data;
                        return View("ExcelUpload");
                    }
                }
            }
            else
            {
                data = "This file is not valid format";
                ViewBag.Message = data;
            }

            return View("ExcelUpload");
        }
        else
        {
             data = "Only Excel file format is allowed";

             ViewBag.Message = data;
             return View("ExcelUpload");
        }
    }
    else
    {
         if (FileUpload == null)
         {
             data = "Please choose Excel file";
         }

         ViewBag.Message = data;
         return View("ExcelUpload");
    }
}

public int PostExcelData(DateTime Date_d_arrivée, TimeSpan heure_d_arrivée, Decimal Poids_cam, int Id_cam, int Id_qualité)
{
    Réception_phosphateEntities DbEntity = new Réception_phosphateEntities();
    var InsertExcelData = DbEntity.usp_InsertNewEmployeeDetails(Date_d_arrivée, heure_d_arrivée, Poids_cam, Id_cam, Id_qualité );

    return InsertExcelData;
}

这是我的表Réception_camions的存储过程:

ALTER PROCEDURE [dbo].[usp_InsertNewEmployeeDetails]  
    (@Date_d_arrivée DATETIME = NULL,  
     @heure_d_arrivée TIME(7) = NULL,  
     @Poids_cam DECIMAL(18, 2) = null,  
     @Id_cam INT = NULL, 
     @Id_qualité INT = NULL)
AS 
BEGIN
    INSERT INTO Réception_camions([Date d'arrivée], [heure d'arrivée], Poids_cam,  
                                   Id_cam, Id_qualité)  
    VALUES (@Date_d_arrivée, @heure_d_arrivée, @Poids_cam,  
            @Id_cam, @Id_qualité)  
END

如果有人可以帮助我,我会被阻止。

0 个答案:

没有答案