JavaScript Autocomplete Array not working with WebMethod in vb.net

87 views Asked by At

Autocomplete TextBox code in JavaScript

function getList_FixedValue() {
    var arr = ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"];
    return arr;
}

function getList_FromServerSide() {

    $.ajax({
        type: "POST",
        url: "patient-problem-submit.aspx/GetCCList",
        data: '{doctorId: "' + $("#<%=hdnDoctorId.ClientID%>")[0].value + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            alert(response.d); //here alert shows my expected data
            return response.d;
        },
        failure: function (response) {
            alert("failed to get data");
        }
    });
}

$('#txtCCPick').autocompleteArray(
        //getList_FixedValue(), //this works fine
        getList_FromServerSide(), //this not working
        {
            delay: 10,
            minChars: 1,
            matchSubset: 1,
            onItemSelect: selectItem,
            onFindValue: findValue,
            autoFill: true,
            maxItemsToShow: 10
        }
     );
 });

WebMethod in VB.......

<System.Web.Services.WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Shared Function GetCCList(ByVal doctorId As String) As String()
    Dim customers As New List(Of String)()
    Using conn As New SqlConnection()
        conn.ConnectionString = ConfigurationManager.ConnectionStrings("ConnStr").ConnectionString
        Using cmd As New SqlCommand()
            cmd.CommandText = String.Format("SELECT pkCCID, CCName FROM CC WHERE fkDoctorID = " + doctorId + " order by CCName")
            cmd.CommandType = CommandType.Text
            'cmd.Parameters.AddWithValue("@DoctorId", doctorId)
            cmd.Connection = conn
            conn.Open()

            Dim dbAdpt As New SqlDataAdapter(cmd)
            Dim ds As New DataSet

            dbAdpt.Fill(ds)

            If (Not ds Is Nothing) Then
                If (ds.Tables(0).Rows.Count > 0) Then
                    For Each row As DataRow In ds.Tables(0).Rows
                        customers.Add(String.Format("{0}", row("CCName")))
                    Next
                End If
            End If
            conn.Close()
        End Using
    End Using

    Return customers.ToArray()

End Function

Inside autocompleteArray, When I call getList_FixedValue() function then items are loaded my textbox properly. but when I call getList_FromServerSide() function then items are not loaded in my textbox. So i need help for this issue. thanks in advance.

1

There are 1 answers

0
Kaysar Ahmed On BEST ANSWER

Ultimately I got my solution by the following way. problem was that there are no direct return option from success portion of ajax call

 function getList_FromServerSide() {

    var result;
    $.ajax({
        type: "POST",
        url: "patient-problem-submit.aspx/GetCCList",
        data: '{doctorId: "' + $("#<%=hdnDoctorId.ClientID%>")[0].value + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: !1,
        success: function (response) {
            //alert(response.d); //here alert shows my expected data
            result = response.d;

        },
        failure: function (response) {
            alert("failed to get data");
        }
    });
    return result;
}