Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 12th, 2011, 02:14 PM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default LINQ Update Statement

Hi

Does anyone know of any good web-based resources where I can get information on how to construct LINQ Update statements. I've done LINQ Select statements before with Imar's help but now I'm keen to look into LINQ Update statements instead of relying on standard SQL statements.

Thanks

Phil
Reply With Quote
  #2 (permalink)  
Old April 12th, 2011, 02:58 PM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

You may want to look at the 101 LINQ samples: http://msdn.microsoft.com/en-us/vcsharp/aa336746

However, Updates are not really part of LINQ; they are part of technologies that use LINQ such as LINQ to SQL or Entity Framework. Often all you need to do is call SaveChanges or SubmitChanges on a context that contains a modified entity,

What exactly are you trying to update?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #3 (permalink)  
Old April 12th, 2011, 03:10 PM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default LINQ to SQL

Hi Imar

Thanks for the reply. I want to update the status of a record in my SQL database which is by default set to '1' when the user interact which the system and triggers the update, I want to update the corresponding record and change its status to '3', etc.

The numerical status codes do correspond to associated status descriptions within a foreign key relationship between my tables.

I would prefer to use LINQ updates rather than standard SQL as it seems alot 'cleaner' to implement within my build if that make sense?

Thanks

Phil
Reply With Quote
  #4 (permalink)  
Old April 12th, 2011, 03:42 PM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

I guess it all depends on what you're doing. Here's a simple example using LINQ to SQL but the idea is the same for EF (not sure which one you're using):

Code:
 
using (PlanetWroxDataContext context = new PlanetWroxDataContext())
{
  Person myPerson  = context.People.Where(p => p.Id == 123).First();
  myPerson.Whatever = 3;  // Update the property
  context.SubmitChanges();
}
Does this help? If not, you need to be more concrete about what you're doing and how.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!

Last edited by Imar; April 17th, 2011 at 03:08 PM..
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Phil Critchley (April 12th, 2011)
  #5 (permalink)  
Old April 12th, 2011, 03:57 PM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default LINQ to SQL

Hi Imar

Thanks for the posting, it definately helps and explains what I'm after doing.

Thanks for your help.

Phil
Reply With Quote
  #6 (permalink)  
Old April 12th, 2011, 05:45 PM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default LINQ to SQL

Hi Imar

I'm using Entity Framework. I can't get 'SubmitChanges' to implement with the EF and the only method I've found which is remotely the same is 'SaveChanges'. I'm assuming that both methods provide the same functionality and will update my backend SQL database?

Code:
Using myLiteratureCatalogue As New Model.LiteratureCataloguingEntities
            Dim existingStatus = (From publication In myLiteratureCatalogue.Publications Where publication.PublicationID = "" Select publication).First()

            ' Change the status of the publication.
            existingStatus.StatusID = (1)

            myLiteratureCatalogue.SaveChanges()

        End Using
Reply With Quote
  #7 (permalink)  
Old April 13th, 2011, 03:00 AM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Phil,
Quote:
I'm assuming that both methods provide the same functionality and will update my backend SQL database?
Yes, but in different frameworks. SubmitChanges is part of LINQ to SQL, while SaveChanges is part of Entity Framework 4. Both are similar ORM frameworks, where EF is getting more attention that L2S lately. I didn't know which one you were using so I mentioned both in my first post and then used L2S as an example later on.

Just realize that this code performs 2 queries: one to get the object, and another to update it. If you already had a reference to the said publication, you could update that instance instead.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Phil Critchley (April 13th, 2011)
  #8 (permalink)  
Old April 13th, 2011, 05:09 AM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default LINQ to Entities

Hi Imar

Thanks very much for all the information you have provided me with. Its definately very beneficial to a novice like me who is just starting out with building ASP projects and not familiar with LINQ just the more traditional SQL.

Hopefully speak to you again soon

Phil
Reply With Quote
  #9 (permalink)  
Old April 13th, 2011, 11:46 AM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

You're welcome...

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #10 (permalink)  
Old April 16th, 2011, 05:48 PM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default LINQ Update

Hi Imar

Is it possible to update a SQL Server database field with a NULL value using LINQ to Entities?

I'm trying to update a UniqueIdentifier field by getting the update to empty out the GUID. Is this possible?

Phil
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
update multiple columns in an update statement debbiecoates SQL Server 2000 1 August 17th, 2008 04:01 AM
UPDATE statement takwirira ASP.NET 2.0 Basics 7 March 7th, 2007 07:48 PM
insert and/or update statement carswelljr SQL Language 3 September 28th, 2006 10:03 AM
Update Statement some error thas123 ADO.NET 1 January 8th, 2006 10:30 AM



All times are GMT -4. The time now is 10:30 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.