JOE SKEET
JOE SKEET

Reputation: 8108

c# strange datatable nullreferenceexception

foreach (DataRow row in dttemp.Rows)
    dt_final.ImportRow(row);
foreach (DataRow row in dttemp1.Rows)
    dt_final.ImportRow(row);

i am getting this exception on the LAST line here

when i check the contents of row are NOT null

what am i doing wrong?

Upvotes: 0

Views: 1357

Answers (2)

Chad La Guardia
Chad La Guardia

Reputation: 5164

A NullReferenceException occurs usually when you try to invoke a member or method on a null object (in other words, when you use the . operator on something that is null). My best guess is that dt_final or perhaps dttemp1 as other users have suggested is null.

Upvotes: 2

Andrey
Andrey

Reputation: 60065

dt_final is definitely null, just because nothing else can be. The question is why it throws error only at last line. Reason is that dttemp.Rows is empty and first foreach is not executed.

Upvotes: 1

Related Questions