1

Please can somebody help me on the above question? I am using SimpleData in my project and trying to update records as follows but get the error mentioned above.

var releasedHosts = show_crm.tblName.Update(ID: Id, ID: newId);

Error Message:

Named argument 'ID' cannot be specified multiple times

Thank you.

Code Snippet

string siteName = ddlDeleteSite.SelectedItem.Text;
if (!siteName.Equals("Unknown-Site") && siteName != null)
        {
            int siteId = Convert.ToInt32(ddlDeleteSite.SelectedValue);
            //Update record to Attach Hostname to selected site by updating it's SiteID and remove it from 'Unknown-Site'
            var updatedSite = show_crm.tblSites.UpdateBySiteID(SiteID: siteId, Deleted: true);
            if (updatedSite != null)
            {
                var unknownSite = show_crm.tblSites.FindBySiteName("Unknown-Site");
                int unknownSiteId = unknownSite.SiteID;
                var releasedHosts = show_crm.tblHostNames.Update(new { SiteID = unknownSiteId }, new { SiteID = siteId });//(show_crm.tblHostNames.SiteID == siteId, SiteID: unknownSiteId);
            }
            lblDeleteSiteStatus.Text = ddlDeleteSite.SelectedItem.Text + " deleted successfully.";
            lblDeleteSiteStatus.Visible = true;
            RetrieveSites(ddlDeleteSite);

        }
LiaqatG
  • 367
  • 1
  • 3
  • 17
  • You're going to need to explain a bit more than that. Why are you trying to update two fields called ID? Are they related in tables some how? If so, how? – Dan Maharry Jan 21 '13 at 14:20
  • I'm not updating two fields called ID, rather it is the same field. In the 'Update' method, the first expression forms the 'WHERE' clause for the corresponding SQL UPDATE statment, while the second expression corresponds to the SET clause of SQL UPDATE statement. Hope this clarifies the questions. – LiaqatG Jan 22 '13 at 12:43

1 Answers1

1

When you want to use a column as criteria and update it, you have to use either the criteria overload of update:

show_crm.tblName.UpdateAll(show_crm.tblName.ID == Id, ID: newId);

or you can specify two objects as the criteria to do an optimistic-concurrency type of this:

show_crm.tblName.Update(new {ID = newId}, new {ID = Id});

In the latter form, the first object contains the new values and the second one the "original" values.

Mark Rendle
  • 9,274
  • 1
  • 32
  • 58
  • Thanks Mark! "show_crm.tblName.Update(show_crm.tblName.ID == Id, ID: newId);" The first one above did remove the compilation error but throws an 'Argument Exception' with the message "List initializers must contain at least one initializer". The second statement "show_crm.tblName.Update(new {ID = newId}, new {ID = Id});" doesn't even throw any exceptions but is not updating the table. For you to get what I am trying to do in the context of my project, please see above in the updated question above the entire method code snippet. – LiaqatG Jan 22 '13 at 12:39
  • Sorry, Liaqat, my mistake; see first version in edited answer. – Mark Rendle Jan 25 '13 at 13:43
  • Thank you very much Mark. You're a STAR! Why didn't I try the 'DeleteAll' method? Anyway, that did resolve the issue. – LiaqatG Jan 27 '13 at 13:34
  • Well, let's be fair, the dynamic nature means it's not the most discoverable API. Although that may change soon... :) – Mark Rendle Jan 27 '13 at 14:16
  • Your efforts are really valuable and appreciated Mark. Thanks again. – LiaqatG Jan 29 '13 at 15:21