locked
Following Beth Massi video to create edit/update screen but getting code error RRS feed

  • Question

  • Hi

    I think I followed the instructions exactly as the video says but when it comes to writing the code to initialize the screen if parameters are passed versus the new data screen if no parms are passed, I get an error.

    First, here is the code...

    Private Sub CreateNewPLANODETAIL_InitializeDataWorkspace(saveChangesTo As List(Of
    Microsoft.LightSwitch.
    IDataService))

            If Me.JobId.HasValue Then

               Me.PLANODETAILProperty = Me.PLANODETAIL

            Else

               Me.PLANODETAILProperty = New PLANODETAIL()

            End If

    End Sub

    I added the "If", "else" and "End If" just as the video says but I get the following error on the Me.JobId.HasValue entry:

    HasValue is not a member of "short".

    The parameter I'm passing is JobId, it is defined as a "short integer" and bound to the JobId in the PlanoDetail record.

    Thanks



    • Edited by Jyuma1 Thursday, December 13, 2012 11:24 PM
    Thursday, December 13, 2012 11:22 PM

Answers

  • Do you have control over the schema?  If so, I would consider using one field, an integer with identity turned on, as the primary key.  So you would have ClientId, WorkOrderId, and WorkOrderDetailId, with zero or one to many relationships between them.  If you do it this way, LS will interpret the schema and provide out of the box options for you to add child records.  Setting properties of the child records in code would be easy as well.

    To answer your question, here is an example for setting a default value for the Client property when adding a new record.

    In your collection_Changed method use something like this:

    If e.Action = NotifyCollectionChangedAction.Add Then

    If (Me.ClientScreenParameter) IsNot Nothing)

    Dim q = From c In Me.DataWorkSpace.MyDataSource.Clients

    Where (c.Client = Me.ClientScreenParameter)

    Select q

    Dim r = q.FirstOrDefault

    If (r IsNot Nothing) Then

    Me.Client = r

    End If

    End If

    There are a few ways to do this but this approach should work (again, your schema doesn't make this any easier).

    To access the collection_changed event, click on the collection (left side) and use the expand the "write code" menu.

    To set defaults on the WorkOrderDetail,

    For Each w As WorkOrderDetail In Me.WorkOrderDetails

    w.MyParentProperty = Me.MyParentProperty

    Change MyDataSource to your datasource.  Change ClientScreenParameter to the name of your screen parameter for client.  Change MyParentProperty to your related property.

    Hope this helps.



    • Edited by Hessc Friday, December 14, 2012 6:02 AM clarification
    • Marked as answer by Jyuma1 Saturday, December 15, 2012 6:41 PM
    Friday, December 14, 2012 5:28 AM

All replies

  • Sorry... it was my mistake.  I forgot to set the parameter to not required.  

    Okay, now that the code doesn't produce any errors, the function I'm attempting to perform doesn't work.

    The new record I'm attempting to create has 3 keys... Client, Job and Sequence. The client Job and Sequence are screen parameters that I'm sending from the prior screen.  If the combination of screen parameters (Client/Job/Sequence) already exists I want to edit the existing record.  If the combination of Client/Job/Sequence does not exist I want to display the same screen but with the Client, Job and Sequence already filled in.  I thought the Beth Massi video showing how to create a New Data and Edit screen would do the job but I guess not. 

    Any suggestions?  

    • Edited by Jyuma1 Friday, December 14, 2012 1:15 AM
    Friday, December 14, 2012 12:22 AM
  • A couple of ideas.

    1. You are looking for 3 parameters but your code is only inspecting the JobId.  Did you try to add the other 2 parameters?

    2. If .HasValue is causing problems, try:

    If (Me.JobId <> Nothing) AndAlso (Me.Param2 IsNot Nothing) AndAlso (Me.Param3 IsNot Nothing) Then

    Me.PLANODETAILProperty = Me.PLANODETAIL

    Else

        Me.PLANODETAILProperty = New PLANODETAIL()

    End If

    Change Param2 and Param3 to your other screen parameters.

    3.  Try your code in the screen_Created method instead of InitializeDataWorkSpace.

    Friday, December 14, 2012 3:29 AM
  • Yes... I realized too late that I only included the single parameter in the code I posted but I do have all 3 parms accounted for in the actual program code.

    I think my problem is the fact that I'm just not doing this correctly. The Beth Massi video is showing how to use the same screen for new records and updating records. I'm trying to send the 3 parameters to the Create New Detail Screen and have the parms automatically fill-in the 3 fields on the new screen. 

    This is what I am doing…

    I have a screen that shows a WorkOrder header at the top and the child Workorder detail records in a grid at the bottom. I want to create a new child record based on the current key in the Workorder Header plus a sequence number to keep the keys unique.

     Lets say the WorkOrder is for Customer A and the WorkOrder number is 10.  Lets also say that there are 3 existing detail records for this WorkOrder.  The keys for the 3 existing WorkOrder detail records would therefore be as follows:

    Client  WorkOrder      Sequence#

    A                     10                    1

    A                     10                    2

    A                     10                    3

    Now I wish to add a 4<sup>th</sup> detail record.  The user would click the Add button which would call the Create New Detail Screen sending it 3 parameters parm1=A parm2=10 and parm3=4 (because the next available sequence nbr is 4)

    I would need the Create New Detail Screen to be initialized with those 3 parms so the Client on the Create New Detail Screen would be pre-filled with A and the WorkOrder would be pre-filled with 10 and the Sequence Nbr would be pre-filled with 4.   

    So I guess my question is… how do I get parameters sent from one screen to automatically populate the same fields in the Create New Detail screen?

    Thanks for your help

     
    Friday, December 14, 2012 4:56 AM
  • Do you have control over the schema?  If so, I would consider using one field, an integer with identity turned on, as the primary key.  So you would have ClientId, WorkOrderId, and WorkOrderDetailId, with zero or one to many relationships between them.  If you do it this way, LS will interpret the schema and provide out of the box options for you to add child records.  Setting properties of the child records in code would be easy as well.

    To answer your question, here is an example for setting a default value for the Client property when adding a new record.

    In your collection_Changed method use something like this:

    If e.Action = NotifyCollectionChangedAction.Add Then

    If (Me.ClientScreenParameter) IsNot Nothing)

    Dim q = From c In Me.DataWorkSpace.MyDataSource.Clients

    Where (c.Client = Me.ClientScreenParameter)

    Select q

    Dim r = q.FirstOrDefault

    If (r IsNot Nothing) Then

    Me.Client = r

    End If

    End If

    There are a few ways to do this but this approach should work (again, your schema doesn't make this any easier).

    To access the collection_changed event, click on the collection (left side) and use the expand the "write code" menu.

    To set defaults on the WorkOrderDetail,

    For Each w As WorkOrderDetail In Me.WorkOrderDetails

    w.MyParentProperty = Me.MyParentProperty

    Change MyDataSource to your datasource.  Change ClientScreenParameter to the name of your screen parameter for client.  Change MyParentProperty to your related property.

    Hope this helps.



    • Edited by Hessc Friday, December 14, 2012 6:02 AM clarification
    • Marked as answer by Jyuma1 Saturday, December 15, 2012 6:41 PM
    Friday, December 14, 2012 5:28 AM
  • A small piece of advice. I wouldn't be using SmallInt as an ID (though from memory you're attaching to an existing database).

    A database that I've inherited uses SmallInt & there are times where it's just a nightmare (it was a while back, so I don't remember the specifics).


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Friday, December 14, 2012 5:34 AM
    Moderator
  • Yes I have control over the schema.   

    I'm attached to a SQL database that I've imported from an old Access database.  I've made so many changes to it already, so what's one more? :)  

    If I can find out what "with identity turned on" means, I'll give it a try in the morning and let you know.  

    Thanks for your help. 

     
    Friday, December 14, 2012 6:35 AM
  • @Yann... I can change the SmallInt if you really think it's worth it.  No big deal at this point.  What should it be?  Integer?
    Friday, December 14, 2012 6:37 AM
  • Sorry for the delayed response... I was called away for 2 days.

    I have the screen designer opened to the New Detail Screen and I'm attempting to do what
    Hessc suggested (to access the collection_changed event) but over on the left side there are two entries for the table named PLANODETAIL (which I assume to be synonymous with the collection).

    The first one is PLANODETAILProperty and the other (after the list of all the fields in the table) is just PLANODETAIL which is the query that I created that attaches the screen parameters to the fields in the PLANODETAIL table above.

    If I focus on the PLANODETAILProperty (at the top) and then use the drop-down on the "Write Code" the only selection available that contains the word "Changed" is the  PLANODETAILProperty_changed which is identified as a method not an event (are they the same?) 

    If I focus on the PLANODETAIL (the query that connects the screen parameters to the fields in the table above), the "Write Code" drop-down contains a selection for PLANODETAIL_Changed  but again it is a method not an event. 

    I don't know which to use or even if I'm in the right place to begin with.

    A gentle push in the right direction please.

    Thanks

    • Edited by Jyuma1 Saturday, December 15, 2012 12:44 PM
    Saturday, December 15, 2012 12:42 PM
  • I got it to work by what is likely an unconverntional approach but for the purposes of this project, it is better to leave the schema as is... on all future projects I'll create a more contemporary style schema. 

    @Yann... you will likely recognize my approach as clearly old-school.  :)~

    I added the following code to the IntilizeDataWorkspace method. 

    Private Sub CreateNewPLANODETAIL_InitializeDataWorkspace(saveChangesTo As List(Of Microsoft.LightSwitch.IDataService))

                If Me.Job_No.HasValue Then

                    Me.PLANODETAILProperty = New PLANODETAIL()

                    Me.PLANODETAILProperty.Client = Client

                    Me.PLANODETAILProperty.Job_No = Job_No

                Else

                    Me.PLANODETAILProperty = New PLANODETAIL()

                End If

            End Sub

    The two "New PLANODETAIL()" statements are probably not required... I could have gotten away with one but what the heck.  

    Now I need to add the third parm... Job_No_Seq... and that will do the trick.

    Thanks to both of you for your kind assistence and patience.

    Ed

     

    Saturday, December 15, 2012 2:06 PM
  • Glad you got it working.  Sometimes you just have to play around with it.
    Saturday, December 15, 2012 4:23 PM
  • Yup, but I learned a lot from what you posted.  I may have gotten it to work but I didn't code it the proper way.  I should have set the tables up the way you suggested... next time I will.

    Thanks for your help.

    Saturday, December 15, 2012 6:40 PM
  • @Yann... is there a 2012 version of your book?  email me.
    Saturday, December 15, 2012 7:46 PM
  • LightSwitch will be much happier with Integer PK's, yes.

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Monday, December 17, 2012 4:38 AM
    Moderator
  • You're welcome Ed!

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Monday, December 17, 2012 4:39 AM
    Moderator