简体   繁体   中英

Exception: System.ArgumentNullException: Value cannot be null

I'm trying to call the data to Excel and export the Excel in email using a stored procedure. However, I have error message showing

Exception:System.ArgumentNullException: Value cannot be null. Parameter name: source

I got an error message show it comes from my data table source

This is where I call my SQL from the stored procedure:

public List<DTO_List_Non_Upload> CallNonUpload()
{
    var value = db.Database.SqlQuery<DTO_List_Non_Upload>("[WPSV2_Get_Non_Upload_Photo_Information] @DateFrom, @DateTo, @contractNo",
                   // get the cdr date for today  parameter: datetime
                   new SqlParameter("@DateFrom", "20191001"),
                   // oc 
                   new SqlParameter("@DateTo", "20191009"),
                   new SqlParameter("@contractNo", "Patrolmanwe")).ToList();

    return value;
}

This is where I want I return the data as a function EmailSender() :

public List<DTO_List_Non_Upload> EmailSender()
{
    return patrol_Export_Services.CallNonUpload();
}

This is where I want to get my sql data to my Excel:

var mylist = new List<DTO_List_Non_Upload>();

mylist = EmailSender();

string filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, System.Configuration.ConfigurationSettings.AppSettings["Exportexcel"].ToString());

ExcelXlsx excelXlsx = new ExcelXlsx(filepath);

DataTable Source = mylist.ToDataTable<DTO_List_Non_Upload>();
excelXlsx.RenderDataTableToSheet(0, Source, 1, 0, false);

Error:

Exception: System.ArgumentNullException: Value cannot be null.

Parameter name: source

at System.Linq.Enumerable.Contains[TSource](IEnumerable 1 source, TSource value, IEqualityComparer 1 comparer)
at KS.Utils.Common.Extension.ToDataTable[T](IList`1 data, String[] ignoreProperties) in C:\coding\ks-photo\branches\NewOA4600007097_export\Patrol_Excel_Export\KS.utils\Common\Extension.cs:line 29
at Patrol_Excel_Export.SchedulerPatrolExport.run() in C:\coding\ks-photo\branches\NewOA4600007097_export\Patrol_Excel_Export\Patrol_Excel_Export\SchedulerPatrolExport.cs:line 93
at Patrol_Excel_Export.Program.Main(String[] args) in C:\coding\ks-photo\branches\NewOA4600007097_export\Patrol_Excel_Export\Patrol_Excel_Export\Program.cs:line 47

Looking at the stacktrace

at System.Linq.Enumerable.Contains[TSource](IEnumerable1 source, TSource value, IEqualityComparer1 comparer) at KS.Utils.Common.Extension. ToDataTable

mylist is null

if(mylist == null)
   // Throw exceotpion or do something else
else {
   DataTable Source = mylist.ToDataTable<DTO_List_Non_Upload>();
   excelXlsx.RenderDataTableToSheet(0, Source, 1, 0, false);
}

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