function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rich.ax131Rich.ax131 

Error referencing custom object field name

I'm just now digging into the 4.0 API samples (nice job, by the way).  I'm trying to query a custom table using the following code:

qr =binding.query("select City__c from Masters_Of_Iron__c");

I'm getting a "Specified cast is not valid" error message.  The actual field name according to SalesForce is "City" but when I do a describe on the Masters_Of_Iron table I see that the name property is "City__c" so that's how I assume I am to reference it. 

I see some similar posts on this board but nothing has resolve my error.  I've tried various combinations of case on the field reference.

Any suggestions?  Thanks in advance.

 

SuperfellSuperfell
Are you getting a cast error from the client code, or a cast error returned in a SOAPFault? which line gives the cast error ? (can you show the rest of your code, there's not a lot to go on)
Rich.ax131Rich.ax131

Sorry.  I'm using the "querySample" function from the "basicSample_cs" sample application downloaded from this site.  The only change made was the SQL call.

private void querySample()

{

//Verify that we are already authenticated, if not

//call the login function to do so

if (!loggedIn)

{

if (!login())

return;

}

QueryResult qr = null;

binding.QueryOptionsValue = new sforce.QueryOptions();

binding.QueryOptionsValue.batchSize = 3;

binding.QueryOptionsValue.batchSizeSpecified = true;

try

{

qr =binding.query(

//"select id, Website, Name from Account where Name = 'Golden Straw'");

"select City__c from Masters_Of_Iron__c");

if (qr.size > 0)

{

Account account = ((Account) qr.records[0]);

Console.WriteLine("Found " + qr.size.ToString() + " accounts using Name = 'Golden Straw', ID = " + account.Id + ", website = " + account.Website);

}

else

{

Console.WriteLine("No records were found. Try running the create account sample.");

Console.ReadLine();

}

qr = binding.query("select FirstName, LastName from Contact");

bool bContinue = false;

int loopCounter = 0;

while (bContinue)

{

Console.WriteLine("\nResults Set " + Convert.ToString(loopCounter++) + " - ");

//process the query results

for (int i=0;i<qr.records.Length;i++)

{

Contact con = (Contact) qr.records[i];

string fName = con.FirstName;

string lName = con.LastName;

if (fName == null)

Console.WriteLine("Contact " + (i + 1) + ": " + lName);

else

Console.WriteLine("Contact " + (i + 1) + ": " + fName + " " + lName);

}

//handle the loop + 1 problem by checking to see if the most recent queryResult

if (qr.done)

bContinue = false;

else

qr = binding.queryMore(qr.queryLocator);

}

Console.WriteLine("\nQuery succesfully executed.");

Console.Write("\nHit return to continue...");

Console.ReadLine();

}

catch (Exception ex)

{

Console.WriteLine("\nFailed to execute query succesfully, error message was: \n"

+ ex.Message);

Console.Write("\nHit return to continue...");

Console.ReadLine();

}

}

SuperfellSuperfell
No Doubt the exception comes from this line

Account account = ((Account)qr.records[0]);

because now the query isn't returning Account's it's returning Masters_Of_Iron__c's
Rich.ax131Rich.ax131
Duh.  I'm an idiot.  Thank you for pointing out the obvious.