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
todw17todw17 

C# ASP.NET - Getting results into a DataGrid

Has anyone had any luck getting the query result to display in a DataGrid?  In the quickstart files provided, the results are displayed in a simple table.  I was able to get that to work fine, but I would like to have the added functionality of a DataGrid control.

Im new to all of this and am looking for a kick in the right direction.

Thanks in advance,

Todd

 

 

 

 

zakzak
Hi,

Unfortuantly the generated Web Services classes in .NET do not play well with .NET's data binding infrastructure (because the WS side uses public fields, while the databinding side looks for public properties, last i heard this is fixed in Whidbey). You can bind to a datagrid, but you'll need manually setup your columns in the datagrid (see the asp.net quickstart for details on how to do that if you haven't done it before)

Cheers
Simon
GlennWGlennW

Todd;

I find it is best to do the following:

  1. Check the fields that you are getting against the meta data (from describeSobject) and use that data to build an empty dataset.  Use the soaptype to determine the column datatype
  2. Get the sobject() array using your query
  3. Add in a new datarow
  4. Read throught the .Any property of each sobject in the array and if the .LocalName matches one of your column names you can add that field to the newly created datarow in the dataset
  5. Then just bind your datagrid to the newly created dataset.

Cheers;
GlennW

DevAngelDevAngel

Hi todw17,

There is a great sample (toot toot) on sourceforge that shows inplace editing on a datagrid as well as some other binding to datagrids.  It uses the Partner version of the api, but does not mean it can't be adapted to Enterprise (you can use partner or enterprise, just because you may be a customer doesn't exclude you from using partner).

Here is a link to the code.

http://prdownloads.sourceforge.net/sforce/csPatnerSample_with_source.zip?download

Cheers

Nick @ AKCSLNick @ AKCSL

Hi Dave,

this is a really useful link, I'd like to use the some of the libraries in my own development.

I'm trying to use it at the moment to populate a Datatable direct from a Queryresult using the example code:

"public static void LoadDataTable(sforce.QueryResult qr, System.Data.DataTable dt)

{

sforce.sObject[] records = qr.records;

for (int i=0;i

{

System.Data.DataRow dr = dt.NewRow();

if (records[i].Id != null)

dr["Id"] = records[i].Id;

System.Xml.XmlElement[] fields = records[i].Any;

for (int j=0;j

{

if (!fields[j].LocalName.Equals("type"))

{

if (fields[j].InnerText.Length > 0)

dr[fields[j].LocalName] = fields[j].InnerText;

else if (!dr.Table.Columns[fields[j].LocalName].AllowDBNull)

{

if (dr.Table.Columns[fields[j].LocalName].DataType.Equals(typeof(System.DateTime)))

dr[fields[j].LocalName] = System.DateTime.Now;

else if (dr.Table.Columns[fields[j].LocalName].DataType.Equals(typeof(System.Double)))

dr[fields[j].LocalName] = 0.0;

else if (dr.Table.Columns[fields[j].LocalName].DataType.Equals(typeof(System.Int32)))

dr[fields[j].LocalName] = 0;

else

dr[fields[j].LocalName] = "(missing)";

}

}

}

dt.Rows.Add(dr);

}

}"

The problem is that I'm using the Enterprise wsdl (the example uses the Partner wsdl), and I don't have an "Any" property on my sObject class.

Following the downloaded example, I've tried to add it as follows:

"public abstract class sObject {

///

[System.Xml.Serialization.XmlElementAttribute("fieldsToNull", IsNullable=true)]

public string[] fieldsToNull;

///

[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]

public string Id;

///

[System.Xml.Serialization.XmlAnyElementAttribute()]

public System.Xml.XmlElement[] Any;

}"

When I run the example, "fields" (in the highlighted line in the first code section) comes back a causing it to fail from there on in.

I don't think I want to change to the Partner wsdl, as I guess that none of the table layouts are defined, and have to be created from the Describer at runtime (I've defined my Datasets already)

Am I doing anything obviously wrong?

I'm using C#.NET, VS and API 3.0

Cheers

Nick

DevAngelDevAngel

Hi Nick,

To do data binding with the enterprise wsdl, you will need to do a little more wrapping.  What you will need is the fields specified in the select clause of the SOQL statement.  You will use these field values to determine what info from the describeSObject call to use to create your datatable.

I would wrap the whole query call in a functin that returns a datatable object that you can add to a dataset.

public System.Data.DataTable QueryToDataTable(string SOQL) {

}

I have attached a sample that does data binding using a wrapper and some parsing functions.  Replace my web reference file with yours.

Cheers.

 

Nick @ AKCSLNick @ AKCSL

Many thanks Dave,

this is an EXCELLENT example, and I would highly recommended it to anyone using the Enterprise wsdl.

cheers

Nick

 

Altair08Altair08
I'm sorry... I know this is an old thread, but I am unable to locate the attachment you mentioned.

"I have attached a sample that does data binding using a wrapper and some parsing functions.  Replace my web reference file with yours."

Do you still have this?
LuisMXLuisMX

 


DevAngel wrote:

Hi Nick,

To do data binding with the enterprise wsdl, you will need to do a little more wrapping.  What you will need is the fields specified in the select clause of the SOQL statement.  You will use these field values to determine what info from the describeSObject call to use to create your datatable.

I would wrap the whole query call in a functin that returns a datatable object that you can add to a dataset.

public System.Data.DataTable QueryToDataTable(string SOQL) {

}

I have attached a sample that does data binding using a wrapper and some parsing functions.  Replace my web reference file with yours.

Cheers.

 


Cant find attached file :(

 

ChayneChayne

Hi DevAngel,

 

Were can I find the attached example of the wrapper?

 

Cheers