How to get pass this error "Value cannot be null. Parameter name: entity"
Solution 1
matchedCaseNumber
is probably null
when passed to DeleteOnSubmit
if(matchedCaseNumber != null)
{
dc.reportsSents.DeleteOnSubmit(matchedCaseNumber);
dc.SubmitChanges();
}
EDIT: If I understand correctly what you're trying to do, this would be better:
private void button1_Click(object sender, EventArgs e)
{
DataClasses1DataContext dc = new DataClasses1DataContext();
foreach (var item in dc.reportsSents)
{
(new MyReportRenderer()).RenderTest(Convert.ToString(item));
dc.reportsSents.DeleteOnSubmit(item);
}
dc.SubmitChanges();
}
Solution 2
You have
var matchedCaseNumber = (...).FirstOrDefault();
dc.reportsSents.DeleteOnSubmit(matchedCaseNumber);
So what if matchedCaseNumber
is null? The OrDefault
makes that possible.
Change that 2nd line into:
if (matchedCaseNumber != null)
dc.reportsSents.DeleteOnSubmit(matchedCaseNumber);
Also,
int rowCount = dc.reportsSents.Count();
for(int i = 0; i <= rowCount;)
{
...
i = (i +1);
}
should probably become:
for(int i = 0; i < rowCount; i += 1) // note: NOT <=
{
...
}
You are looping rowCount+1 times. And there really is no point in moving the increment of i
out of the loop statement. Don't confuse/hinder opitimizers.
Solution 3
It means that matchCaseNumber
is null
.
When you create matchCaseNumber you use .FirstOrDefault()
which returns the first instance or the default value for that type (which is null
for reference types).
In other words, your query:
from CaseNumberKey in dc.GetTable<reportsSent>()
select CaseNumberKey
appears to be returning no data.
korrowan
Updated on June 05, 2022Comments
-
korrowan almost 2 years
private void button1_Click(object sender, EventArgs e) { DataClasses1DataContext dc = new DataClasses1DataContext(); var rec = dc.reportsSents.FirstOrDefault(); int rowCount = dc.reportsSents.Count(); if (rec != null) { for(int i = 0; i <= rowCount;) { var matchedCaseNumber = (from CaseNumberKey in dc.GetTable<reportsSent>() select CaseNumberKey).FirstOrDefault(); (new MyReportRenderer()).RenderTest(Convert.ToString(matchedCaseNumber)); dc.reportsSents.DeleteOnSubmit(matchedCaseNumber); dc.SubmitChanges(); i = (i +1); } }
When the code above is executed I get this error:
Value cannot be null. Parameter name: entity
and matchedCaseNumber is highlighted in this line:
dc.reportsSents.DeleteOnSubmit(matchedCaseNumber);
What does this mean and why am I getting it. What I am trying to do is pass MatchedCaseNumber to the method and then delete it after the method is executed and then step through the table. Any help would be appreciated.
Thanks.