Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2011-04-23
Medium Priority
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

Bob Learned earned 2000 total points
ID: 35456716
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

ID: 35464450
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month20 days, 17 hours left to enroll

810 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