locked
Defining Default Values for Related Entities RRS feed

  • Question

  • I have a screen for "patients" PPIsheets table defining the product "Allergies" Allergy table one of the allerygy

    properties is the related table MiniMonogram "SpecificDrugName" with a summary property of TradeGenericNames Allergies

    also has a property in the Allergy tabe TypeOfAllergy.

    I wanted the box to initially fill in "NKDA" but I could not define Type of allery this way without error. when I opened

    the Allergy screen.
        public partial class AllergiesListDetail
        {

            partial void AllergiesListDetail_Created()
            {
                this.Allergies.SelectedItem.TypeOfAllergy = "None";
                //this.PPIsheets.SelectedItem.Allergies = this.Allergies.SelectedItem.TypeOfAllergy = "KNDA";
            }

    So I went to the code for the allery table and this worked.
        public partial class Allergy
        {
            partial void Allergy_Created()
            {
                this.TypeOfAllergy = "NKDA";
            }
        }

    I tried the same on the MiniMonogram table but the following code did nothing to fill in the combo box for the screens.
            partial void MiniMonogram_Created()
            {
                this.TradeGenericNames = "None";
                //this.DataWorkspace.ApplicationData.MonogramNameNone();
            }
    I also tried these patterns on the Allergies screen which is a List and Detail screen for "Patients" PPISheets table which

    has a child table Alergy "Alergies". PPIsheets is one to Many Allergies, Allergies is Many to One MiniMonograms
        public partial class AllergiesListDetail
        {

            partial void AllergiesListDetail_Created()
            {
                this.Allergies.SelectedItem.SpecificDrugName.TradeGenericNames = "None";
                //SpecificDrugName = MiniMonogram summary property TradeGenericNames
                //Error 1

                this.Allergies.SelectedItem.SpecificDrugName = this.DataWorkspace.ApplicationData.MonogramNameNone();
                //Query MonogramNameNone where TradeGenericNames = "None"
                //Error 2
            }


    -Error 1    NullReferenceException was unhanndled by user code
                {System.NullReferenceException: Object reference not set to an instance of an object.
                at LightSwitchApplication.AllergiesListDetail.AllergiesListDetail_Created()
                at LightSwitchApplication.AllergiesListDetail.DetailsClass.__AllergiesListDetail_InvokeCreated

    (AllergiesListDetail s)
                at

    Microsoft.LightSwitch.Details.Framework.Client.ScreenDetails`2.<Microsoft.LightSwitch.Client.Implementation.Internal.IScre

    enDetailsImplementation.OnScreenUILoaded>b__1a()
                at Microsoft.LightSwitch.Utilities.Internal.UserCodeHelper.CallUserCode(Type sourceType, String methodName,

    String instance, String operation, ILoggingContext context, Action action, String additionalText, Func`1

    getCompletedMessage, Boolean tryHandleException, Boolean swallowException, Exception& exception)}

    -Error 2    Cannot implicitly convert type
                'Microsoft.LightSwitch.IDataServiceQueryable<LightSwitchApplication.MiniMonogram>'
                to 'LightSwitchApplication.MiniMonogram'

    The screen looks like this.


    • Edited by reigh7 Tuesday, January 20, 2015 8:30 PM Grammar
    Tuesday, January 20, 2015 8:29 PM

Answers

  • I found the answer by asking another forum question. I made a single property on "PPI sheet" Table called "Patient Allergies" a calculated field with a default string "NKDA" No Known Drug Allergies. Then copied the rows for each patient from the related table "Allergy" Summary computed Property to the Patient allergies Property on the PPI sheet table. See code Blocks Below

    namespace LightSwitchApplication
    {
    	public partial class PPIsheet
    	{
    		partial void PatientAllergies_Compute(ref string result)
    		{
    			// Set result to the desired field value
    			result = string.Empty;
    			foreach (var allergy in Alergy)
    			{
    				result += allergy.AllergySummary + ", ";
    			}
    			if (result.Length > 2)
    				result = result.Substring(0, result.Length - 2);
    			else if (result == string.Empty)
    				result = "KNDA";
    		}
    	}
    }

    ...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.LightSwitch;
    namespace LightSwitchApplication
    {
    	public partial class Allergy
    	{
    		partial void AllergySummary_Compute(ref string result)
    		{
    
    			// handle Nulls
    			
    			//make some string vars with defaults
    			string genName = "";
    			string classSum = "";
                		string generic = "";
               		string classification = "";
    			
    			//set string vars only if Monogram not null
    			if(Monogram != null)
    			{
    				//use null coalesce operator '??' to convert null properties to string
    				genName = Monogram.GenericName ?? "None Listed";
    				classSum = Monogram.ClassificationSummary ?? "No Classification";
                    		generic = " / Generic Drug: ";
                    		classification = " / ";
    			}
                		if (classSum == "")
                    	classification = "";
    
    			result = "(Type: " + TypeOfAllergy + generic + genName + classification + classSum + ")";
    		}
    	}
    }

    • Marked as answer by reigh7 Sunday, March 1, 2015 1:59 AM
    Sunday, March 1, 2015 1:58 AM

All replies

  • It is not totally clear from your description how your model is set up, but you should use the SelectedItemChanged event rather than Created.  Also, you can't assign foreign key entities directly from a string field, you have to assign the entity itself.  The best way to do that is by a lambda expression like this:

    // check first if the TypeOfAllergy has not already been set
    if (this.Allergies.SelectedItem.TypeOfAllergy == null) 
    {this.Allergies.SelectedItem.TypeOfAllergy = 
    this.DataWorkspace.YourDataSourceName.MiniMonograms
    .Where(x => x.TypeOfAllergy == "KDNA").FirstOrDefault();
    }
    
    

    This way you query the datasource for the entity that you want to assign and then assign the entity.

    Hopefully this gets you closer to the solution.



    • Edited by Hessc Wednesday, January 21, 2015 4:47 AM
    Wednesday, January 21, 2015 4:43 AM
  • Thank you for trying to help. I was always able to define the default value from the created code for the Allergy table entity for the property TypeOfAllergy. I was trying to define the default value for SpecificDrugName which was a related value on the Allergy table entity. I tried doing the same code I did on the Allergy table on the MiniMonogram table but for the TradeGenericName summary property but it did not show any values in the combobox control for the Allergy screen or for any other screens that used that summary property. I tried playing with the created code for the allergy screen as there is no Selected Item Changed event in the code drop down for screens. In the end I had issues with a different screen for placing orders for medications. I had wanted a whole table like the one on the Allergies screen to appear when a patient name was selected but I could not get that to work with the relationships. I have decided for now to make Allergies a single property string field on the patient table.
    Saturday, January 24, 2015 3:16 AM
  • I found the answer by asking another forum question. I made a single property on "PPI sheet" Table called "Patient Allergies" a calculated field with a default string "NKDA" No Known Drug Allergies. Then copied the rows for each patient from the related table "Allergy" Summary computed Property to the Patient allergies Property on the PPI sheet table. See code Blocks Below

    namespace LightSwitchApplication
    {
    	public partial class PPIsheet
    	{
    		partial void PatientAllergies_Compute(ref string result)
    		{
    			// Set result to the desired field value
    			result = string.Empty;
    			foreach (var allergy in Alergy)
    			{
    				result += allergy.AllergySummary + ", ";
    			}
    			if (result.Length > 2)
    				result = result.Substring(0, result.Length - 2);
    			else if (result == string.Empty)
    				result = "KNDA";
    		}
    	}
    }

    ...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.LightSwitch;
    namespace LightSwitchApplication
    {
    	public partial class Allergy
    	{
    		partial void AllergySummary_Compute(ref string result)
    		{
    
    			// handle Nulls
    			
    			//make some string vars with defaults
    			string genName = "";
    			string classSum = "";
                		string generic = "";
               		string classification = "";
    			
    			//set string vars only if Monogram not null
    			if(Monogram != null)
    			{
    				//use null coalesce operator '??' to convert null properties to string
    				genName = Monogram.GenericName ?? "None Listed";
    				classSum = Monogram.ClassificationSummary ?? "No Classification";
                    		generic = " / Generic Drug: ";
                    		classification = " / ";
    			}
                		if (classSum == "")
                    	classification = "";
    
    			result = "(Type: " + TypeOfAllergy + generic + genName + classification + classSum + ")";
    		}
    	}
    }

    • Marked as answer by reigh7 Sunday, March 1, 2015 1:59 AM
    Sunday, March 1, 2015 1:58 AM