?
Solved

How do you update a field in a record using LINQ

Posted on 2009-04-19
7
Medium Priority
?
1,265 Views
Last Modified: 2013-12-17
I have a record in a table.  I want to update the date field in the table when the user presses the submit button.  I know I am missing something so easy, can someone point me in the right direction.

Here is the code I have tried.
RadRelayDataClassDataContext dcRadRelay = new RadRelayDataClassDataContext();
 
 
            PROVIDER_REFERRAL_QUEUE myProviderReferralQueue = new PROVIDER_REFERRAL_QUEUE();
 
            myProviderReferralQueue.DateFirstTryForAppt = DateTime.Today;
            lblFirstAttemptDate.Text = Convert.ToString(DateTime.Today);
 
            dcRadRelay.PROVIDER_REFERRAL_QUEUEs.UpdateonSubmit;---<--Just A Shot:)
            dcRadRelay.SubmitChanges();

Open in new window

0
Comment
Question by:kwh3856
  • 5
  • 2
7 Comments
 

Author Comment

by:kwh3856
ID: 24181817
Problem solved needed to lookup record first and then submit the changes.
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 24181875
Hi Kenny;

With this line of code you create an record of type PROVIDER_REFERRAL_QUEUE:
PROVIDER_REFERRAL_QUEUE myProviderReferralQueue = new PROVIDER_REFERRAL_QUEUE();

Then you assigned a value to that new record with this line of code.
myProviderReferralQueue.DateFirstTryForAppt = DateTime.Today;  

Then you mark it for Update with this line of code:
dcRadRelay.PROVIDER_REFERRAL_QUEUEs.UpdateonSubmit;

You are not updating a record but creating one.

If you want to update a record you need to get a reference to the record to be updated, using a Linq query possibly, and then modify the field and then UpdateOnSubmit.

Hope that helps.

Fernando
0
 

Author Comment

by:kwh3856
ID: 24181897
Fernando,
I thought I had it but the field never gets updated now.  I have no errors that pop up....just no data:)
Can you show me where I made my mistake?
Thanks
Kenny
 

protected void btnAttempt1_Click(object sender, Infragistics.WebUI.WebDataInput.ButtonEventArgs e)
        {
            RadRelayDataClassDataContext dcRadRelay = new RadRelayDataClassDataContext();
 
 
            PROVIDER_REFERRAL_QUEUE myProviderReferralQueue = new PROVIDER_REFERRAL_QUEUE();
 
            string MPI;
            MPI = Session.Contents["MPI"].ToString();
            Guid ConvertedMPI = new Guid(MPI);
            
            var ReferralsResult = from referralsdata in dcRadRelay.PROVIDER_REFERRAL_QUEUEs
                                  where referralsdata.MPI == ConvertedMPI
                               select new { referralsdata };
 
            int ReferralRecCount = Convert.ToInt32(ReferralsResult.Count());    // <===== Added this line to get total count here
 
            if (ReferralRecCount == 0)                    // <===== Changed this line so that the query does not get executed again
            {
                // No Referral record found
            }
            else
            {
                foreach (var ReferralsResultRecord in ReferralsResult)
                {
                    myProviderReferralQueue.DateFirstTryForAppt = DateTime.Today;
                    lblFirstAttemptDate.Text = Convert.ToString(DateTime.Today);
                    
                    dcRadRelay.SubmitChanges();
                }
            }
 
        }

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:kwh3856
ID: 24181900
Ooohhh....I think I understand your comment now.  I will try a few things and let you know.
0
 

Author Comment

by:kwh3856
ID: 24181916
That was exactly it.  Here is my correct code:
 
 

protected void btnAttempt1_Click(object sender, Infragistics.WebUI.WebDataInput.ButtonEventArgs e)
        {
            RadRelayDataClassDataContext dcRadRelay = new RadRelayDataClassDataContext();
 
 
            string MPI;
            MPI = Session.Contents["MPI"].ToString();
            Guid ConvertedMPI = new Guid(MPI);
            
            var ReferralsResult = from referralsdata in dcRadRelay.PROVIDER_REFERRAL_QUEUEs
                                  where referralsdata.MPI == ConvertedMPI
                               select new { referralsdata };
 
            int ReferralRecCount = Convert.ToInt32(ReferralsResult.Count());    // <===== Added this line to get total count here
 
            if (ReferralRecCount == 0)                    // <===== Changed this line so that the query does not get executed again
            {
                // No Referral record found
            }
            else
            {
                foreach (var ReferralsResultRecord in ReferralsResult)
                {
                    
                    ReferralsResultRecord.referralsdata.DateFirstTryForAppt = DateTime.Today;
                    lblFirstAttemptDate.Text = Convert.ToString(DateTime.Today);
                    
                    dcRadRelay.SubmitChanges();
                }
            }
 
        }

Open in new window

0
 

Author Closing Comment

by:kwh3856
ID: 31572094
AWESOME!!!!
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 24184536
Hi Kenny;

Your last post looks correct to me.

Fernando
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month16 days, 8 hours left to enroll

862 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