LINQ to SQL Updating Objects


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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

And if you change

q = mUser


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

Does it work?
badpilotAuthor Commented:
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.

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.

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
badpilotAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.