Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi.

i have a database by following diagram.

My Diagram

when i want to update a record from LINK tables Like LinkStudentClass with following code.

private void btnSave_Click(object sender, EventArgs e)
        {
            var LSC = (from l in Program.scdc.LinkStudentClasses
                                    where l.ClassID == Program.SelectedClassID && l.StudentID == Program.SelectedStudentID
                                    select l).Single();

            int ChangedStudentId = Convert.ToInt32(DDLStudents.Text.Split('-')[0]);
            LSC.ClassID = Program.SelectedClassID;
            LSC.StudentID = ChangedStudentId;
            Program.scdc.SubmitChanges();
            MainForm.NeedRefreshForm = true;
            this.Close();
        }


and i get following error.

Error

WHY?

What I have tried:

I realized that I had to delete the record and create a new record like this one.

private void btnSave_Click(object sender, EventArgs e)
        {
            var LSC = (from l in Program.scdc.LinkStudentClasses
                                    where l.ClassID == Program.SelectedClassID && l.StudentID == Program.SelectedStudentID
                                    select l).Single();

            int ChangedStudentId = Convert.ToInt32(DDLStudents.Text.Split('-')[0]);
            Program.scdc.LinkStudentClasses.DeleteOnSubmit(LSC);
            Program.scdc.SubmitChanges();
            LSC = new LinkStudentClass();
            LSC.ClassID = Program.SelectedClassID;
            LSC.StudentID = ChangedStudentId;
            Program.scdc.LinkStudentClasses.InsertOnSubmit(LSC);
            Program.scdc.SubmitChanges();
            MainForm.NeedRefreshForm = true;
            this.Close();
        }



but how can update directly?
Posted
Updated 2-Feb-19 7:20am
v5
Comments
MadMyche 1-Feb-19 15:04pm    
Please post your code and not link to images of it.
1. Many people will not click on the links.
2. Hard to copy/paste code from an image.

1 solution

The exception is being generated (thrown) by your "application code".

Only you know what's in the variables in your "error image", and why. And which you're not even referencing in the code above.
 
Share this answer
 
Comments
Maciej Los 3-Feb-19 15:22pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900