LightSwitch – Setting default Values for DB Table Columns

Background

Evaluating Microsoft’s LightSwitch for a quick Data Entry Job.

This is my very first application and so it will be basic.

 

Need

Screen

Data Entry Screen

Initial

screen-20161022-1129am

 

Save

When we try to save, we get the error message pasted below:

It states “This field is required“.

screen-20161022-1130am-this-field-is-required

 

Resolution

Data Sources

Set Data Source elements data element fields values to the values you want them to be.

To do so please follows the steps outlined below:

  1. Access Data Source element
  2. Choose Data Object
  3. Access the Write Code menu
    • On the Server Tab
      • General Methods – …. Inserting
    • Or, On the HTMLClient Tab
      • General Methods – … Created

Server

_Created

Menu

datasource-server-writecode-generalmethods-created

 

Code

Here is the C# Code


using Microsoft.LightSwitch;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;

namespace LightSwitchApplication
{
    public partial class listofServersAlt
    {

        partial void listofServers_Inserting(listofServer entity)
        {

            const String guidEmpty = "00000000-0000-0000-0000-000000000000";

            String sequence = null;

            //get sequence as empty GUID
            sequence = guidEmpty;

            //get Sequence from entity sequence field
            if (entity.sequence != null)
            {
                sequence = entity.sequence.ToString();
            }

            //if sequence is empty, then set to new GUID
            if (guidEmpty.Equals(sequence))
            {
                //Guid.NewGuid Method ()
                //https://msdn.microsoft.com/en-us/library/system.guid.newguid(v=vs.110).aspx
                entity.sequence = System.Guid.NewGuid();

            } // if (guidEmpty.Equals(sequence))


        } // listofServers_Inserting

    }

}

 

_Created

Method

On the Data Source, access Write Code \ General Methods \ created

datasource-htmlclient-generalmethods-created-menu

Code

Here is the javascript Code



/// <reference path="~/GeneratedArtifacts/viewModel.js" />


/*
    a)  GUID: The GUID Generator
    http://guid.us/GUID/JavaScript

*/

function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

function newGUID() {
    // then to call it, plus stitch in '4' in the third group
    guid = (S4() + S4() + "-" + S4() + "-4" + S4().substr(0, 3) + "-" + S4() + "-" + S4() + S4() + S4()).toLowerCase();

    return (guid);

}

function guidEmpty()
{
    var empty;
    
    empty = '00000000-0000-0000-0000-000000000000'

    return (empty);
}

myapp.listofServer.created = function (entity) {

    // Write code here.
    //entity.sequence = newGUID(); //System.Guid.NewGuid();

    entity.sequence = guidEmpty();
    entity.active = true;
    entity.iMonitorDatabaseBackup = true;
    entity.iMonitorServer = true;
    entity.iSupportsWinngmts = true;
    entity.passwordEncrypted = false;

    entity.dateCreated = new Date();

};

 

 

Validation

Let us go back and review the screen

screen-20161115-1055am-defaults-filled-with-zeroes

 

Explanation

  1. The sequence label is filled with a an empty GUID
  2. The date created filled is filled with current datetime
  3. And, the check-boxes are auto-filled as well

 

References

  1. Nicole Haugen
    • Microsoft – Developer Network
      • How To: Using the Created Method to Set Default Property Values
        Link
  2. Developer Networks
    • Getting Started with LightSwitch \ LightSwitch Guided Tour  \ Working with Data
      • Validating Data in a LightSwitch Application
        Link
    • Visual Studio 2015 > Visual Studio LightSwitch  > Data: The Information Behind Your Application
      • How to: Handle Data Events
        Link
  3. Tim Leung’s Blog
    • LightSwitch – In What Order do Screen Events Fire?
      Link
  4. Beth Massi
    • Beginning LightSwitch in VS 2013 Part 1: What’s in a Table? Describing Your Data
      Link
  5. Eric Erhardt
    • Updating Records in LightSwitch using Stored Procedures
      Link
  6. Gyan Sangrah
    • Default value in light switch 2011
      Link
  7.  GUID
    • GUID – The GUID Generator
      Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s