Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 521
  • Last Modified:

LINQ statement does not updated record in database

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
kwh3856
Asked:
kwh3856
3 Solutions
 
Vel EousResearch & Development ManagerCommented:
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
 
eguilhermeCommented:
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
 
Ioannis ParaskevopoulosCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
kwh3856Author Commented:
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
 
Fernando SotoCommented:
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
 
kwh3856Author Commented:
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
 
kwh3856Author Commented:
Thank you very much.
0
 
Fernando SotoCommented:
Not a problem, glad to help.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now