locked
Set relationship value of Child record to Parent record RRS feed

  • Question

  • I have two tables I am working with Equipment2Task and EquipmentTaskHistory the Parent table is Equipment2Task and the Child Table is EquipmentTaskHistory they are related though a field called Equipment2TaskID.

    When a value changes in the Parent table I am trying to insert a new record in the Child table but I can't seem to set the relationship between the two tables with the Equipment2TaskID

    Here is the code I am working with:

    Private Sub Jan_Changed()
    			If Me.Jan = False Then
    				Me.JanStatusType = Nothing
    				Dim taskHistory = Me.DataWorkspace.ServiceManagerData.EquipmentTaskHistories.AddNew()
    				taskHistory.EquipmentID = Me.Equipment.EquipmentID
    
    '**** Problem Line ****				
    'taskHistory.Equipment2Task = Me.Equipment2TaskID
    '*****************************************
    
    				taskHistory.TaskMonth = 1
    				taskHistory.Recommendation = "Unmarked"
    				taskHistory.StatusType = Nothing
    				taskHistory.TaskStatusActionDate = Now()
    				taskHistory.ModifiedBy = Me.Application.User.FullName
    				taskHistory.ModifiedDate = Now()
    				Me.DataWorkspace.ServiceManagerData.SaveChanges()
    			End If
    		End Sub
    
    I get the Error:(Value of type 'Integer' can not be converted to 'LightswitchApplication.Equipment2Task'

     

    Any ideas would be great...




    • Edited by bullet117 Wednesday, October 19, 2011 3:38 PM
    Wednesday, October 19, 2011 3:36 PM

Answers

  • Hi again If yo have relationship as follows (one to many):

    ž

    then with this code (I do logic in on change of Name property in Parent entity Equipment2Task):

    Public Class Equipment2Task
    
            Private Sub Name_Changed()
                If Me.Id <> Nothing Then
                    Dim th As EquipmentTaskHistory
                    th = Nothing
    
                    Dim taskHistory = Me.DataWorkspace.ApplicationData.EquipmentTaskHistories.AddNew()
                    taskHistory.ModifiedBy = Me.Application.User.FullName
                    taskHistory.TaskMonth = 1
                    Me.EquipmentTaskHistory = taskHistory 'this is what you need
     
                    Me.DataWorkspace.ApplicationData.SaveChanges()
                End If
            End Sub
        End Class
    

    1. you insert new data in child entity (EquipmentTaskHistories) (Remarks: you should check if this data exist in child entity)

    2. you update data for foreign key in Parent table

    This surely work, try it (or I could provide you a Solution in SkyDrive)

    Hope this help

    Spaso Lazarevic


    • Edited by Spaso Lazarevic Wednesday, October 19, 2011 10:36 PM
    • Proposed as answer by Spaso Lazarevic Thursday, October 20, 2011 4:15 PM
    • Marked as answer by bullet117 Thursday, October 20, 2011 8:37 PM
    Wednesday, October 19, 2011 10:35 PM

All replies

  • Hi
    If you want just to add new data in Child table (Prop1, Prop2) and update property in Parent table (Prop3) use this code:
    Private Sub Jan_Changed()
    	If Me.Jan = False Then
    		Dim taskHistory = Me.DataWorkspace.ApplicationData.EquipmentTaskHistories.AddNew()
    	
    		taskHistory.Prop1 = 1 //Prop1 is property 1 in Child Entity
    		taskHistory.Prop2 = 2 //Prop2 is property 2 in Child Entity
    		Me.Prop3 = 3 //Prop3 is property from Parent
    		
    		Me.DataWorkspace.ApplicationData.SaveChanges() //Save changes
    	End If
    End Sub
    

    Hope this help
    Spaso Lazarevic
     


    • Edited by Spaso Lazarevic Wednesday, October 19, 2011 4:40 PM empty line added
    Wednesday, October 19, 2011 4:39 PM
  • Spaso, thanks for the response but the field (Equipment2Task) I am trying to set in the Child Table (Equipment2TaskHistory) is the relationship to the Parent Table (Equipment2Task), so Lightswitch treats it as a collection and not an Integer. 

    This is what is happening:

    When I type  taskHistory. I get a pop-up of the fields and the Parent Table (Equipment2Task which is a Collection), so I select the Patent Table (Equipment2Task) I then type = Me. and I get a list of the fields in the Parent Table(Equipment2Task) so I select the Equipment2TaskID (which is the linking field for the relationship between the two tables) I then get a blue squiggle line under Me.Equipment2TaskID and when I hover my mouse over it, it says. Value of type 'Integer' can not be converted to 'LightswitchApplication.Equipment2Task'

    It is giving me this error because the Equipment2Task "field" in the Equipment2TaskHistory table is a collection and not and Integer field because of the relationship.

    So how do I set the vale of the field Equipment2Task Collection in the Equipment2TaskHistory so it is associated with the Equipment2Task collection...


    • Edited by bullet117 Wednesday, October 19, 2011 6:44 PM
    Wednesday, October 19, 2011 5:17 PM
  • Hi again If yo have relationship as follows (one to many):

    ž

    then with this code (I do logic in on change of Name property in Parent entity Equipment2Task):

    Public Class Equipment2Task
    
            Private Sub Name_Changed()
                If Me.Id <> Nothing Then
                    Dim th As EquipmentTaskHistory
                    th = Nothing
    
                    Dim taskHistory = Me.DataWorkspace.ApplicationData.EquipmentTaskHistories.AddNew()
                    taskHistory.ModifiedBy = Me.Application.User.FullName
                    taskHistory.TaskMonth = 1
                    Me.EquipmentTaskHistory = taskHistory 'this is what you need
     
                    Me.DataWorkspace.ApplicationData.SaveChanges()
                End If
            End Sub
        End Class
    

    1. you insert new data in child entity (EquipmentTaskHistories) (Remarks: you should check if this data exist in child entity)

    2. you update data for foreign key in Parent table

    This surely work, try it (or I could provide you a Solution in SkyDrive)

    Hope this help

    Spaso Lazarevic


    • Edited by Spaso Lazarevic Wednesday, October 19, 2011 10:36 PM
    • Proposed as answer by Spaso Lazarevic Thursday, October 20, 2011 4:15 PM
    • Marked as answer by bullet117 Thursday, October 20, 2011 8:37 PM
    Wednesday, October 19, 2011 10:35 PM
  • I got it to work...Thanks Spaso
    Thursday, October 20, 2011 2:45 PM