Solved

LINQ statement does not updated record in database

Posted on 2013-05-15
8
510 Views
Last Modified: 2013-05-16
Using LINQ, I have created a query to locate a record in a table.  The record is successfully found.  I have verified that in debug.  I then need to update one field on that record called Accepted.  I need to set this field to "Y".  I perform a submitchanges and it acts like everything is fine with no problems.  I go back to my database and the record is left unchanged.  Can someone steer me in the right direction as to what the problem may be?  Any help is greatly appreciated.

DBDataContext dc = new DBDataContext();

                    Referral myAddNewReferral = new Referral();


                    // code for Accept button
                    string mguid;
                    
                    mguid = oRow.Items[0].Value.ToString();
                    Guid resultguid = new Guid(mguid);


                    var ReferralResult = from ReferralData in dc.Referrals
                                         where ReferralData.REFGUID == resultguid
                                        select new
                                        {
                                            ReferralData.RefReason,
                                            ReferralData.Insurance,
                                            ReferralData.Accepted
                                        };



                    foreach (var ReferralData in ReferralResult)
                        // Update Referral Record
                        
                             myAddNewReferral.Accepted = "Y";

                             UpdateTable(dc, myAddNewReferral);
							 
							 
		private static void UpdateTable(DBDataContext db, Object record)
        {


            // Get the type of the record to insert
            string type = record.GetType().Name;




            if (type == "Referral")
            {


                try
                {
                    
                    db.SubmitChanges();
                    

                }

Open in new window

0
Comment
Question by:kwh3856
8 Comments
 
LVL 14

Accepted Solution

by:
Vel Eous earned 250 total points
ID: 39170659
If only a single record is being returned by your LINQ to SQL statement, then there is no requirement for your foreach loop.  You also need to alter the value of the returned object (ReferralResult), not of the newly created (newAddNewReferral) if you are looking to update rather than insert.

With that in mind:
var ReferralResult = dc.Referrals.Where(r => r.REFGUID == resultguid).Single();
ReferralResult.Accepted = "Y";
dc.SubmitChanges();

Open in new window

0
 
LVL 10

Expert Comment

by:eguilherme
ID: 39171062
Have you stepped into the UpdateTable method? Does the if condition is met?

If so, the problem might be something else, does the Referral table have a primary key setted? Linq2Sql does not update records if it doesnt have a primary key
0
 
LVL 23

Assisted Solution

by:Ioannis Paraskevopoulos
Ioannis Paraskevopoulos earned 125 total points
ID: 39171088
Hi,

I would change:
foreach (var ReferralData in ReferralResult)
                        // Update Referral Record
                        
                             myAddNewReferral.Accepted = "Y";

                             UpdateTable(dc, myAddNewReferral);

Open in new window


To
foreach (var ReferralData in ReferralResult)
                        // Update Referral Record
                        
                             ReferralData .Accepted = "Y";

                             UpdateTable(dc, ReferralData );

Open in new window


Giannis
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:kwh3856
ID: 39171393
Tchuki and Jyparask,
I will give that a try.  I think both of you are saying the same thing and that is most likely the problem.  I will let you know.

Thanks
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 125 total points
ID: 39172680
Hi Kenny;

The record myAddNewReferral is created at the beginning of the code example and at that time is a empty record. You then make a query to the database searching for the record that has the ReferralData.REFGUID == resultguid and when it is found you return three fields of the record and which is an anonymous type. In the foreach loop, which is missing a { so I am not sure that the next three statement are in that loop. You modify the myAddNewReferral.Accepted with "Y". Now myAddNewReferral is the empty record you created at the beginning of the code and now you have initialized the Accepted field with "Y". You then call UpdateTable with the data context and the partially initialized record myAddNewReferral. In that method you test to see if it is the correct record type and then you save the record to the database. Is that what you wanted to do was save a new record with only one field initialized to the database?
0
 

Author Comment

by:kwh3856
ID: 39173721
Fernando,
That is what I needed to understand what I was actually doing.  I now understand that

Referral myAddNewReferral = new Referral();

creates a new record.  I thought I had to do that just so I could get access to reference the fields in the table.

I tried the code from tchuki and it worked perfect.  I tried the code from jyparask and there was a problem with the the two statements below.

ReferralData .Accepted = "Y";

UpdateTable(dc, ReferralData );

From reading each of your thoughts on how to solve this problem, I have learned greatly.  I would like to award each of you points for helping me to get a better understanding of LINQ.
0
 

Author Closing Comment

by:kwh3856
ID: 39173723
Thank you very much.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39173777
Not a problem, glad to help.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question