Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Linq is not updating my data...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ilyas - 17 Mar 2008 13:29 GMT
Hi all

I have the folowing code:

           NorthwindDataContext nw1 = new NorthwindDataContext();

           Category c1 = new Category();
           c1.CategoryID = 1;
           c1.CategoryName = "New Category2";
           nw1.SubmitChanges();

           //Note that the category name is
           Category c2 = (from c in nw1.Categories where
c.CategoryID==1 select c).Single();
           Assert.AreEqual(c1.CategoryName , c2.CategoryName);

This last line is not true meaning that the value of the Category
hasnt been updated? Why is this? Wha do I need to do to get this to
work as I would expect?

Many thanks
Jon Skeet [C# MVP] - 17 Mar 2008 13:38 GMT
> I have the folowing code:
>
[quoted text clipped - 4 lines]
>             c1.CategoryName = "New Category2";
>             nw1.SubmitChanges();

You haven't attached that to your data context. It's just a completely
free-floating object. If you want to insert a new entry, you need to
call nw1.Categories.InsertOnSubmit(c1)

If you want to update an existing entry, you should probably fetch it
first, then update it, then submit the change.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Ilyas - 17 Mar 2008 14:39 GMT
> > I have the folowing code:
>
[quoted text clipped - 15 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

sorry I missed out a line - what I meant to say was :

            NorthwindDataContext nw1 = new NorthwindDataContext();

            Category c1 = new Category();
            c1.CategoryID = 1;
            c1.CategoryName = "New Category2";
            nw1.SubmitChanges();

           Category c2 = (from c in nw1.Categories where
c.CategoryID==1 select c).Single();
           Assert.AreEqual(c1.CategoryName, c2.CategoryName); //This
line is false

Why is this - surly this should work right?
Ilyas - 17 Mar 2008 14:41 GMT
> > > I have the folowing code:
>
[quoted text clipped - 33 lines]
>
> - Show quoted text -

           Category c1 = new Category();
           c1.CategoryID = 1;
           c1.CategoryName = "New Category2";
           nw1.Categories.InsertOnSubmit(c1);
           nw1.SubmitChanges();

           Category c2 = (from c in nw1.Categories where
c.CategoryID==1 select c).Single();
           Assert.AreEqual(c1.CategoryName, c2.CategoryName);

The categoryName is not updated - why?
Jon Skeet [C# MVP] - 17 Mar 2008 14:48 GMT
<snip>

>             Category c1 = new Category();
>             c1.CategoryID = 1;
[quoted text clipped - 5 lines]
> c.CategoryID==1 select c).Single();
>             Assert.AreEqual(c1.CategoryName, c2.CategoryName);

Right, that's more like it. Is this your *actual* code, or do you have
more code around there?

> The categoryName is not updated - why?

Well, you've told it to insert, not to update. Is there already a
Category with ID 1 before you run this code? Does it run without any
exceptions?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Ilyas - 17 Mar 2008 15:40 GMT
> <snip>
>
[quoted text clipped - 20 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

I do apologise for the confusion.
Im still confused. What im trying to do is to update an object without
getting linq to fetch it first

           //Get the category t update
           Category c1 = new Category();
           c1.CategoryID = 1;

           //Attach the object
           nw1.Categories.Attach(c1);

           //Now update the category
           c1.CategoryName = "New Category2";

           //save changes
           nw1.SubmitChanges();

This gives me the error ChangeConflictException "Row not found or
changed" - my issue is that im not sure why this happens?
Jon Skeet [C# MVP] - 17 Mar 2008 15:46 GMT
> I do apologise for the confusion.
> Im still confused. What im trying to do is to update an object without
> getting linq to fetch it first

Hmm... not sure about that.

>             //Get the category t update
>             Category c1 = new Category();
[quoted text clipped - 11 lines]
> This gives me the error ChangeConflictException "Row not found or
> changed" - my issue is that im not sure why this happens?

I suspect it's due to how you've got change tracking configured. As
ever, set the log to write the SQL out where you can see it - that's
likely to give hints as to what's going on.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Marc Gravell - 17 Mar 2008 16:29 GMT
Without a timestamp column it issues a WHERE matching all the old values -
i.e. (from a simple PUBS example):

UPDATE [dbo].[Employees]
SET [Extension] = @p16
WHERE ([EmployeeID] = @p0) AND ([LastName] = @p1) AND ([FirstName] = @p2)
AND ([
Title] = @p3) AND ([TitleOfCourtesy] = @p4) AND ([BirthDate] = @p5) AND
([HireDa
te] = @p6) AND ([Address] = @p7) AND ([City] = @p8) AND ([Region] = @p9) AND
([P
ostalCode] = @p10) AND ([Country] = @p11) AND ([HomePhone] = @p12) AND
([Extensi
on] = @p13) AND ([ReportsTo] = @p14) AND ([PhotoPath] = @p15)

Without the necessary data, it somehow short-circuits to "0=1". I haven't
checked how it detects this.

I think you will struggle to UPDATE an object that you didn't get via LINQ.
You can probably do it if you try hard enough... but... why? Note that you
can serialize/deserialize to round-trip:

      Employee emp1;
       using (PubsDataContext ctx = new PubsDataContext())
       {
           ctx.Log = Console.Out;
           Employee emp0 = ctx.Employees
               .Where(x => x.EmployeeID == 1).Single();
           // make a seraialized clone
           DataContractSerializer dcs = new
DataContractSerializer(typeof(Employee));
           using(MemoryStream ms = new MemoryStream()) {
               dcs.WriteObject(ms, emp0);
               ms.Position = 0;
               emp1 = (Employee) dcs.ReadObject(ms);
           }
       }
       using (PubsDataContext ctx = new PubsDataContext())
       {
           ctx.Log = Console.Out;
           ctx.Employees.Attach(emp1);
           emp1.Extension = "2314";
           ctx.SubmitChanges();
       }
Ilyas - 17 Mar 2008 18:02 GMT
> Without a timestamp column it issues a WHERE matching all the old values -
> i.e. (from a simple PUBS example):
[quoted text clipped - 40 lines]
>             ctx.SubmitChanges();
>         }

This doesnt make any sense to me. In effect what you are saying is
that I need to fetch the objec from linq first, make the changes and
then update it

My question is why do I need to get it from Linq first?

I've noticed that the update command issued is
exec sp_executesql N'UPDATE [dbo].[Categories]
SET [CategoryName] = @p0
WHERE 0 = 1',N'@p0 nvarchar(13)',@p0=N'New Category2'

..which of course wont actually update any rows. Interesting enough
theres no mention of my CategoryId...
Jon Skeet [C# MVP] - 17 Mar 2008 20:31 GMT
<snip>

> This doesnt make any sense to me. In effect what you are saying is
> that I need to fetch the objec from linq first, make the changes and
> then update it

Or change the change-tracking method your entity uses.

> My question is why do I need to get it from Linq first?

LINQ is trying to make sure you're not overwriting someone else's
changes accidentally.

> I've noticed that the update command issued is
> exec sp_executesql N'UPDATE [dbo].[Categories]
[quoted text clipped - 3 lines]
> ..which of course wont actually update any rows. Interesting enough
> theres no mention of my CategoryId...

That's exactly what Mark said:

<quote>
Without the necessary data, it somehow short-circuits to "0=1". I
haven't checked how it detects this.
</quote>

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Jon Skeet [C# MVP] - 17 Mar 2008 14:47 GMT
> sorry I missed out a line - what I meant to say was :
>
[quoted text clipped - 9 lines]
>             Assert.AreEqual(c1.CategoryName, c2.CategoryName); //This
> line is false

That's exactly what you posted before. Where's the extra line?

> Why is this - surly this should work right?

No - your original SubmitChanges() isn't doing anything, because you're
not adding anything to it.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.