locked
Code breaks using the Savechanges method RRS feed

  • Question

  • Hello,

    I have a block of code that adds entities to multiple tables.  The first is the Students table which has several relationships and required fields - this code works as expected.  Then I have the Parents table which currently has a one to many related table and six required fields.  No fields have custom validation and I'm accounting for the fields that are required in my code - and I get a ValidationException error when I use the SaveChanges method:

     

    If Me.ApplicationsSet.SelectedItem.ApplicationStatus <> "Pending" Then Me.ShowMessageBox("You can only enroll applicants that have a status of 'Pending'.", "Invalid Enrollment", MessageBoxOption.Ok) Exit Sub End If If Me.ShowMessageBox("Are you sure you want to enroll this applicant?", "Please Verify", MessageBoxOption.YesNoCancel) = MessageBoxResult.Yes Then 'This starts the process of moving the applicant and their family into the school regular tables. First add applicant into student table. Dim appl As Applications = Me.ApplicationsSet.SelectedItem Dim newStud As Students = New Students With newStud .FirstName = appl.ChildFirstName .MiddleName = appl.ChildMiddleName .LastName = appl.ChildLastName 'MPSStudenID will need to be inserted .Grade = appl.GradeInFall 'Room will need to be inserted .DateOfBirth = appl.DateOfBirth .HomePhone = appl.Parent1HomePhone .Address = appl.Parent1Address .City = appl.Parent1City .State = appl.Parent1State .Zip = appl.Parent1Zip .SpecialCircumstances = appl.Question6 .Status = "Current" End With Me.DataWorkspace.ApplicationData.SaveChanges() 'Check to see if parent 1 already exists in the system. If not, create them in the parent table Dim newPar As Parents = New Parents Dim newVol As Volunteers = New Volunteers Dim newChild As Children = New Children Dim newPar2 As Parents = New Parents Dim newVol2 As Volunteers = New Volunteers Dim newChild2 As Children = New Children Dim newPickup As StudentPickupList = New StudentPickupList Dim newPickup2 As StudentPickupList = New StudentPickupList If appl.ExistingParent1 = "No" Then 'Add new parent to parent table With newPar .FirstName = appl.Parent1FirstName .LastName = appl.Parent1LastName .Address = appl.Parent1Address .City = appl.Parent1City .State = appl.Parent1State .Zip = appl.Parent1Zip .HomePhone = appl.Parent1HomePhone .CellPhone = appl.Parent1CellPhone .EmailAddress = appl.Parent1Email .Company = appl.Parent1Employer 'Are we adding job title? .BusinessPhone = appl.Parent1WorkPhone '.Status = "Current" End With Me.DataWorkspace.ApplicationData.SaveChanges()

    Can anyone see what I'm doing wrong?

    Thanks, Gene


    Tuesday, July 9, 2013 10:49 PM