locked
Default Values on Created RRS feed

  • Question

  • I have a "Companies" table and Form Level Auth for a LS App.  I also have multiple 'users' per 'Company'.  In addition, I have a 'Projects' table.  There can be multiple projects per company and multiple users per company.  I have created a table that combines the 'users' and 'company' to create the proper relationship since the users are stored in the aspnet_Users table.  In this association table, I have a field for the user name which will be identical to the UserName in the aspnet_Users table.

    When a new project is created, there are a few default values that need to be entered into the Project record.  These defaults are stored in the Company table.  So, when a user creates a new project, the app needs to look up the default values for the company they belong to and assign to the project.  

    I had this working previously when I did not have multiple users per Company but rather only one user per company.  I used the following code:

    Private Sub CustomerDataWorkup_Created()
                Me.Value1= DataWorkspace.MyData.Companies.Where(Function(eb) eb.MasterLogin = Application.User.FullName).SingleOrDefault.Default_Value1
    
    End Sub

    with the new setup, I attempted to use this code:

    Me.Value1 = DataWorkspace.MyData.Companies.Where(Function(eb) eb.CompanyUsers.Any(Function(u) u.UserName = Me.Application.User.Name)).SingleOrDefault.Default_Value1

    but I get an error:  "Object reference not set to an instance of an object."  Both in debug and when published.

    Any help would be greatly appreciated!

    Saturday, November 1, 2014 12:25 PM

Answers

  • Not sure to understand the relationship...do you expect 0/1 entity or 0/n entities ? For 0/n you'll need to use FirstOrDefault.

    Anyway if I'm not wrong entity is a reference type and default is null/nothing, have you tried to assign SingleOrDefault result to a variable before using properties to check if there is at least one match in you query ?


    Marco


    • Edited by technetcolmar Sunday, November 2, 2014 5:01 PM
    • Marked as answer by Voyagr12 Wednesday, November 5, 2014 6:43 PM
    Sunday, November 2, 2014 5:01 PM
  • Hi Voyagr, when you're expecting 0 or multiple results you always need FirstOrDefault because SingleOrDefault:

    "Returns the only element of a sequence, or a default value if the sequence is empty"

    Additionally remember to check for null results because 'Default' on Entity is not an initialized object but a null/nothing.

    Glad it helped, if it's ok please remember to mark as answered or vote as helpful, this will help other people to find answers.


    Marco

    • Proposed as answer by technetcolmar Monday, November 3, 2014 6:48 PM
    • Marked as answer by Voyagr12 Wednesday, November 5, 2014 6:43 PM
    Monday, November 3, 2014 7:59 AM

All replies

  • Not sure to understand the relationship...do you expect 0/1 entity or 0/n entities ? For 0/n you'll need to use FirstOrDefault.

    Anyway if I'm not wrong entity is a reference type and default is null/nothing, have you tried to assign SingleOrDefault result to a variable before using properties to check if there is at least one match in you query ?


    Marco


    • Edited by technetcolmar Sunday, November 2, 2014 5:01 PM
    • Marked as answer by Voyagr12 Wednesday, November 5, 2014 6:43 PM
    Sunday, November 2, 2014 5:01 PM
  • Thanks Marco,

    the FirstorDefault worked.  I replaced the SingleorDefault with the FirstorDefault and all is now works as intended.  Not sure exactly why unless I am actually returning multiple records so the function doesnt know what to do?

    Thanks!

    Sunday, November 2, 2014 9:07 PM
  • Hi Voyagr, when you're expecting 0 or multiple results you always need FirstOrDefault because SingleOrDefault:

    "Returns the only element of a sequence, or a default value if the sequence is empty"

    Additionally remember to check for null results because 'Default' on Entity is not an initialized object but a null/nothing.

    Glad it helped, if it's ok please remember to mark as answered or vote as helpful, this will help other people to find answers.


    Marco

    • Proposed as answer by technetcolmar Monday, November 3, 2014 6:48 PM
    • Marked as answer by Voyagr12 Wednesday, November 5, 2014 6:43 PM
    Monday, November 3, 2014 7:59 AM