Link to home
Start Free TrialLog in
Avatar of JMS1965
JMS1965Flag for United States of America

asked on

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

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
            Else
                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
        ctxAcctSettings.SaveChanges()

        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!
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JMS1965

ASKER

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!