Link to home
Start Free TrialLog in
Avatar of kwh3856
kwh3856Flag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Vel Eous
Vel Eous

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kwh3856

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kwh3856

ASKER

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.
Avatar of kwh3856

ASKER

Thank you very much.
Not a problem, glad to help.