korrowan korrowan -4 years ago 91
C# Question

How to get pass this error "Value cannot be null. Parameter name: entity"

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.

Answer Source

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();
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download