title: C# Help to validate table data before updating
body: <p>I am working on a C# method that receives a json object containing the following values.</p>
  <pre><code>meetingid
  
  o_agendaitem
  o_legistarid
  o_title
  
  n_agendaitem
  n_legistarid  
  n_title
  </code></pre>
  <p>The values with an &quot;o&quot; represent the record that comes from the database.
  The values with an &quot;n&quot; represent the edited values from a web form.</p>
  <p>I put all together to pass it to the C# method:</p>
  <pre><code> var data = JSON.stringify({
              'meetingid': $scope.meetingId,
              'o_agendaitem': o_xref.AgendaItem,
              'o_legistarid': o_xref.LegistarID,
              'o_title': o_xref.Title,
              'n_agendaitem': n_xref.AgendaItem,
              'n_legistarid': n_xref.LegistarID,
              'n_title': n_xref.Title,
  </code></pre>
  <p>I am having trouble writing the logic in a clear and simple way.</p>
  <p>For example, lets say I have the data below:</p>
  <pre><code>44841   1   62704   Title#1
  44841   2   62218   Title#2
  44841   3   62663   Title#3
  44841   4   62679   Title#4
  44841   5   62709   Title#5
  </code></pre>
  <p>The user edits the values, and changes the 1 for a 6, then the data would change to:</p>
  <pre><code>44841   2   62218   Title#2
  44841   3   62663   Title#3
  44841   4   62679   Title#4
  44841   5   62709   Title#5
  44841   6   62704   Title#6
  </code></pre>
  <p>Meeting Id and Agenda Item are a Composite Key.</p>
  <p>I use the &quot;o&quot; agenda item value and meeting id, to find that record, and if exists, delete it, and then create a new entry with the &quot;n&quot; values.</p>
  <p>But I need to check that the meeting id + the &quot;n&quot; agenda item don't exist already too.</p>
  <p>Example:</p>
  <pre><code>44841   1   62704   Title#1
  44841   2   62218   Title#2 &lt; I edit this row, I want to change the 2 for a 6, but I made a mistake and instead entered a 5, then based on my current logic, I would be deleting a record I did not mean to delete. How can I add a new check to make sure that the user is made aware of this error?
  44841   3   62663   Title#3
  44841   4   62679   Title#4
  44841   5   62709   Title#5
  </code></pre>
  <p>This is my attempt to accomplish so far (I added notes in the parts I am not sure if I am doing it right or not):</p>
  <pre><code>   public ActionResult UpdateXRefItem(Parameters data)
      {
          bool status = false;
          string message = &quot;&quot;;
  
          using (Entities entities = new Entities())
          {
              //use the &quot;o&quot; value to find the record
              var oldRec = entities.XRef_WIP.Find(data.meetingId, data.o_agendaItem);
  
              if (oldRec != null)                {
                  
                  //I am not sure if I should remove the record here, or better do a few more checks. 
                  //entities.XRef_WIP.Remove(oldRec);
                 
                  //use the &quot;n&quot; value combined with the meeting id to see if already exists
                  var newVal = entities.XRef_WIP.Find(data.meetingId, data.n_agendaItem);
                  
                  //if the value from the form already exists, return a message to the user
                  if (newVal != null)
                  {
                      status = false;
                      message = &quot;This Agenda Item already exists for this Cross-Reference List. Please verfy your data and try again.&quot;;
                  }
                  else{
                      
                  //after removing the &quot;old&quot; record, and check that the combination of meeting id + agenda item
                  //do not exist alreay, then create a new table entry. 
                  //I cannot update agenda item because it is part of the composite key.
                  //entities.XRef_WIP.Add(new XRefWIP
                  //{
                  //    MeetingID = data.meetingId,
                  //    AgendaItem = data.n_agendaItem,
                  //    LegistarID = data.n_legistarId,
                  //    Title = data.n_title
                  //});
                  //status = true;
                  //message = &quot;Cross-Reference record has been succssfully updated.&quot;;
                  //entities.SaveChanges();                       
                  }
              }
          }
          return new JsonResult { Data = new { status = status, message = message } };
      }
  </code></pre>
  <p>I hope this makes sens and someone is willing to offer a hand to complete the logic. I think I am close, but I am having trouble putting my ideas together.</p>
  <p>Thank you,
  Erasmo</p>