locked
Seting Default Values on Fields From related tables RRS feed

  • Question

  • I have an "entity table" that has a "related table" Entity table<----Many to One---->Related Table so I can query results with "Entity tables" Autocomplete box control and that entries can be dynamically entered into the "related table". This was setup using Lightswitch I know in light switch if I want to set a default value of a property that has a choice list I have to enter


    this
    .HowOrdered = "T.O.";

    Where T.O. is the stored value of a not the display name of the choice list and that instance works.

    I want to set up something similar for my dynamic choice list I setup in the "related table" so I tried.

    this.Procedure.Code = "0";

    Where Procedure is the name of the property relationship in the Entity and Code is the property in the related table. I get errors stating there is a null value problem when add new records with this line of code.  I have tried many other combinations of things like writing the _Created() method on the end of the Related table but this just gives me no default value on the Entity table. I also tried using other values like .ID but no luck how do I accomplish this task?

    • Moved by BonnieBMVP Saturday, September 21, 2013 3:48 PM might get better help here
    Thursday, September 19, 2013 9:25 PM

All replies

  • Does the procedure with Code == "0" already exist in the Related Table?  Is this code running in a Screen (client side) or on the DataSource (server side)?

    Assuming the RelatedTable with code=="0" is already present in the table you need to right click  on the table and 'Add Query'.  Create a query called ZeroCodeItem (or whatever) which returns the item with Code=="0".  Make sure to set 'Number of Results Returned' to 'One' for the query.

    If you want to do this client side then "Add Data Item..." to your screen and grab ZeroCodeItem that you just created.  Call the Data Item "ZeroCode"  Then 'Write Code' in the EntityTable_Created event to set this.Procedure = this.ZeroCode.

    If you are working server side then design the EntityTable and 'Write Code' for EntityName_created. Add the line

    this.Procedure = this.DataWorkspace.ApplicationData.ZeroCodeItem;

    There is a LightSwitch forum which may give you better quality answers

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=lightswitch


    Paul Linton

    Thursday, September 19, 2013 11:31 PM
  • I am not sure what "==" means but...

    Yes I already populated it to the table with the codes on it natively and am trying to reference it any way I can on the table that is receiving the summary property via a relationship. BTW when I ran the code this.HowOrdered = "T.O."; in instances were there was not a choice and not on another table it did not matter if it was an existing entry yet or not the code simply defined the "default" string or int it should be if no one changed it. does that change with choice lists and summary property lists? I already am nervous that someone who installs this may have issues automating the import of the codes because there are well over 1000 and Lightswitch changed and now limits imports to 1000 entries at a time for a table. I just don't want to assume someone will install this and then not have the entry it is querying because the automated upload doesn't work either as planned. I may have to look into some type of service/shared database but just wondering if the entry has to exist for it to be a default value to be defined?

    Friday, September 20, 2013 2:15 AM
  • If you don't know what "==" is then I think it would be a good idea for you to pause you current project and devote some time to learning the very basics of C#.  There are a lot of on-line materials, books and courses available.

    Can you simplify your questions a little bit?   Sentences like

    BTW when I ran the code this.HowOrdered = "T.O."; in instances were there was not a choice and not on another table it did not matter if it was an existing entry yet or not the code simply defined the "default" string or int it should be if no one changed it. does that change with choice lists and summary property lists?

    Are almost impossible to understand.  How did you "ran the code this.HowOrdered "?  What is the type of 'this'?  What is the type of 'HowOrdered"?  Are you saying that you have a problem?  Or that you do not have a problem?

    What are you nervous about?  What is going to be installed?

    Are you comfortable with the difference between a native sql value and a foreign key?  I think this is at the root of your confusion.


    Paul Linton

    Friday, September 20, 2013 3:14 AM
  • But isn't == a Boolean operator? I am defining default values in code snippets written in C# held within a Lightswitch project and we are talking about three different models here.

    Table Property(Native Key)-------> Default Value  works

    Table connected by a relationship and native to the project (foreign Key)--------->Default "Value does not work yet"

    Table connected for an external database or service (foreign Key)--------->Default Value "theorized it may need to be set up this way in the future"

    The Whole Classes Code assuming a Native key is as follows

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.LightSwitch; namespace LightSwitchApplication {     publicpartialclassMedicationOrderDetail     {         partialvoid MedicationOrderDetail_Created()         {             this.HowOrdered = "T.O.";

    //NATIVE KEY WORKS FINE

                //this.Procedure.Code = "0";             //this.DiagnosisItem.Code = "0";

    //THESE ARE FORIEN KEY PROPERTIES I DO NOT KNOW HOW TO WRITE THE ADDRESS TO THEM WITHOUT GETTING A NULL VALUE ERROR

            }     } }

        
    Saturday, September 21, 2013 12:32 AM
  • //THESE ARE FORIEN KEY PROPERTIES I DO NOT KNOW HOW TO WRITE THE ADDRESS TO THEM WITHOUT GETTING A NULL VALUE ERROR

    Maybe you could try doing what I told you in a previous post.


    Paul Linton

    Saturday, September 21, 2013 7:27 AM
  • OK what is that?
    Do you mean study C# code?

    I read through a two inch thick binder of the C# code standard. As for tutorials they are harder to come by because of dependencies for each tutorial determining an order to them and actually finding ones with relevant code and not just consol.writeline "hello world" over and over for each explanation of a TYPE weather struct, Class parameter, property ect... I have the folder for instance of "Official Visual Studio 2010 Samples for C# 4.0". I have no idea what order to go at them and the only one on Operators only shows how do demonstrate overloading them.

    • Edited by reigh7 Saturday, September 21, 2013 3:44 PM added questions
    Saturday, September 21, 2013 3:17 PM
  • Reigh, I went ahead and moved your thread to the LightSwitch forum. I hope this is more appropriate for your questions ...

    ~~Bonnie Berent DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, September 21, 2013 3:50 PM
  • Thank you it is hard to know where to post this stuff at times since it involves several technologies.
    Saturday, September 21, 2013 3:52 PM
  • Yes, I agree. I don't know if this forum will be any better, but since you used LightSwitch to begin with, others here that use LightSwitch might know more about what you're asking about. We'll see ......

    ~~Bonnie Berent DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, September 21, 2013 3:58 PM
  • OK what is that?

    I have made three posts in this thread (prior to this one).  Look carefully at all of them.  One contains code plus instructions which cover two different possible scenarios.  That is it.


    Paul Linton

    Saturday, September 21, 2013 10:31 PM
  • Ok on the procedure table (the related table) I set up a query named...

    ProcedureNotApplicable
         Filter
              where ProcedureName = literal NOT APPLICABLE

    then on the entity table I wrote

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.LightSwitch;
    namespace LightSwitchApplication
    {
        public partial class MedicationOrderDetail
        {
            partial void MedicationOrderDetail_Created()
            {
                this.HowOrdered = "T.O.";
                this.Procedure = this.DataWorkspace.ApplicationData.ProcedureNotApplicable;
                //this.DiagnosisItem.Code = "0";
            }
        }
    }

    but it is still not accepting this code "this.DataWorkspace.ApplicationData.ProcedureNotApplicable;" saying "No modeled Description available" unless I make ProcedureNotApplicable a method like this

    this.Procedure = this.DataWorkspace.ApplicationData.ProcedureNotApplicable();


    • Edited by reigh7 Wednesday, September 25, 2013 7:01 PM
    Wednesday, September 25, 2013 7:00 PM
  • Good.  It sounds like everything is working now.  Is that right?


    Paul Linton

    Wednesday, September 25, 2013 9:29 PM
  • yes I have a hard time though knowing when or when not to use the "()" operators in my code.
    Thursday, September 26, 2013 10:54 PM
  • It can be confusing but if you pay close attention to the Intellisense you can work it out.

    Background - if the item is a Property then there are no parenthesis, if it is a method call then there are parenthesis.  There really isn't a lot of difference between a Property get and a parameterless method call (in fact, a property get is effectively translated to a new parameterless method by the compiler)

    How to know when to use ()

    It depends on the version of the IDE you are using but the Intellisense will show you if you are accessing a Property or a Method.  If 'x' is a class which has Properties and methods then when you type

    x.

    you will see a popup menu with various icons down the side.  An icon that looks like a spanner(or wrench) is a Property and so there are no parenthesis needed.  In some IDEs you will see an icon that looks like a hand pointing at a dialog box.

    If it is a method then the icon looks like a cube.

    The other way to tell is to type the name and then type the open parenthesis

    x.UserName(

    if UserName is a method then a tooltip will appear giving you information about the method and its parameters.  If it is a Property then no tooltip will appear and you should delete the opening parenthesis that you typed.


    Paul Linton

    Thursday, September 26, 2013 11:25 PM