locked
Bind Webservice response into GridView RRS feed

  • Question

  • User1601010121 posted

    My web-service returns XML string Response . I have this data to Show it on Grid-view. How to do so ?
    request And Response code of my web service

    string requestXml2 = requestXml.ToString();
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
    byte[] bytes;
    bytes = System.Text.Encoding.ASCII.GetBytes(requestXml2);
    request.ContentType = "text/xml; encoding='utf-8'";
    request.ContentLength = bytes.Length;
    request.Method = "POST";
    Stream requestStream = request.GetRequestStream();
    requestStream.Write(bytes, 0, bytes.Length);
    requestStream.Close();
    HttpWebResponse response;
    response = (HttpWebResponse)request.GetResponse();
    if (response.StatusCode == HttpStatusCode.OK)
    {
    Stream responseStream = response.GetResponseStream();
    string responseStr = new StreamReader(responseStream).ReadToEnd();
    return responseStr ;
    }
    <?xml version="1.0" encoding="UTF-8"?><result command="searchhotels" tID="1457501395000001" ip="116.69.241.79" date="2016-03-09 05:29:59" version="2.0" elapsedTime="4.4613111019135"><currencyShort>USD</currencyShort><hotels count="17"><hotel runno="0" preferred="no" cityname="DUBAI" order="3" hotelid="911475"><from>398.4681<formatted>398.47</formatted></from><availability>available</availability></hotel><hotel runno="1" preferred="no" cityname="DUBAI" order="3" hotelid="444505"><from>412.8173<formatted>412.82</formatted></from><availability>available</availability></hotel><hotel runno="2" preferred="no" cityname="DUBAI" order="3" hotelid="149176"><from>397.3643<formatted>397.36</formatted></from><availability>available</availability></hotel><hotel runno="3" preferred="no" cityname="DUBAI" order="3" hotelid="1031048"><from>488.6109<formatted>488.61</formatted></from><availability>available</availability></hotel><hotel runno="4" preferred="yes" cityname="DUBAI" order="3" hotelid="1253328"><from>493.5906<formatted>493.59</formatted></from><availability>available</availability></hotel><hotel runno="5" preferred="no" cityname="DUBAI" order="3" hotelid="994075"><from>441.5159<formatted>441.52</formatted></from><availability>available</availability></hotel><hotel runno="6" preferred="no" cityname="DUBAI" order="3" hotelid="1466928"><from>432.6856<formatted>432.69</formatted></from><availability>available</availability></hotel><hotel runno="7" preferred="no" cityname="DUBAI" order="3" hotelid="31264"><from>441.5159<formatted>441.52</formatted></from><availability>available</availability></hotel><hotel runno="8" preferred="no" cityname="DUBAI" order="3" hotelid="71964"><from>430.478<formatted>430.48</formatted></from><availability>available</availability></hotel><hotel runno="9" preferred="yes" cityname="DUBAI" order="3" hotelid="1420308"><from>264.9095<formatted>264.91</formatted></from><availability>available</availability></hotel><hotel runno="10" preferred="yes" cityname="DUBAI" order="3" hotelid="838245"><from>483.6342<formatted>483.63</formatted></from><availability>available</availability></hotel><hotel runno="11" preferred="yes" cityname="DUBAI" order="3" hotelid="31084"><from>353.2127<formatted>353.21</formatted></from><availability>available</availability></hotel><hotel runno="12" preferred="no" cityname="DUBAI" order="3" hotelid="275975"><from>397.3643<formatted>397.36</formatted></from><availability>available</availability></hotel><hotel runno="13" preferred="no" cityname="DUBAI" order="3" hotelid="1066898"><from>432.6856<formatted>432.69</formatted></from><availability>available</availability></hotel><hotel runno="14" preferred="yes" cityname="DUBAI" order="3" hotelid="31434"><from>386.9073<formatted>386.91</formatted></from><availability>available</availability></hotel><hotel runno="15" preferred="no" cityname="DUBAI" order="3" hotelid="92008"><from>440.4121<formatted>440.41</formatted></from><availability>available</availability></hotel><hotel runno="16" preferred="no" cityname="DUBAI" order="3" hotelid="615835"><from>375.2885<formatted>375.29</formatted></from><availability>available</availability></hotel></hotels><successful>TRUE</successful></result>

    This code returns me this XML string

    **This is How i'm Binding grid**

    WebService service = new WebService();
    string xml = service.FindCountry();\\ getting xml string 
    DataSet ds = new DataSet();
    XmlTextReader reader = new XmlTextReader(new StringReader(xml));
    GridView1.DataSource = (ds.ReadXml(reader));
    GridView1.DataBind();

    **Exception Get while binding gridview**

    - Data source is an invalid type. It must be either an IListSource,
    IEnumerable, or IDataSource.

    Wednesday, March 9, 2016 8:25 AM

Answers

  • User36583972 posted

    Hi morfious90,

    From your error description, I suggest you can try the following two methods.

    1: ReadXml() returns XmlReadMode which you can't assign as DataSource. You can populate the DataSet and assign it as DataSource in a separate line. You can try the following code.

    DataSet ds = new DataSet();
    XmlTextReader reader = new XmlTextReader(new StringReader(xml));
    ds.ReadXml(reader);
    GridView1.DataSource = ds;
    GridView1.DataBind(); 
    

    2: You can refer the following tutorial. It defines a DataTable return type in the method and gridview can be bound by the DataTable which will return from a Web Service.

    Populate (Bind) GridView using Web Service (Web Method) in ASP.Net:

    http://www.aspsnippets.com/Articles/Populate-Bind-GridView-using-Web-Service-Web-Method-in-ASPNet.aspx

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 10, 2016 5:14 AM