• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 579
  • Last Modified:

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

0
badpilot
Asked:
badpilot
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now