Solved

LINQ statement does not updated record in database

Posted on 2013-05-15
8
507 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:
Tchuki earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:kwh3856
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you very much.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem, glad to help.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now