LINQ to SQL Updating Objects

Hello

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 asp.net 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
                db.SubmitChanges()


'At this point in a winforms app the database is updated, in an asp.net 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

badpilotAsked:
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.

 
CodeCruiserCommented:
And if you change

q = mUser

with

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

Does it work?
0
 
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.

Thanks
0
 
CodeCruiserCommented:
Try changing

Dim q

to

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.
0

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.
0
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.