472,993 Members | 2,242 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 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())
{
db.GetTable<T>().Attach(obj);
update(obj);
db.SubmitChanges();
}
}

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;

try
{
MyDB.UpdateMyclass (theObj);
............etc.

What is missing in this code? Is there another easy (working..) way
to save this object in the DB using Linq?
THANKS!
Jun 27 '08 #1
5 1289
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
delegate
* 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?

Marc
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).

Marc
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

Marc
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
3
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table:...
9
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how...
14
by: thj | last post by:
Hi, I was wondering what you guys are using and why? LINQ to SQL or NHibernate? Thanks in advance, Tommy
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.