0

I am using lightswitch and i was able to set a default value for a field using the Created() method. Now I can explicitly set this value. My query has a parameter that filters it. I want to set my default value of the field to which ever dynamic value is going to be supplied. how do i do that? Below is what i have..

partial void Payperiod_Created()
{

    this.EstateID = 5;
}

Edited

I created a method method in public partial class sspDataService .. the data serivce class that is going to return the value that i want. My intention here was to create a global variable.

public string estateName()
{

    string esName = "";
    string uName = this.Application.User.Identity.Name;
    try
    {
        var qryUser =

            this.aspnet_Users.Where(a => (a.UserName == uName)).SingleOrDefault();
        esName = qryUser.PayGroup;

    }
    catch (Exception e)
    {

        Debug.WriteLine(e.InnerException.ToString());
    }
    return esName;
}

However i cannot access this method inside my Payperiod_Created() method. Hope this helps.

Maverick1415
  • 113
  • 1
  • 12

2 Answers2

0

Can you elaborate? (Specifically, how will the value be supplied?)

For the method specifically, you simply need to pass the value into the method itself, so that the definition for your method becomes :

partial void Payperiod_Created(int newVal)
{
    this.EstadeID = newVal;
}

And then wherever you call the method you include the new value :

Payperiod_Created(5);    //this will EstateID to 5
0

You might declare the parameter as an Nullable Int (int?) whcih is a variable type that accepts either null or int values. Then check for null value provided, if is is null then you can set as the default value:

public void ExecuteQuery(int? myParam)
{
   EstateId = myParam ?? EstateId;
   // Execute your query...
}

Now, expression EstateId = myParam ?? EstateId; is an IsNull operator, equivalent to "set EstateId value to myParam values, unless it is null, which then set it to whatever EstateId values now", or in other words to:

if(myParam != null) { EstateId = myParam; } 
SalvadorGomez
  • 552
  • 4
  • 15