473,748 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.SubmitChange s();
}
}

public static void UpdateMyclass(M yclass iss)
{
Update<Myclass> (iss, delegate(Myclas s 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.Descript ion = TextBox2.Text;
theObj .ID = TextBox1.Text;

try
{
MyDB.UpdateMycl ass (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 1312
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<MyClassp assing "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
16418
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 write a safe but easy implementation (i.e. no codedom) for an IBindingListView.Filter (by compiling to a Predicate<T>). Anybody know if this is possible at all? Marc
3
2900
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: tlkpColor (PK) tlkpColorID
9
2503
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 about vs2008? is it different name space or framework for linq xml or linq sql? ( 2. do we need to have references to what linq's dlls. or namespaces? system core? 3. what name spaces are needed?
14
3689
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
8995
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9561
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9381
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9332
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.