[CLOSED] Need Help Using a Web Service with a Grid

  1. #1

    [CLOSED] Need Help Using a Web Service with a Grid



    I am trying to attach data from a web service to a grid... my web service returns a list of company names in an array but I can't get them to attach to the Store or the Grid. I'm not really sure of the correct way to call the web service and how it should return the data (I would like to just return a dataset if that's possible). I didn't follow the examples where the web service returned the suppliers from the Northwind database (I'm not very familiar with Linq or C#). Could someone point me in the correct direction or give me a quick sample? Thanks in advance,

    -Ian

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server"></head>
    <body>
        <form id="form1" runat="server">
        <ext:ScriptManager ID="ScriptManager1" runat="server"></ext:ScriptManager>   
        <ext:Store runat="server" ID="Store1">
            <Proxy>
                <ext:HttpProxy Method="GET" Url="CustomersSearchService.asmx/GetCustomers"></ext:HttpProxy>
            </Proxy>
            <Reader>
                <ext:ArrayReader>
                    <Fields>
                        <ext:RecordField Name="CompanyName"  />
                    </Fields>
                </ext:ArrayReader>
            </Reader>
        </ext:Store>     
        <ext:GridPanel 
                ID="GridPanel1" 
                runat="server" 
                StoreID="Store1" 
                StripeRows="true"
                Title="Array Grid" 
                TrackMouseOver="true"
                Width="600" 
                Height="350"
                AutoExpandColumn="CompanyName">
                <ColumnModel ID="ColumnModel1" runat="server">
                    <Columns>
                        <ext:Column ColumnID="CompanyName" Header="Company" Width="160" Sortable="true" DataIndex="CompanyName" />                    
                    </Columns>
                </ColumnModel>
            </ext:GridPanel>           
        </form>
    </body>
    </html>
    I also tried this for my reader
     <ext:Store runat="server" ID="Store1">
            <Proxy>
                <ext:HttpProxy Method="GET" Url="CustomersSearchService.asmx/GetCustomers"></ext:HttpProxy>
            </Proxy>
            <Reader>
                <ext:XmlReaderReaderID="ID">
                    <Fields>
                        <ext:RecordField Name="CompanyName" />
                        <ext:RecordField Name="ID" />
                    </Fields>
                </ext:XmlReader>
            </Reader>
    </ext:Store>
    here is my .asmx web service
    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports System.ComponentModel
    Imports Microsoft.ApplicationBlocks.Data
    
    ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    '<System.Web.Script.Services.ScriptService()> _
    <System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
    <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <ToolboxItem(False)> _
    Public Class CustomersSearchService
        Inherits System.Web.Services.WebService
    
        <WebMethod()> _
        Public Function GetCustomers(ByVal prefix As String) As String()
        'Public Function GetCustomers(ByVal prefix As String) As DataSet
            Dim cs As String = "Server=(local);Database=gulfstreammediagroup;Uid=sa;Pwd=sa"
            Dim ds As DataSet = SqlHelper.ExecuteDataset(cs, "spCustomerGetCustomerNames", prefix)
            Dim items As New List(Of String)
            Dim dr As DataRow
            For Each dr In ds.Tables(0).Rows
                items.Add(dr("CompanyName"))
            Next
            'Return ds
            Return items.ToArray()
        End Function
    End Class
    Here is the simple stored procedure I am using to return "company names"

    CREATE PROCEDURE [dbo].[spCustomerGetCustomerNames]
    @prefix varchar(100)
    AS
    BEGIN
      SET NOCOUNT ON;
      CREATE Table #temptable (CompanyName varchar(20), ID int)
      insert into #temptable (CompanyName, ID) values ('Company One', 1)
      insert into #temptable (CompanyName, ID) values ('Company Two', 2)
      insert into #temptable (CompanyName, ID) values ('Company Three', 3)
      select * From #temptable
    END
  2. #2

    RE: [CLOSED] Need Help Using a Web Service with a Grid

    Hi,

    If you work with WebService then need use XMLReader because your web service return xml.
    Unfortunately you can't return array from web service because XMLReader can't parse it.
    So, you should create class which will be return as List


    public class Company
        {
            private int id;
            private string companyName;
    
            //It is required for XML serialization
            public Company()
            {
            }
    
            public Company(int id, string companyName)
            {
                this.id = id;
                this.companyName = companyName;
            }
    
            public int ID
            {
                get { return id; }
                set { id = value; }
            }
    
            public string CompanyName
            {
                get { return companyName; }
                set { companyName = value; }
            }
        }
    Then you should fill company List (your web service method)

            [WebMethod]
            public List<Company> GetCustomers()
            {
                List<Company> customers = new List<Company>(4);
    
                for (int i = 0; i < 4; i++)
                {
                    customers.Add(new Company (i, "Company " + i ));
                }
    
                return customers;
            }
    If you see what response give web service then you see next:

      <?xml version="1.0" encoding="utf-8" ?> 
    - <ArrayOfCompany xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
    - <Company>
      <ID>0</ID> 
      <CompanyName>Company 0</CompanyName> 
      </Company>
    - <Company>
      <ID>1</ID> 
      <CompanyName>Company 1</CompanyName> 
      </Company>
    - <Company>
      <ID>2</ID> 
      <CompanyName>Company 2</CompanyName> 
      </Company>
    - <Company>
      <ID>3</ID> 
      <CompanyName>Company 3</CompanyName> 
      </Company>
      </ArrayOfCompany>
    What is it mean? It is mean that each record has name Company with children nodes: ID and CompanyName

    It is mean that we should have the following reader in Store
            <Reader>
                <ext:XmlReader ReaderID="ID" Record="Company">
                    <Fields>
                        <ext:RecordField Name="ID" />
                        <ext:RecordField Name="CompanyName" />                    
                    </Fields>
                </ext:XmlReader>
            </Reader>
    It should be XmlReader because response is XML. We must define Record="Company" and RecordField's ID nad CompanyName (it is service information for parser). Also we can define ReaderID=ID which means that field ID is key field

    Here is full example

    WebService
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    
    namespace Coolite.Sandbox.Temp.AjaxEvents
    {
        /// <summary>
        /// Summary description for CustomersSearchService
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
        // [System.Web.Script.Services.ScriptService]
        public class CustomersSearchService : System.Web.Services.WebService
        {
    
            [WebMethod]
            public List<Company> GetCustomers()
            {
                List<Company> customers = new List<Company>(4);
    
                for (int i = 0; i < 4; i++)
                {
                    customers.Add(new Company (i, "Company " + i ));
                }
    
                return customers;
            }
        }
    
        public class Company
        {
            private int id;
            private string companyName;
    
            //It is required for XML serialization
            public Company()
            {
            }
    
            public Company(int id, string companyName)
            {
                this.id = id;
                this.companyName = companyName;
            }
    
            public int ID
            {
                get { return id; }
                set { id = value; }
            }
    
            public string CompanyName
            {
                get { return companyName; }
                set { companyName = value; }
            }
        }
    }

    ASPX Page
    <%@ Page Language="C#" %>
    <%@ Register Assembly="Coolite.Ext.Web" Namespace="Coolite.Ext.Web" TagPrefix="ext" %>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <ext:ScriptManager ID="ScriptManager1" runat="server"></ext:ScriptManager>   
        
        <ext:Store runat="server" ID="Store1">
            <Proxy>
                <ext:HttpProxy Method="GET" Url="CustomersSearchService.asmx/GetCustomers"></ext:HttpProxy>
            </Proxy>
            
            <Reader>
                <ext:XmlReader ReaderID="ID" Record="Company">
                    <Fields>
                        <ext:RecordField Name="ID" />
                        <ext:RecordField Name="CompanyName" />                    
                    </Fields>
                </ext:XmlReader>
            </Reader>
        </ext:Store> 
        
        <ext:GridPanel 
                ID="GridPanel1" 
                runat="server" 
                StoreID="Store1" 
                StripeRows="true"
                Title="Array Grid" 
                TrackMouseOver="true"
                Width="600" 
                Height="350"
                AutoExpandColumn="CompanyName">
                <ColumnModel ID="ColumnModel1" runat="server">
                    <Columns>
                        <ext:Column ColumnID="ID" Header="ID" Width="160" Sortable="true" DataIndex="ID" />                    
                        <ext:Column ColumnID="CompanyName" Header="Company" Width="160" Sortable="true" DataIndex="CompanyName" />                    
                    </Columns>
                </ColumnModel>
            </ext:GridPanel>           
        </form>
    </body>
    </html>
    Please let me know if you still have difficults with this example


  3. #3

    RE: [CLOSED] Need Help Using a Web Service with a Grid

    Thanks so much for the sample and the explanations, that's what I needed... one little thing however:

    This works fine if I use Method="POST" instead of Method="GET" in the HttpProxy (GET doesn't fill the datagrid with any data)...

    Is that correct? Is there some setting I need to have in order to use the GET method? What is the difference between using the GET and POST?
  4. #4

    RE: [CLOSED] Need Help Using a Web Service with a Grid

    Hi,

    The POST method should be used if you try to submit some more information (you can use GET but size of submitted data can't exceed more than 255-8192 bytes, it depends from browser and server) .


    If you are hitting a limit on length, you should consider using POST instead of GET. POST does not have such low limits on the size of name/value pairs, because the data is sent in the header, not in the URL. The limit on POST size, by default, is 2 MB on IIS 4.0 and 128 KB on IIS 5.0. POST is also a little more secure than GET -- it's tougher (though not impossible) to tinker with the values of POSTed variables, than values sitting in the querystring.*



    Also in some browser (FF for example) if use POST but no parameters which submitted to the server you can get error:*411 Content Length Required


    Also it can be need to include in your web.config next section which allows GET and POST requests to the web-service


    <webServices>
    <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
    </protocols>
    </webServices>





  5. #5

    RE: [CLOSED] Need Help Using a Web Service with a Grid

    Great... thank you so much for this example, it is exactly what I needed. Everything works perfectly.

Similar Threads

  1. Need Help Using a Web Service !
    By metallica87 in forum 1.x Help
    Replies: 2
    Last Post: Mar 19, 2012, 3:25 AM
  2. Replies: 4
    Last Post: Jul 28, 2011, 4:36 PM
  3. Replies: 1
    Last Post: Feb 17, 2010, 9:38 AM
  4. [CLOSED] web service problem
    By speedstepmem2 in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Nov 02, 2009, 9:18 AM
  5. Software as a service?
    By dbassett74 in forum Licensing
    Replies: 1
    Last Post: May 21, 2009, 3:02 PM

Posting Permissions