Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Update Account in C#

The following example shows how to get an account and update some elements using the following Customer Management service operations.

This example has been developed and run within the environment described in Getting Started Using C# and Visual Basic with Bing Ads Services.

System_CLiX_note Note

This example uses the UserName and Password elements for authentication. For Managing User Authentication with OAuth, replace the UserName and Password elements with the AuthenticationToken, which is your OAuth access token.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using UpdateAccount.BingAds.CustomerManagement;

namespace UpdateAccount
{
    class Program
    {
        private static CustomerManagementServiceClient service = null;

        // Specify your credentials.

        private static string m_userName = "<UserNameGoesHere>";
        private static string m_password = "<PasswordGoesHere>";
        private static string m_token = "<DeveloperTokenGoesHere>";

        // Specify the identifier of the account to update.

        private static long m_accountId = <AccountIdGoesHere>; 

        static void Main(string[] args)
        {
            try
            {
                service = new CustomerManagementServiceClient("BasicHttpBinding_ICustomerManagementService");

                UpdateAccount(m_accountId);

                service.Close();
            }
            catch (CommunicationException e)
            {
                Console.WriteLine(e.Message);

                if (service != null)
                {
                    service.Abort();
                }
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e.Message);

                if (service != null)
                {
                    service.Abort();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

                if (service != null)
                {
                    service.Abort();
                }
            }
        }

        // Update the accounts's data.
        // The update will fail if another user updated the account
        // data between the time you retrieved the account object
        // and the time you called the UpdateAccount operation.

        static void UpdateAccount(long accountId)
        {
            UpdateAccountRequest request = new UpdateAccountRequest();
            UpdateAccountResponse response = null;
            Account account = null;

            account = GetAccountData(m_accountId);

            if (null != account)
            {
                // You can update all elements that are not marked as
                // read-only on MSDN. Update the fields as appropriate.
                // This example updates the identifier of the payment
                // instrument that is used to settle the account.

                account.PaymentMethodId = <PaymentMethodIdGoesHere>;

                try
                {
                    request.UserName = m_username;
                    request.Password = m_password;
                    request.DeveloperToken = m_token;
                    request.Account = account;

                    response = service.UpdateAccount(request);

                    Console.WriteLine("Account {0} ({1}) was successfully updated.", account.Name, m_accountId);
                }
                catch (FaultException<AdApiFaultDetail> fault)
                {
                    Console.WriteLine("The following errors occurred while updating account {0} ({1}).", account.Name, account.Id);

                    foreach (AdApiError error in fault.Detail.Errors)
                    {
                        Console.WriteLine("Error code: {1}\nMessage: {0}\nDetail: {2}\n", error.Message, error.Code, error.Detail);
                    }
                }
                catch (FaultException<ApiFault> fault)
                {
                    Console.WriteLine("The following errors occurred trying to update account {0}.\n", account.Id);

                    foreach (OperationError error in fault.Detail.OperationErrors)
                    {
                        if (1001 == error.Code)
                        {
                            // You cannot update the read-only fields,
                            // such as the status fields.

                            Console.WriteLine("You do not have permissions to update one or more of the fields.\n");
                        }
                        else if (510 == error.Code)
                        {
                            // You should consider retrieving the
                            // account object again to get the new
                            // time stamp and data. You can then show
                            // the latest updates to the user to
                            // confirm that they want to continue with
                            // their update.
                            // If so, update the elements as appropriate
                            // and call the UpdateAccount operation.

                            Console.WriteLine("Another user updated the account.\n");
                        }
                        else
                        {
                            Console.WriteLine("Error code: {1}\nMessage: {0}\nDetails: {2}\n", error.Message, error.Code, error.Details);
                        }
                    }
                }
            }
        }

        // Because the update operation is a complete overwrite,
        // you must first get the account's current data, which
        // includes the time stamp of the previous write operation.

        static Account GetAccountData(long accountId)
        {
            GetAccountRequest request = new GetAccountRequest();
            GetAccountResponse response = null;

            try
            {
                request.UserName = m_username;
                request.Password = m_password;
                request.DeveloperToken = m_token;
                request.AccountId = accountId;

                response = service.GetAccount(request);
            }
            catch (FaultException<AdApiFaultDetail> fault)
            {
                Console.WriteLine("The following error occurred trying to access account {0}.\n", accountId);

                foreach (AdApiError error in fault.Detail.Errors)
                {
                    Console.WriteLine("Error code: {1}\nMessage: {0}\nDetail: {2}\n", error.Message, error.Code, error.Detail);
                }
            }
            catch (FaultException<ApiFault> fault)
            {
                foreach (OperationError error in fault.Detail.OperationErrors)
                {
                    if (1001 == error.Code)
                    {
                        Console.WriteLine("You do not have permissions to access account {0}.", accountId);
                    }
                    else
                    {
                        Console.WriteLine("The following error occurred trying to access account {0}.\n", accountId);
                        Console.WriteLine("Error code: {1}\nMessage: {0}\nDetails: {2}\n", error.Message, error.Code, error.Details);
                    }
                }
            }

            return (null == response) ? null : response.Account;
        }
    }
}

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.