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

Error binding autocomplete to web method

5 Answers 116 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
Oren
Top achievements
Rank 1
Oren asked on 09 Jan 2014, 12:48 PM
Hello,
I tried to build a small test page that has a autocompletebox bind to a web method in the code behind of the page.
I copied the example from the autocompletedemo page, but I got this error : "The server method 'GetCompanyNames' failed."
There is a snippet of my code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html>
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <telerik:RadScriptManager runat="server"></telerik:RadScriptManager>
        <telerik:RadAjaxPanel runat="server" ID="RadAjaxPanel1">
            <div>
                <fieldset style="width: 400px">
                    <legend>Web service binding</legend>
                    <label for="RadAutoCompleteBox2">
                        Company Names:</label>
                    <telerik:RadAutoCompleteBox runat="server" ID="RadAutoCompleteBox2" Width="250" DropDownHeight="150"
                        DropDownWidth="250">
                        <WebServiceSettings Path="Defualt.aspx" Method="GetCompanyNames" />
                    </telerik:RadAutoCompleteBox>
                    <p>
                        <asp:Label runat="server" ID="Label2" />
                    </p>
                </fieldset>
            </div>
        </telerik:RadAjaxPanel>
    </form>
</body>
</html>


Code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    [WebMethod]
    public AutoCompleteBoxData GetCompanyNames(object context)
    {
        string searchString = ((Dictionary<string, object>)context)["Text"].ToString();
        List<AutoCompleteBoxItemData> result = new List<AutoCompleteBoxItemData>();
        AutoCompleteBoxItemData childNode = new AutoCompleteBoxItemData();
        childNode.Text = "aaaa";
        childNode.Value = "aaaa";
        result.Add(childNode);
 
        AutoCompleteBoxData res = new AutoCompleteBoxData();
        res.Items = result.ToArray();
 
        return res;
    }
}



Thank you for your help,
Kind regards,
Oren Yardeni.

5 Answers, 1 is accepted

Sort by
0
Nencho
Telerik team
answered on 14 Jan 2014, 09:42 AM
Hello Oren,

Please make sure that you had correctly referenced the aspx page name, where the WebMethod is implemented. As I can see, you are referring to <WebServiceSettings Path="Defualt.aspx", while the page is _Default.aspx.

Regards,
Nencho
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Oren
Top achievements
Rank 1
answered on 14 Jan 2014, 11:39 AM
Hello,
Thank you for your reply , I fixed this and check the method name is correct and the path is correct but still I get the same error.
Kind regards,
Oren
0
Nencho
Telerik team
answered on 17 Jan 2014, 09:48 AM
Hello Oren,

The Access Modifier of the WebMethod should be public static, in order to call the method correctly. I am sending you a sample project, demonstrating the entire correct implementation.

Note : dll files are removed from the attachment.

Regards,
Nencho
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
David
Top achievements
Rank 1
answered on 26 Jan 2018, 04:03 PM

[quote]Nencho said:Hello Oren,

The Access Modifier of the WebMethod should be public static, in order to call the method correctly.

[/quote]

Nencho, your response conflicts with the current documentation: https://docs.telerik.com/devtools/aspnet-ajax/controls/autocompletebox/data-binding/binding-to-web-service

This may be due to changed since your post back in 2014, but it would be good to clarify.

0
Peter Milchev
Telerik team
answered on 31 Jan 2018, 11:45 AM
Hello David,

Please excuse us for the misunderstanding. 

In a PageMethods binding scenario, the Web method should be static while it should not be static in a Web Service binding scenario. 

Attached is a sample project implementing both approaches. To run it, the .NET 4.5 version of the Telerik assemblies should be added to the bin folder of the project. 

PageMethods:


Default.aspx

<telerik:RadScriptManager ID="RadScriptManager1" runat="server" EnablePageMethods="true" />
<telerik:RadAutoCompleteBox RenderMode="Lightweight" ID="RadAutoCompleteBox3" runat="server" Width="400" DropDownHeight="150"
    EmptyMessage="Select Company Names">
    <WebServiceSettings Method="GetCompanyNames" Path="default.aspx" />
</telerik:RadAutoCompleteBox>

Default.aspx.cs

[WebMethod]
public static AutoCompleteBoxData GetCompanyNames(object context)
{
    string searchString = ((Dictionary<string, object>)context)["Text"].ToString();
    DataTable data = GetChildNodes(searchString);
    List<AutoCompleteBoxItemData> result = new List<AutoCompleteBoxItemData>();
 
    foreach (DataRow row in data.Rows)
    {
        AutoCompleteBoxItemData childNode = new AutoCompleteBoxItemData();
        childNode.Text = row["ProductName"].ToString();
        childNode.Value = row["ProductID"].ToString();
        result.Add(childNode);
    }
 
    AutoCompleteBoxData res = new AutoCompleteBoxData();
    res.Items = result.ToArray();
 
    return res;
}
 
private static DataTable GetChildNodes(string searchString)
{
    SqlCommand selectCommand = new SqlCommand(@"SELECT * FROM [Products] WHERE ProductName LIKE @ProductName + '%'");
    selectCommand.Parameters.AddWithValue("ProductName", searchString.Replace("%", "[%]").Replace("_", "[_]"));
    return GetData(selectCommand);
}
 
private static DataTable GetData(SqlCommand selectCommand)
{
    selectCommand.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
    SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
 
    DataTable data = new DataTable();
    adapter.Fill(data);
 
    return data;
}

Web Service: 

Default.aspx

<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>

<telerik:RadAutoCompleteBox runat="server" ID="RadAutoCompleteBox1" OnClientRequesting="requesting">
    <WebServiceSettings Path="LoadEntries.asmx" Method="GetCompanyNames" />
</telerik:RadAutoCompleteBox>

LoadEntries.asmx

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

LoadEntries.asmx.cs

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 LoadEntries : System.Web.Services.WebService
{
 
    public LoadEntries()
    {
 
        //Uncomment the following line if using designed components
        //InitializeComponent();
    }
 
    [WebMethod]
    public AutoCompleteBoxData GetCompanyNames(RadAutoCompleteContext context)
    {
        //Accesses the additional data sent from the client.
        string clientData = context["ClientData"].ToString();
 
        string sql = "SELECT  * from Customers WHERE CompanyName LIKE '" + context.Text + "%'";
        SqlDataAdapter adapter = new SqlDataAdapter(sql,
            ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].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]["CompanyName"].ToString();
            itemData.Value = data.Rows[i]["CustomerID"].ToString();
 
            result.Add(itemData);
        }
 
        dropDownData.Items = result.ToArray();
        return dropDownData;
    }
}


Regards,
Peter Milchev
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
AutoCompleteBox
Asked by
Oren
Top achievements
Rank 1
Answers by
Nencho
Telerik team
Oren
Top achievements
Rank 1
David
Top achievements
Rank 1
Peter Milchev
Telerik team
Share this question
or