locked
Webservice help - bind gridview to returned objects RRS feed

  • Question

  • User-1431497110 posted

    Hi

    I've run into an issue when trying to bind a gridview to object date returned from a webservice. First time I've delved into webservices so may be a basic question:

    I'm connecting to a webservice provided by a 3rd party. In my code (vb.net) I have

    Dim webSvc As New Webservice.GetWebService

    ......aother stuff...

    Dim ObjResults As GetObjectListResult = webSvc.GetObjectList()

    How do I bind a gridview to ObjResults as the error I get is "An invalid data source is being used for DataList1. A valid data source must implement either IListSource or IEnumerable. ". Do I need to convert this to a dataset / datatable? Any coding examples? When I browse the definiation of I can see the property as Object() as Webservice.ObjectData()

    I know data is being returned as ObjResults .Object.Length returns a value of 72 which I know is 72 records. Any help is much appreciated.

    Thanks

    Chooch

     

     

     

    Thursday, June 16, 2011 6:12 PM

Answers

  • User3866881 posted

    Hello again:)

    Try this——

     DataTable dt = new DataTable();
                dt.Columns.Add("Id", typeof(string));
                dt.Columns.Add("Title", typeof(string));
                dt.Columns.Add("Description", typeof(string));
                dt.Columns.Add("LastChange",typeof(string));

                var result = from e in XDocument.Load("XMLFile2.xml").Descendants("Object")
                             select new
                             {
                                 Id = e.Attribute("id").Value,
                                 Title = e.Elements("StringVal").First().Value,
                                 Description = e.Elements("StringVal").Last().Value,
                                 LastChange = e.Element("DateTimeVal").Value
                             };

                foreach (var item in result)
                {
                    dt.Rows.Add(item.Id, item.Title, item.Description, item.LastChange);
                }

    return dt;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 18, 2011 9:32 PM

All replies

  • User3866881 posted

    Hello:)

    The exception has told you so clearly that your returned type should be implementing IListSource or IEnumerable……. You should return type of that——

    Either choose a DataTable, DataSet or a List<T>.

    Thx

    Friday, June 17, 2011 9:55 PM
  • User-1431497110 posted

    Hi

    Thanks for that. Being new to webervice, how can I return a dataset / datatable or convert the objects to a dataset / datatable. (The code I'm using was provided by the webservice provider as an example uptil that point but after that says I'm on my own). I had a  seacrh for anwers and believs I need to serialixe this (?)

    If it helps the response should look like

     

    <?xml version="1.0"
    encoding="utf-8"?>

    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-envelope">

    <soap12:Body>

    <GetObjectListResponse xmlns="http://www.somesite.com/WebSvc/v1">

    <GetObjectListResult success="true"
    errorMsg="">

    <Object id="44018">

    <StringVal name="Title">Test1</StringVal>

    <StringVal name="Description">Testing.</StringVal>

    <DateTimeVal name="LastChange">2007-05-09T10:03:29</DateTimeVal>

    </Object>

    <Object id="44052">

    <StringVal name="Title">Test2</StringVal>

    <StringVal name="Description">Testing 2 again</StringVal>

    <DateTimeVal name="LastChange">2007-01-11T17:24:55</DateTimeVal>

    </Object>...

    </GetObjectListResult>

    </GetObjectListResponse>

    </soap:Body>

    </soap:Envelope>

    Thanks

    Chooch

    Saturday, June 18, 2011 11:31 AM
  • User3866881 posted

    Hello again:)

    Try this——

     DataTable dt = new DataTable();
                dt.Columns.Add("Id", typeof(string));
                dt.Columns.Add("Title", typeof(string));
                dt.Columns.Add("Description", typeof(string));
                dt.Columns.Add("LastChange",typeof(string));

                var result = from e in XDocument.Load("XMLFile2.xml").Descendants("Object")
                             select new
                             {
                                 Id = e.Attribute("id").Value,
                                 Title = e.Elements("StringVal").First().Value,
                                 Description = e.Elements("StringVal").Last().Value,
                                 LastChange = e.Element("DateTimeVal").Value
                             };

                foreach (var item in result)
                {
                    dt.Rows.Add(item.Id, item.Title, item.Description, item.LastChange);
                }

    return dt;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 18, 2011 9:32 PM
  • User-1431497110 posted

    Hi

    I marked your code as answered. For any one else out there I ended up with the following (sure there's probably better / succint way but it solved my issue:

          

    Dim dt As New DataTable()

            dt.Columns.Add("Title", GetType(String))

            dt.Columns.Add("Description", GetType(String))

    Dim xmlDoc As New XmlDocument

    xmlDoc.Load("d:\myFileName.xml")

    For Each node As XmlNode In xmlDoc.DocumentElement.ChildNodes

     If node.Name = "Object" Then

    Dim row As DataRow = dt.NewRow()

                    dt.Rows.Add(row)               

    For Each subNode As XmlNode In node.ChildNodes

         Dim xe As XmlElement = DirectCast(subNode, XmlElement)

                      If xe.GetAttribute("name").ToString = "Title" Then

                           row("Title") = xe.InnerXml.ToString

                      End If
                     

                     If xe.GetAttribute("name").ToString = "Description" Then

                            row("Description") = xe.InnerXml.ToString

                     End If

    Next         

    End If       

    Next

            GridView1.DataSource = dt

            GridView1.DataBind()

     

    Thanks

    Chooch

    Wednesday, June 22, 2011 5:38 PM