值不能为空。 (参数's')尝试将excel文件上传到数据库时

Posted

技术标签:

【中文标题】值不能为空。 (参数\'s\')尝试将excel文件上传到数据库时【英文标题】:Value cannot be null. (Parameter 's') While trying to upload excel file to db值不能为空。 (参数's')尝试将excel文件上传到数据库时 【发布时间】:2020-11-11 01:11:00 【问题描述】:

我正在上载 Excel 值并将其保存到数据库中。在将值保存到 db 时,我收到错误 Value cannot be null. (Parameter 's') 我猜这个问题可能与 EF 有关,但我仍然无法确定如何解决它。

如果excel没有空列,则excel将成功上传并保存到db。但是如果某些excel列是空的,我该如何处理呢?

有些列我可以清空,这就是为什么我必须让一些可以为空。

我不知道我做错了什么

ex.StackTrace:

  at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
   at System.Int64.Parse(String s)
   at LoanWarehouse.Controllers.AccountUploadController.<SaveCustomerUpload>d__14.MoveNext() in C:\Users\DELL\source....\Controllers\UploadController.cs:line 409

这是提取值并保存到数据库的方法

   public async Task<Response> SaveCustomerUpload(int UploadId, ExcelWorksheet workSheet, int rowSize)
        
            Response response = new Response();
            response.IsSuccessful = true;

            List<Customer> CustomerDatalist = new List<Customer>();

            //skip first row for column header
            for (int i = 2; i <= rowSize; i++)
            
                try
                

                    CustomerDatalist.Add(new Customer
                    
                        CustId = long.Parse(workSheet.Cells[i, 1].Value.ToString()),
                        FirstName_CompanyName = workSheet.Cells[i, 2].Value.ToString(),
                        MiddleName = workSheet.Cells[i, 3].Value.ToString(),
                        Gender = workSheet?.Cells[i, 4]?.Value?.ToString(),
                        FatherName = workSheet?.Cells[i, 5]?.Value?.ToString(),
                        MotherName = workSheet?.Cells[i, 6]?.Value?.ToString(),
                        DateOfBirth = null,
                        BVN = long.Parse(workSheet?.Cells[i, 8]?.Value?.ToString()),
                        EmailAddress = workSheet?.Cells[i, 9]?.Value?.ToString(),
                        OfficeNumber = workSheet?.Cells[i, 10]?.Value?.ToString(),
                        Nationality = workSheet?.Cells[i, 11]?.Value?.ToString(),
                        PlaceOfBirth = workSheet?.Cells[i, 12]?.Value?.ToString(),
                        MaritalStatus = workSheet?.Cells[i, 13]?.Value?.ToString(),
                        SpouseName = workSheet?.Cells[i, 14]?.Value?.ToString(),
                        TelephoneNo = workSheet?.Cells[i, 15]?.Value?.ToString(),
                        MobileNo = workSheet?.Cells[i, 16]?.Value?.ToString(),
                        Address = workSheet?.Cells[i, 17]?.Value?.ToString(),
                        OfficeEmailAddress = workSheet?.Cells[i, 18]?.Value?.ToString(),
                        FaxNumber = workSheet?.Cells[i, 19]?.Value?.ToString(),
                        WebsiteURL = workSheet?.Cells[i, 20]?.Value?.ToString(),
                        RelatedComapany = workSheet?.Cells[i, 21]?.Value?.ToString(),
                        LegalConstitution = workSheet?.Cells[i, 22]?.Value?.ToString(),
                        ShareHolder = workSheet?.Cells[i, 22]?.Value?.ToString(),
                    
                    
                    );
                
                catch (Exception ex)
                
                    response.Message = ex.ToString();
                    response.IsSuccessful = false;
                    logger.Error(ex);
                    break;
                
            

            if (response.IsSuccessful)
            
                //save the details for this upload
                await _context.Customer.AddRangeAsync(CustomerDatalist);
                await _context.SaveChangesAsync();
            
            else
            
                //reverse the initial AccountUpload and delete the details
                var CustomerUpload = await _context.Customer.FindAsync(UploadId);
                _context.Customer.Remove(CustomerUpload);
                await _context.SaveChangesAsync();
            


            return response;
        

【问题讨论】:

我看到很多 Value?.toString() 和 Value.toString()。我建议调试它们,因为将 null 转换为字符串会引发异常。 有推荐的excel格式,有些栏位可能是空的,因为上传excel时有些信息可能不可用。我有一个方法在上传之前检查标题。 如果excel没有空列,则excel将成功上传并保存到db。但是如果某些excel列是空的,我该如何处理? 【参考方案1】:

您可以将属性的空值“转换”为字符串。 示例:CustId = long.Parse(workSheet.Cells[i, 1].Value.ToString()) ?? ""

编辑:对不起。这不起作用,因为它给 CustId 一个空字符串。但我认为这会奏效。首先,在“CustomerDatalist.Add”之外创建2个变量:

string _CustId = workSheet.Cells[i, 1].Value.ToString()
string _BVN = workSheet.Cells[i, 8].Value.ToString()

在“添加”方法中将它们各自的行更改为: CustId = (_CustId == "") ? (int?)null : long.Parse(_CustId), BVN = (_BVN == "") ? (int?)null : long.Parse(_CustId),

如果字符串为空,这将为属性分配空值。但它需要您更改“CustomerDatalist”类中​​的一些参数。添加 ?在属性和对象本身的定义中:

long ?CustId
long ?BVN

public CustomerDatalist(int ?CustId,... ?BVN,...) 

添加“?”使属性能够接受空值,如果数据库中的列可以接受空值,它必须工作。

【讨论】:

当单元格的值为空时,这段代码会抛出异常。 我收到了OfficeOpenXml.ExcelRangeBase.Value.get returned null.【参考方案2】:

您能否找到一种解决方法,首先将 Excel 文件转换为 CSV 文件?

【讨论】:

以上是关于值不能为空。 (参数's')尝试将excel文件上传到数据库时的主要内容,如果未能解决你的问题,请参考以下文章

Json Serializable - 无效参数(输入):不能为空

值不能为空。参数名称:starter中的connectionString appsettings.json

值不能为空。参数名称:dest

值不能为空。参数名称:httpContext

值不能为空。参数名称:尝试使用 web api 获取数据时的源

值不能为空。参数名称:元素