0

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](IEnumerable1 source, TSource value, IEqualityComparer1 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

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
clc988
  • 1
  • 1
  • `EmailSender()` returns `null` – Fabio Nov 05 '19 at 06:53
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Sir Rufo Nov 05 '19 at 07:04
  • Hey thanks for the comment. how come on my program it shows mylist have called the values. What should I do? Please help. But my source is null – clc988 Nov 05 '19 at 07:04

1 Answers1

2

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);
}
Rawitas Krungkaew
  • 5,234
  • 1
  • 13
  • 28
  • Hi thanks for the reply, but when i executed , the program shows mylist have data. What should I do, please help me. Actually My source is null, but why? – clc988 Nov 05 '19 at 07:07
  • You'll have to debug inside function 'ToDataTable' in Extension.cs – Rawitas Krungkaew Nov 05 '19 at 07:18
  • I have fix the problem. However I could not export data to my excel. Mylist shows 358 records but could not gen in excel. Is there a war to gen to excel. Thanks. I think si the data table problem – clc988 Nov 05 '19 at 08:11