LINQ to SQL Updating Objects

Posted on 2012-09-14
Last Modified: 2012-09-18

How come in a winforms application I can retrieve an object using LINQ to SQL, replace the object with a version with updated fields, call .SubmitChanges and it works, yet in an site it doesn't apply the changes?

Example below, thanks!

Public Sub updateUser(mUser As User)

     Using db As New myDataContext

                Dim q = (From u In db.Users
                         Where u.UserID = mUser.UserID
                         Select u).FirstOrDefault

                q = mUser

'At this point in a winforms app the database is updated, in an app no changes are
'made. I've stepped through the code and everything looks ok up until SubmitChanges is
'called, drilling down in to that shows that the object passed for update doesn't have the
'changed values in the new object.

            End Using
End Sub

Open in new window

Question by:badpilot
    LVL 83

    Expert Comment

    And if you change

    q = mUser


    q.Name = mUser.Name
    q.Age = mUser.Age

    Does it work?

    Author Comment

    Yup, sorry forgot to mention that - fairly annoying having to manually set every property though.

    Everything else works, I'm not over-riding or using any partial methods, all just as it is set up out of the box.

    I've also checked there isn't anything in my page load that could causing this and completely eliminated it by calling the update from a button on a blank page.

    LVL 83

    Accepted Solution

    Try changing

    Dim q


    Dim q As User

    where User is the name of your class. If that does not work, you may want to add a method to this class that takes an object of same type and copies the property values so you won't be repeating the code.

    Author Comment

    Sorry for the delay in replying. Declaring q as a User object doesn't fix this.

    I just ran a quick test in a console app and it doesn't work there either, I may have just been imagining things when I thought I had updated this way before.

    I'd be interested to see if there are any better ways of doing this though, I'll return here if I ever do find a way.

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    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.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    755 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

    25 Experts available now in Live!

    Get 1:1 Help Now