Hi, I have a small problem, I am trying to insert a new row into a batabase table using linq to sql.

I have Microsoft publication on Linq but it was based on the beta of Linq and does not match the current version completly.

I have a class P11Entry that is laid out exactly as the db table.

I have the Linq data context named dc

however I am getting a syntax error that I do not understand.

LinqDataContext dc = new LinqDataContext();
P11Entry anEntry = new P11Entry;

anEntry.Week_Number = intWeekNum;
anEntry.Week_Begining = dtWeekStart;
anEntry.EmpID = intEmpID;
anEntry.Gross_Pay = decPay;
anEntry.Tax = decTaxDue;
anEntry.NICs = decNICs;

dc.tblP11.InsertOnSubmit(anEntry);//SYNTAX ERROR

The exception is a type exception and I do not understand why. Please can anyone help?

Thanks for your time.

P11Entry anEntry = new P11Entry;
looks weird, should this not be
P11Entry anEntry = new P11Entry();?
Does InsertOnSubmit expects a parameter of type P11Entry?

Hi there thanks for the response. Sorry you were wright that was a typo. However I have solved the insert new problem, I was using the wrong class object.

However I now have an update issue that I do not understand, the code is similar but has this line instead

 //Update an egsisting table entry
tblP11 anEntry = dc.tblP11s.Single(u => u.Week_Number == newP11.Week_Number && u.EmpID == newP11.EmpID && u.Contract_Start == newP11.Contract_Start);//Error on this line
if (anEntry != null)
anEntry.Gross_Pay = newP11.Gross_Pay;
anEntry.Tax = newP11.Tax;
anEntry.NI_cuntributions = newP11.NI_cuntributions;

I have not put the catch for the try block in as to see the problem I do not think it is needed.

The problem is the table that is being inserted into has a three part composit primary key. I need to ensure that the correct record is updated.

However the Linq statement is producing an error, of the same type as previously.

Any help would be great thank you.

This article has been dead for over six months. Start a new discussion instead.