I did look at the related posts that were answered, but still get the NullReference exception in InsertOnSubmit(). I have an auto generated LINQ class Transaction from which I create a derived class that overrides ToString():
public partial class MyTransaction:DataAccess.Transaction
{
public MyTransaction():base()
{
}
public override string ToString()
{
return "some text";
}
}
And the insert is as follows:
public bool InsertTransaction(Transaction t)
{
using (MarketPricesDataContext dataContext = new MarketPricesDataContext(connectionString))
{
try
{
dataContext.Transaction.InsertOnSubmit(t);
dataContext.SubmitChanges();
return true;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
return false;
}
}
}
I thought calling the base contructor from MyTransaction would solve my problem, but it did not. I can insert Transaction items but when I try to insert a MyTransaction item I get a NullReferenceException. This works:
Transaction t=new Transaction();
t.Type="A"; //set some values
Data d=new Data();
d.InsertTransaction(t);
This throws a NullReferenceException (Object reference not set to an instance of an object.) in InsertOnSubmit():
MyTransaction myt=new MyTransaction();
myt.Type="A"; //set some values
Data d=new Data();
d.InsertTransaction(myt as Transaction);
What am I doing wrong?