[Solved] jQuery Autocomplete Error: Invalid web service call, missing value for parameter

lingers
 
on May 06, 2021 10:47 PM
Sample_180865.zip
4450 Views

i need to use (id) generated from searchText() to autocmplete GetAutoCompleteData3 which is SearchText3(). But since i added id that is 

data: "{ 'pid': '" + request.term + "', id: '" + request.term + +"'}",i keep gettin errors in the first Textbox( localhost says error) using .autosuggest

while i get this error in  the second Textbox using .autosuggest3.

{"Message":"Invalid web service call, missing value for parameter: \u0027pid\u0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}.

$(function () {
    SearchText();
});
function SearchText() {
    $(".autosuggest").autocomplete({
        source: function (request, response) {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Order.aspx/GetAutoCompleteData",
                data: "{ 'pid': '" + request.term + "', id: '" + request.term + +"'}",
                dataType: "json",
                success: function (data) {
                    if (data.d.length > 0) {
                        response($.map(data.d, function (item) {
                            return {
                                label: item.split('/')[0],
                                val: item.split('/')[1]
                            }
                        }));
                    }
                    else {
                        response([{ label: 'No Records Found', val: -1 }]);
                    }
                },
                error: function (result) {
                    alert("Error");
                }
            });
        },
        select: function (event, ui) {
            if (ui.item.val == -1) {
                return false;
            }
            $('#lblUserId').text(ui.item.val);
        }
    });
}
$(function () {
    SearchText3();
});

function SearchText3() {
    $(".autosuggest3").autocomplete({
        source: function (request, response) {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Order.aspx/GetAutoCompleteData3",
                data: "{ 'posino': '" + request.term + "', id: '" + $(".autosuggest").val() + "'}",
                dataType: "json",
                success: function (data) {
                    if (data.d.length > 0) {
                        response($.map(data.d, function (item) {
                            return {
                                label: item.split('/')[0],
                                val: item.split('/')[1]
                            }
                        }));
                    }
                    else {
                        response([{ label: 'No Records Found', val: -1 }]);
                    }
                },
                error: function (result) {
                    alert(result.responseText);
                }
            });
        },
        select: function (event, ui) {
            if (ui.item.val == -1) {
                return false;
            }
            $('#lblUserId').text(ui.item.val);
        }
    });
}

 

[WebMethod]
public static List<string> GetAutoCompleteData(string pid, string id)
{
    List<string> result = new List<string>();
    using (SqlConnection con = new SqlConnection("Data Source=NERO-SIGBENU\\SQLEXPRESS01;Integrated Security=true;Initial Catalog=arpackaging;"))
    {
        using (SqlCommand cmd = new SqlCommand("select id,pid from job where pid LIKE '%'+@SearchText+'%'", con))
        {
            con.Open();
            cmd.Parameters.AddWithValue("@SearchText", pid);
            cmd.Parameters.AddWithValue("@id", id);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                result.Add(string.Format("{0}/{1}", dr["pid"], dr["id"]));
            }
            return result;
        }
    }
}
[WebMethod]
public static List<string> GetAutoCompleteData3(string posino, string pid)
{
    List<string> result = new List<string>();
    using (SqlConnection con = new SqlConnection("Data Source=NERO-SIGBENU\\SQLEXPRESS01;Integrated Security=true;Initial Catalog=arpackaging;"))
    {
        using (SqlCommand cmd = new SqlCommand("select id,posino from job_cylinder where pid=@id order by id asc", con))
        {
            con.Open();
            cmd.Parameters.AddWithValue("@SearchText3", posino);
            cmd.Parameters.AddWithValue("@pid", pid);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                result.Add(string.Format("{0}/{1}", dr["posino"], dr["id"]));
            }
            return result;
        }
    }
}

how do i successfully get the id from the first web method and use the id in the second web method.

Please help

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on May 10, 2021 11:59 PM

Hi lingers,

Check this example. Now please take its reference and correct your code.

HTML

<div>
    <asp:TextBox ID="txtSearch" class="autosuggest" runat="server"></asp:TextBox>
    <asp:HiddenField ID="hfPId" runat="server" />
    <br />
    <br />
    <asp:TextBox ID="TextBox2" class="autosuggest3" runat="server"></asp:TextBox>
    <br />
</div>
<link href="https://code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script type="text/javascript">
    $(function () {
        SearchText();
        SearchText3();
    });
    function SearchText() {
        $(".autosuggest").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Default.aspx/GetAutoCompleteData",
                    data: "{ 'pid': '" + request.term + "'}",
                    dataType: "json",
                    success: function (data) {
                        if (data.d.length > 0) {
                            response($.map(data.d, function (item) {
                                return {
                                    label: item.split('/')[0],
                                    val: item.split('/')[1]
                                }
                            }));
                        }
                        else {
                            response([{ label: 'No Records Found', val: -1 }]);
                        }
                    },
                    error: function (result) {
                        alert("Error");
                    }
                });
            },
            select: function (event, ui) {
                if (ui.item.val == -1) {
                    return false;
                }
                $('[id*=hfPId]').val(ui.item.val);
            }
        });
    }

    function SearchText3() {
        $(".autosuggest3").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Default.aspx/GetAutoCompleteData3",
                    data: "{ 'posino': '" + request.term + "', pid: '" + $('[id*=hfPId]').val() + "'}",
                    dataType: "json",
                    success: function (data) {
                        if (data.d.length > 0) {
                            response($.map(data.d, function (item) {
                                return {
                                    label: item.split('/')[0],
                                    val: item.split('/')[1]
                                }
                            }));
                        }
                        else {
                            response([{ label: 'No Records Found', val: -1 }]);
                        }
                    },
                    error: function (result) {
                        alert(result.responseText);
                    }
                });
            },
            select: function (event, ui) {
                if (ui.item.val == -1) {
                    return false;
                }
            }
        });
    }
</script>

Namespaces

using System.Data.SqlClient;
using System.Web.Services;

Code

[WebMethod]
public static List<string> GetAutoCompleteData(string pid)
{
    List<string> result = new List<string>();
    using (SqlConnection con = new SqlConnection("Data Source=.\\SQL2014;Integrated Security=true;Initial Catalog=AjaxSamples;"))
    {
        using (SqlCommand cmd = new SqlCommand("select id,pid from Auto1 where pid LIKE '%'+@SearchText+'%'", con))
        {
            con.Open();
            cmd.Parameters.AddWithValue("@SearchText", pid);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                result.Add(string.Format("{0}/{1}", dr["pid"], dr["id"]));
            }
            return result;
        }
    }
}

[WebMethod]
public static List<string> GetAutoCompleteData3(string posino, string pid)
{
    List<string> result = new List<string>();
    using (SqlConnection con = new SqlConnection("Data Source=.\\SQL2014;Integrated Security=true;Initial Catalog=AjaxSamples;"))
    {
        using (SqlCommand cmd = new SqlCommand("select id,posino from Auto2 where pid=@id AND posino LIKE '%'+@SearchText3+'%' order by id asc", con))
        {
            con.Open();
            cmd.Parameters.AddWithValue("@SearchText3", posino);
            cmd.Parameters.AddWithValue("@id", pid);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                result.Add(string.Format("{0}/{1}", dr["posino"], dr["id"]));
            }
            return result;
        }
    }
}

Screenshot