Link to home
Start Free TrialLog in
Avatar of sana khan
sana khanFlag for India

asked on

multiple update data using put method web api

How to multiple update data using put method web api. I have used single update but i am unable to write code for multiple update.

   public HttpResponseMessage Put(DelegateTable delegatestable)
        {
            try
            {
             
                using (var ctx = new ShowContext())
                {

                    var delegatedata = ctx.delegates.FirstOrDefault(s => s.Barcode__c == delegatestable.Barcode__c);

                    if (delegatedata != null)
                    {
                        if (delegatestable.Salutation__c!=null)
                        {
                            delegatedata.Salutation__c = delegatestable.Salutation__c;
                        }
                        if (delegatestable.First_Name__c != null)
                        {
                            delegatedata.First_Name__c = delegatestable.First_Name__c;
                        }
                        if (delegatestable.Last_Name__c != null)
                        {
                            delegatedata.Last_Name__c = delegatestable.Last_Name__c;
                        }
                        if (delegatestable.Account_Name__c != null)
                        {
                            delegatedata.Account_Name__c = delegatestable.Account_Name__c;
                        }
                        if (delegatestable.Contact_Email__c != null)
                        {
                            delegatedata.Contact_Email__c = delegatestable.Contact_Email__c;
                        }
                        if (delegatestable.Category__c != null)
                        {
                            delegatedata.Category__c = delegatestable.Category__c;
                        }
                        if (delegatestable.Conference_Type__c != null)
                        {
                            delegatedata.Conference_Type__c = delegatestable.Conference_Type__c;
                        }
                        if (delegatestable.Conference_Selection__c != null)
                        {
                            delegatedata.Conference_Selection__c = delegatestable.Conference_Selection__c;
                        }
                        if (delegatestable.Payment_Status_Interface__c != null)
                        {
                            delegatedata.Payment_Status_Interface__c = delegatestable.Payment_Status_Interface__c;
                        }
                        
                        //  delegatedata.Barcode__c = delegatestable.Barcode__c;

                        ctx.SaveChanges();
                        return Request.CreateResponse(HttpStatusCode.OK, "Record updated");
                    }
                    else
                    {
                        return Request.CreateResponse(HttpStatusCode.NoContent, delegatedata);
                    }
                }
            }
            catch (Exception ex)
            {

                return Request.CreateResponse(HttpStatusCode.BadGateway, ex);
            }
        }

Open in new window


using json for updating data through postman application
Avatar of sana khan
sana khan
Flag of India image

ASKER

Tried below code for multiple update but how to write json format for it in postman application to test it. Please help

 public HttpResponseMessage Put(List<DelegateTable> delegatestable)
        {
            try
            {
             
                using (var ctx = new ShowContext())
                {

                    foreach (DelegateTable item in delegatestable)
                    {
                        //DelegateTable _ObjdelegateTable = new DelegateTable();
                        var delegatedata = ctx.delegates.FirstOrDefault(s => s.Barcode__c == item.Barcode__c);

                        if (delegatedata != null)
                        {
                            if (item.Salutation__c != null)
                            {
                                delegatedata.Salutation__c = item.Salutation__c;
                            }
                            if (item.First_Name__c != null)
                            {
                                delegatedata.First_Name__c = item.First_Name__c;
                            }
                            if (item.Last_Name__c != null)
                            {
                                delegatedata.Last_Name__c = item.Last_Name__c;
                            }
                            if (item.Account_Name__c != null)
                            {
                                delegatedata.Account_Name__c = item.Account_Name__c;
                            }
                            if (item.Contact_Email__c != null)
                            {
                                delegatedata.Contact_Email__c = item.Contact_Email__c;
                            }
                            if (item.Category__c != null)
                            {
                                delegatedata.Category__c = item.Category__c;
                            }
                            if (item.Conference_Type__c != null)
                            {
                                delegatedata.Conference_Type__c = item.Conference_Type__c;
                            }
                            if (item.Conference_Selection__c != null)
                            {
                                delegatedata.Conference_Selection__c = item.Conference_Selection__c;
                            }
                            if (item.Payment_Status_Interface__c != null)
                            {
                                delegatedata.Payment_Status_Interface__c = item.Payment_Status_Interface__c;
                            }

                            //  delegatedata.Barcode__c = delegatestable.Barcode__c;

                            ctx.SaveChanges();
                            return Request.CreateResponse(HttpStatusCode.OK, "Record updated");
                        }
                        else
                        {
                            return Request.CreateResponse(HttpStatusCode.NoContent, delegatedata);
                        }
                    }
                    var message = Request.CreateResponse(HttpStatusCode.Created, delegatestable);
                    message.Headers.Location = new Uri(Request.RequestUri.ToString());
                    return message;
                }
            }
            catch (Exception ex)
            {

                return Request.CreateResponse(HttpStatusCode.BadGateway, ex);
            }
        }

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.