views:

174

answers:

1

I can't figure out the correct way to insert a new record in a child table.

There's a single datacontext in the app and the target table (CustNotes) is a child of a table named Customer. There's a one to many association between Customer and CustNotes. The datacontext name is CustomerOrdersDataContext.

Here's the code I'm using:

private void Button_Click(object sender, RoutedEventArgs e)
    {   
       int newSeqNumber;
       FindID = (int)lstCustomerNames.SelectedValue;

               var CustNoteNum = 
                   (from c in dbC.CustNotes                          
                    where c.CustomerID == FindID
                    select new 
                    {c.NoteOrder}).Max(c => c.NoteOrder);

        newSeqNumber = CustNoteNum + 1;

        CustomerOrdersDataContext notes = new CustomerOrdersDataContext();

        CustNote newNote = new CustNote();

        newNote.Note = NewNote.Text;
        newNote.NoteOrder = (byte)newSeqNumber;
        newNote.CustomerID = FindID;

        ***notes.CustNotes.Add(newNote);***  
        notes.SubmitChanges();
    } 

The error relates to the bold, italic line. Here it is:

System.Data.Linq.Table' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Data.Linq.Table' could be found (are you missing a using directive or an assembly reference?)

Anybody have a clue that'll help.

A: 

Instead of:

notes.CustNotes.Add(newNote);

You should do:

notes.CustNotes.InsertOnSubmit(newNote);
Keltex
Terrific help. Thanks. Not only did that work, but it also pointed up another bug... newNote.Note = NewNote.Text; should have been newNote.Note = newNoteText.Text;
Jack McG