This is a migrated thread and some comments may be shown as answers.

Problem trying to bind to Web Service (server method failed)

7 Answers 34 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
Yan Moura
Top achievements
Rank 1
Veteran
Iron
Yan Moura asked on 24 Feb 2021, 10:56 PM

Hello!

I am trying to run the demo found here but with no success (not working). I made the minimum of changes to get data from my own DB but I keep receiveing the same error saying that "The server method 'GetiPMP' failed". I already double checked path, code and I even referred to the solution showed here but no success.

My code...

 

PMP_WS.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="iPMP_WS.aspx.cs" Inherits="custom_scripts_iPMP_iPMP_WS" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <script type="text/javascript">
        function requesting(sender, eventArgs) {
            var context = eventArgs.get_context();
            //Data passed to the service.
            context["ClientData"] = "ClientData_Passed_To_The_Service";
        }
    </script>
</head>
 
<body>

    <form id="form1" runat="server" >
        <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />

        <telerik:RadAutoCompleteBox RenderMode="Lightweight" runat="server" ID="RadAutoCompleteBox1" ClientID="RadAutoCompleteBox1" 
            autopostback="true" 
            InputType="Token" Width="100%" Visible="true"
            AllowCustomEntry = "false"
            DropDownWidth="300px"
            onclientrequesting="requesting" >

            <WebServiceSettings Path="iPMP_WS.asmx" Method="GetiPMP" />
        </telerik:RadAutoCompleteBox>

    </form>

</body>
</html>

 

iPMP_WS.asmx

<%@ WebService Language="C#" CodeBehind="~/App_Code/iPMP_WS.cs" Class="iPMP_WS" %>

 

iPMP_WS.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Web.UI;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class iPMP_WS : System.Web.Services.WebService
{

    public iPMP_WS()
    {
    }

    [WebMethod]
    public static AutoCompleteBoxData GetiPMP(RadAutoCompleteContext context)
    {
        string clientData = context["ClientData"].ToString();

        string sql = "SELECT top 10 desc as 'edesc',codes as 'ecode' FROM INVENTORY";
        SqlDataAdapter adapter = new SqlDataAdapter(sql,ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString);
        DataTable data = new DataTable();
        adapter.Fill(data);
        List<AutoCompleteBoxItemData> result = new List<AutoCompleteBoxItemData>();
        AutoCompleteBoxData dropDownData = new AutoCompleteBoxData();

        result = new List<AutoCompleteBoxItemData>();

        for (int i = 0; i < data.Rows.Count; i++)
        {
            AutoCompleteBoxItemData itemData = new AutoCompleteBoxItemData();
            itemData.Text = data.Rows[i]["edesc"].ToString();
            itemData.Value = data.Rows[i]["ecode"].ToString();
            result.Add(itemData);
        }

        dropDownData.Items = result.ToArray();
        return dropDownData;
    }
}

Any idea?

7 Answers, 1 is accepted

Sort by
0
Peter Milchev
Telerik team
answered on 26 Feb 2021, 04:57 PM

Hi Yan,

I cannot see the ScriptService attribute that is highlighted in this reply:

Please try adding it and see if that would fix the issue.

Regards,
Peter Milchev
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Yan Moura
Top achievements
Rank 1
Veteran
Iron
answered on 26 Feb 2021, 06:44 PM

Hi Peter,

 

I added the missing line to the code but the problem still persists...

I even replace actual code with test data to discard the possibility of the issue being caused by a DB connection error with this:

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Web.UI;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// Summary description for ReadACB
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class iPMP_WS : System.Web.Services.WebService
{

    public iPMP_WS()
    {
    }

    [WebMethod]
    public static AutoCompleteBoxData GetiPMP(RadAutoCompleteContext context)
    {
        string clientData = context["ClientData"].ToString();

        List<AutoCompleteBoxItemData> result = new List<AutoCompleteBoxItemData>();
        AutoCompleteBoxData dropDownData = new AutoCompleteBoxData();
        result = new List<AutoCompleteBoxItemData>();
        AutoCompleteBoxItemData itemData = new AutoCompleteBoxItemData();
        dropDownData.Items = result.ToArray();
        itemData.Text = "mytext";
        itemData.Value = "myvalue";
        result.Add(itemData);
        return dropDownData;
    }
}

0
Yan Moura
Top achievements
Rank 1
Veteran
Iron
answered on 26 Feb 2021, 06:57 PM
I may be wrong but seems to me that it is not finding the method.
0
Yan Moura
Top achievements
Rank 1
Veteran
Iron
answered on 26 Feb 2021, 07:02 PM

If I replace the Path attribute with anything that does not exist the error is exactly the same. For instance:

<WebServiceSettings Path="foo.asmx" Method="GetiPMP" />

Isn't weird?

PS: The asmx file is within the App_Code folder of my project. Is really just a matter of have it there or should I have to someaht "register" it?

0
Yan Moura
Top achievements
Rank 1
Veteran
Iron
answered on 02 Mar 2021, 01:32 PM

Peter,

I figured the issue. This demo will NEVER work as is and and the demo page is not intuitive. In the JS function, look at the bold part:

 function requesting(sender, eventArgs) {
            var context = eventArgs.get_context();
            //Data passed to the service.
            context["ClientData"] = "ClientData_Passed_To_The_Service";
        }

It never should be coded like this because it will mislead the user. Believe it or not but it's NOT obvious that "ClientData_Passed_To_The_Service" should be replaced by user data. For me it gave the impression of being an internal control attribute that should be kept as in the demo, for instance as when you set an attribute like InputType="Token" or InputType="Text". For me "ClientData_Passed_To_The_Service" sounded like this.

And the proof that the demo will mislead anyone is that even admins didn't realize what was going on! Otherwise someone should have said "Hey you have to replace 'ClientData_Passed_To_The_Service' with your own data or var!".

Anyway the fix is:

JS:

function requesting(sender, eventArgs) {

            var context = eventArgs.get_context();

            var myparam = "foo";

            context["myparam"] = myparam;

        }

 

iPMP_WS (only the partial CS chunk where change is needed):

    [WebMethod]
    public static AutoCompleteBoxData GetiPMP(RadAutoCompleteContext context)
    {
        string clientData = context["myparam"].ToString();

 

With this in mind the user can pass how many parameters he wants since "context" is an array of data. For instance in JS:

context["myparam"] = myparam;

context["foo"] = 10;

context["blah"] = "hello world";

 

And in the respective code behind of the ASMX file it's just:

string myparam = context["myparam"].ToString();

string foo = context["foo"].ToString();

string blah = context["blah"].ToString();

 

And use it accordingly.

 

Web Service is an incredibly easy and powerful way of binding AutoCompleteBox to DB but due to a bad demo I wasted one week of work running circles. I hope that this solution save others from suffer with it as I did.

0
Accepted
Peter Milchev
Telerik team
answered on 03 Mar 2021, 05:27 PM

Hello Yan,

You are correct, the comments about the value could be improved to be more descriptive and explanatory. 

Regarding the values, now that you have the method working properly, please add again these snippets and do not use them in the method, just to confirm that the values are passed correctly:

  1. In the requesting function - context["ClientData"] = "ClientData_Passed_To_The_Service";
  2. Optionally, in the web method itself - string testData = context["ClientData"].ToString();

Also, you can add a breakpoint in the web service method itself and see what the actual error is.

 

You can also inspect the request in the Network tab of the DevTools and see its response. It should contain more information about the actual server error.

Regards,
Peter Milchev
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Yan Moura
Top achievements
Rank 1
Veteran
Iron
answered on 03 Mar 2021, 07:40 PM

Thank you Peter!

It's everything working smoothly now. :-)

Tags
AutoCompleteBox
Asked by
Yan Moura
Top achievements
Rank 1
Veteran
Iron
Answers by
Peter Milchev
Telerik team
Yan Moura
Top achievements
Rank 1
Veteran
Iron
Share this question
or