Problem InsertOnSubmit - Linq - ( class inherited from identity class )
In the code below I got an instantiating problem using Linq
Context = using this method SetMappinImport , I got a sorted list cotaining the Column mapping between the columns of an external file and the members of the class "Artikel" which is inherited from the identity class "Bibliotheek" in the Datacontext (dbml )
When I ran the solution I got an instantiating problem which I couldn't understand ...
Somebody an idea why ? Thx in advance
my code :
public void SetMappinImport(DataSet ds, string tabelName, SortedList <string,string> mapping)
{
List<Artikel> artikelenImport = new List<Artikel>();
DataClass.LqDataContext db = new DataClass.LqDataContext();
switchswitch
(tabelName)
{
case "Bibliotheek":
foreach (DataRow rw in ds.Tables[0].Rows)
{
Artikel art = new Artikel();
int i = 1;
i = ds.Tables[0].Columns.IndexOf(mapping[
"Groep"]);
art.Groep =
Convert.ToString (rw[i]);
i = ds.Tables[0].Columns.IndexOf(mapping[
"Omschrijving"]);
art.Omschrijving =
Convert.ToString(rw[i]);
i = ds.Tables[0].Columns.IndexOf(mapping[
"PopCode"]);
art.PopCode =
Convert.ToString(rw[i]);
i = ds.Tables[0].Columns.IndexOf(mapping[
"Brutto"]);
art.Brutto =
Convert.ToString(rw[i]);
i = ds.Tables[0].Columns.IndexOf(mapping[
"Referentie"]);
art.Referentie =
Convert.ToString(rw[i]);
i = ds.Tables[0].Columns.IndexOf(mapping[
"Ve"]);
art.Ve =
Convert.ToString(rw[i]);
db.Bibliotheeks.InsertOnSubmit(art); //<-Object reference not set to an instance of an object ????? art !=null & db.Bibliotheeks !=null
db.SubmitChanges();
artikelenImport.Add(art);
}
//db.Bibliotheeks.InsertAllOnSubmit(artikelenImport ); ---> List<Artikel> arts
break;
case "Productlijnen":
// to-do
break;}
}
Answers
Hi bertoo,
This exception can be thrown when you use derived class from datacontext,
There are two workarounds for this issue,
1) Use partial class.
http://stackoverflow.com/questions/499436/linq-insertonsubmit-nullreferenceexception
2) Add additional properties
Does this work for you? If you have any questions or concerns, please update the thread and we will have a further discussion.
Best Regards
Yichun Feng
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
- Marked As Answer byYichun_FengMSFT, ModeratorWednesday, November 04, 2009 2:00 AM
All Replies
Hi bertoo,
This exception can be thrown when you use derived class from datacontext,
There are two workarounds for this issue,
1) Use partial class.
http://stackoverflow.com/questions/499436/linq-insertonsubmit-nullreferenceexception
2) Add additional properties
Does this work for you? If you have any questions or concerns, please update the thread and we will have a further discussion.
Best Regards
Yichun Feng
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
- Marked As Answer byYichun_FengMSFT, ModeratorWednesday, November 04, 2009 2:00 AM
Thank you for your usfull reply
an additional question :
Based on the first workaround ( partial class ) -> Am I right that I could create additional methods/fields to an inherited identity class from the datacontext?
Is this usefull ? or desired in a data layer for instance
Do you see practical circumstance where I should / could use the features / possibilities?
Best regards
bertooHi bertoo,
Since inherited class form datacontext will cause the problem you initially posted, I recommend that you just use partial class instead.
Best Regards
Yichun Feng
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.


