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
Chris987654321Chris987654321 

Trying to get .NET Sample working

In the querySample() code, how does VS know what the Contact data type is? I get this error message:

 

Error    1    The type or namespace name 'Contact' could not be found (are you missing a using directive or an assembly reference?)   

 

I added WSDL to the web references and named it sForce.And I am using the following libraries.

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services.Protocols;
using Program.sforce;

 

 

 



namespace Program
{
class Program
{
static public SforceService binding;

static void Main(string[] args)
{


Console.Write("Enter username: ");
string username = Console.ReadLine();
Console.Write("Enter password: ");
string password = Console.ReadLine();

// Create a service object
binding = new SforceService();

// Timeout after a minute
binding.Timeout = 60000;

// Try logging in
LoginResult lr;
try
{
Console.WriteLine("LOGGING IN NOW...");
lr = binding.login(username, password);
}
// ApiFault is a proxy stub generated from the WSDL contract when
// the web service was imported
catch (SoapException e)
{
// Write the fault code to the console
Console.WriteLine(e.Code);

// Write the fault message to the console
Console.WriteLine("An unexpected error has occurred: " + e.Message);

// Write the stack trace to the console
Console.WriteLine(e.StackTrace);
}

Console.ReadLine();
}

private void querySample()
{
//The results will be placed in qr
QueryResult qr = null;

//We are going to increase our return batch size to 250 items
//Setting is a recommendation only, different batch sizes may
//be returned depending on data, to keep performance optimized.
binding.QueryOptionsValue = new QueryOptions();
binding.QueryOptionsValue.batchSize = 250;
binding.QueryOptionsValue.batchSizeSpecified = true;

try
{
qr = binding.query("select FirstName, LastName from Contact");
bool done = false;
if (qr.size > 0)
{
Console.WriteLine("Logged-in user can see "
+ qr.records.Length + " contact records.");
while (!done)
{
Console.WriteLine("");
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);
}
if (qr.done)
{
done = true;
}
else
{
qr = binding.queryMore(qr.queryLocator);
}
}
}
else
{
Console.WriteLine("No records found.");
}
}
catch (Exception ex)
{
Console.WriteLine("\nFailed to execute query succesfully," +
"error message was: \n{0}", ex.Message);
}
Console.WriteLine("\n\nHit enter to exit...");
Console.ReadLine();
}
}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell
you need to add a using for the sforce namespace

All Answers

Chris987654321Chris987654321

Here is a screenshot that may help.

VS Configuration

 

SuperfellSuperfell
you need to add a using for the sforce namespace
This was selected as the best answer
SuperfellSuperfell
You can also download all the sample code that's already ready to go.
Chris987654321Chris987654321
Should the WSDL I get from Setup->Develop->API have information about SObjects? It seems like it only has Methods. I was able to access the login() method from the sForce namespace but not the SObjects. I don't see any mention to any SObjects in the WSDL.
Chris987654321Chris987654321
Okay my mistake. I was using some old WSDL I think. It seems to be working now.
SuperfellSuperfell
Make sure to use the WSDL that matches your sample code (i.e. you'll need the enterprise WSDL for sample code that's use the enterprise API, or you'll need the partner WSDL for sample code using the partner API).