I'm working on a SOAP integration (.NET C#) to Salesforce and I am facing a problem when I try to get results from Salesforce.

To do this I'm using the next example code of the Salesforce walkthrough:

private void querySample()
     String soqlQuery = "SELECT FirstName FROM Account";         
     QueryResult qr = binding.query(soqlQuery);

     if (qr.size > 0)
         while (!done)
             sObject[] records = qr.records;
             for (int i = 0; i < records.Length; i++)
                 Account con = (Account)records[i];                     

             if (qr.done)
                 done = true;
                 qr = binding.queryMore(qr.queryLocator);
           Console.WriteLine("No records found.");
     catch (Exception ex)
        Console.WriteLine("\nFailed to execute query succesfully," +
            "error message was: \n{0}", ex.Message);

The problem is in

Account con = (Account)records[i];

When it tries to cast the sObject to the Account I get a runtime error:

System.InvalidCastException: 'An object of type' SalesForceIn.SForce.sObject 'cannot be converted to type' SalesForceIn.SForce.Account '.'

Why I am getting this error? Do I need to create my own converter? Maybe be it's related to wsdl problem?

Thanks in advance. Regards.

Edit: @sfdcfox I'm using Enterprise WSDL because when I export the Partner I don't have the entities like Account in the Wsdl definition. Thank you.

@Keith C Yes I have the correct using in both. Thank you.

  • 2
    Are you using the Enterprise WSDL or Partner WSDL? There's different ways to access fields in each.
    – sfdcfox
    Jan 15, 2020 at 16:10
  • Do you have the right using setup i.e. the equivalent of import com.sforce.soap.enterprise.sobject.Account?
    – Keith C
    Jan 15, 2020 at 18:15
  • 1
    @CrSalesforce looks like you tried to make an edit using your secondary account. See this help doc on merging your accounts. Its painless & will let you comment under this post, make edits without being approved, and accept an answer once you get one. Jan 15, 2020 at 20:36
  • @sfdcfox I'm using Enterprise WSDL because when I export the Partner I don't have the entities like Account in the Wsdl definition. Thank you. Jan 16, 2020 at 15:42
  • @KeithC yes I'm have the correct using in both. Thank you. Jan 16, 2020 at 15:44

1 Answer 1


I tried running the SOAP based Account query against my DEV org.

To do this I imported the Enterprise WSDL as a Web Reference, rather than the newer default of a Service Reference.

After importing the Enterprise WSDL I needed to update the generated Reference.cs proxy classes for public ListViewRecordColumn[][] records. See Importing the Salesforce Winter 15 Partner API to .NET.

Something that became apparent from trying your SOQL query.

SELECT FirstName FROM Account

It appears you are using Person Accounts as a standard org doesn't have Account.FirstName as a field.

When I changed the query to SELECT Name FROM Account it worked fine against my org.

Are you running the SOQL query against the org that generated the Enterprise WSDL? If not, it will likely fail. That is the thing about the Enterprise WSDL - it will only work against the org it was generated from (or one with an identical schema).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.