ASP.NET: Using LINQ to Entities to retrieve and update a single value from a single record

Posted on 2011-04-23
Last Modified: 2012-05-11
Hello –

I am building my first web application, using ASP.NET 4.0 with VB. I'm also using Entity Framework 4.0 and LINQ to Entities, both of which are new to me as well.

I have created some pages that use an Entity Data Source to populate a GridView or DetailView , but this question involves a new page that just needs to work with a single value from a single record. Instead of creating an Entity Data Source, I tried different things in the code and eventually found a way to use LINQ 2 EF to both retrieve and update the required value.

Question:  I have a solution that is working, but since I am learning I'm asking for you to review it to see if this is okay or if there's a better way. Better could be in terms of speed/performance once it is deployed, overall efficiency, coding practices, or anything else I haven't thought of.

Here is the code behind for this page (I'll explain the objective below):

Partial Class iFAapp_AS4
    Inherits System.Web.UI.Page
    Dim ctxAcctSettings As New EFmodels.edmAcctSettings

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If IsPostBack = False Then
            Dim sortSet = (From s In ctxAcctSettings.iFAacctPrefs
                          Where s.AcctID = Profile.iFAacctID And s.PrefName = "NameSort"
                          Select s).Single
            Dim theVal As String = sortSet.PrefValue

            If theVal = 1 Then
                Me.rdoSort1.Checked = True
                Me.rdoSort2.Checked = True
            End If
        End If

    End Sub

    Protected Sub rdoSort1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoSort1.CheckedChanged
        If rdoSort1.Checked = True Then
            SetSort(Profile.iFAacctID, 1)
        End If
    End Sub

    Protected Sub rdoSort2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoSort2.CheckedChanged
        If rdoSort2.Checked = True Then
            SetSort(Profile.iFAacctID, 2)
        End If
    End Sub

    Protected Sub SetSort(ByVal intAcctID As Integer, ByVal strVal As String)
        'update the AcctPref record for name sort
        Dim sortSet = (From s In ctxAcctSettings.iFAacctPrefs
                       Where s.AcctID = intAcctID And s.PrefName = "NameSort"
                       Select s).Single
        'set the new value
        sortSet.PrefValue = strVal

        panelMsg.Visible = True
        lblMsg.Text = "Your selection has been saved. {NameSort saved as: " & strVal & "}"
    End Sub

End Class

Open in new window

This web app uses ASP's membership feature, and it will be offered as software-as-a-service, so each user will work with only his data. This page is an "Account Settings" page, where the user can change personalize certain preferences for the application.

The only setting being addressed so far is the name sort preference, where the user can select whether his contact records are displayed as "firstname lastname" or "lastname, firstname".  The page contains a group of two radio buttons to present these two choices.

The preference is stored in a table and can be identified by the user's AcctID combined with the PrefName "NameSort".  The Page_Load event retrieves the current preference and sets the radio buttons accordingly. If the user checks a different radio button, the code calls "SetSort" which saves the change to the database.

As mentioned, the code above is working. But this is a new technique with LINQ for me so I want to have it reviewed by experts! Many of the future pages of this app will need to lookup a single value in the preferences table, and this particular page will have two additional sections to manage two other preferences, so I want to learn if this approach is okay as is or can be enhanced.

Thanks in advance for any input you can provide!
Question by:JMS1965
    LVL 96

    Accepted Solution

    I don't see anything that, on the surface, would need to be changed.

    Here is some food for thought:

    You can use a single Lambda expression, which is composable, for the LINQ query.  I like to use typed arguments for readability, but that is a personal choice.


    Dim sortSet = (From s In ctxAcctSettings.iFAacctPrefs
                              Where s.AcctID = Profile.iFAacctID And s.PrefName = "NameSort"
                              Select s).Single

    Open in new window

    would be something like this:

    Dim sortSet As List(Of iFAacctPref) = 
              .Where(Function(x) s.AcctID = Profile.iFAacctID AndAlso 
                                             s.PrefName = "NameSort")

    Open in new window


    Author Closing Comment

    Thanks for the input ... good to know I'm on the right tract and that this approach is okay moving forward. I'll start to work with the Lambda expressions as well.

    Thanks again!

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now