471,330 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Update with LINQ help needed

hi, I'm trying to write the simplest db application using C#, ASP and
LINQtosQL but cannot get the Update function to work. I have used the
codemodel from a sample called IntroToLinq.

My Insert and Get functions work, but the Update does nothing. No
errors are thrown and no changes are made.
Here's my code
In the DBHelper class:
public static void Update<T>(T obj, Action<Tupdate) where T : class
using (var db = GetDatabaseData())

public static void UpdateMyclass(Myclass iss)
Update<Myclass>(iss, delegate(Myclass i)
i.ID = iss.ID;
i.Description = iss.Description;
i.Status = iss.Status;
In my codebehind for the page where I want to save:

Myclass theObj = MyDB.GetObjById(TextBox1.Text);
theObj.Description = TextBox2.Text;
theObj .ID = TextBox1.Text;

MyDB.UpdateMyclass (theObj);

What is missing in this code? Is there another easy (working..) way
to save this object in the DB using Linq?
Jun 27 '08 #1
5 1236
It looks like you are updating object from itself? So there won't be any
changes detected...

* You get/create "theObj" with some values
* You pass "theObj" to UpdateMyclass as "iss"
* UpdateMyclass creates a delegate, capturing "iss", that when given an
instance "i", updates "i" from "iss"
* UpdateMyclass calls Update<MyClasspassing "iss" as "obj", and the
* Update<Tgets a context, and attaches "obj"; it then invokes the
delegate and commits

Unwrap that, and the values got copied from the orignal instance to the
same instance...

What did you want to do?

Jun 27 '08 #2
By the way, you can "Attach(obj, true)", which will mark it as dirty. It
won't know *what* is dirty, though - so all values will get updated
(normally only changed values are UPDATEd).

Jun 27 '08 #3
Thank you Marc!

The textbox TextBox2.Text contains a new description for the object,
thats where the difference from original object is.
I tried to use Attach(obj,true) but I got some errors then. (I will
try to recreated it to see what the problem was.)
Jun 27 '08 #4

Ok, I got you now. Thanks for the help, it works fine.
Jun 27 '08 #5
I'm glad it is sorted ;-p

Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.