Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I need to do the autocomplete textbox in asp.net website, for that i am using ajax post to get the the datatable

Here is my client side code :
------------------------------------------------------------------------------
C#
$(function () {
        debugger;
        var availableTags = getbroker()
    });

function getbroker() {
        var RejectedCount;
        $.ajax({
            type: 'POST',
            url: 'common.aspx/get_broker',
            contentType: 'application/json; charset=utf-8',
            data: '{"amc":"' + strcorp + '","schema":"' + ucode + '"}',
            dataType: 'json',
            async: false,
            success: function (r) {
                alert("sucess");
                RejectedCount = r.d;
                get_new(RejectedCount)
            }, error: function (x, e) {
                alert("error");
            }
        });
}

function get_new(RejectedCount) {
// here txt_broker is textbox id
$("#txt_broker").autocomplete({
    source: RejectedCount
        });
}

Server side(first way)
-------------------------------------------------------------------------
VB
<webmethod()>
    Public Shared Function get_broker(ByVal amc As String, ByVal schema As String) As string

        Dim dt As DataTable
        Dim output As New StringBuilder       
        Try

           dt= here i am get the datatable using stored procedure
            If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
               
                output.Append("(")
                For i As Integer = 0 To dt.Rows.Count - 1
                    output.Append("" & dt.Rows(i)("brok_dlr_code").ToString() & "")
                   If i <> dt.Rows.Count - 1 Then
                        output.Append(",")
                    End If
                Next

                output.Append(")")
                Return output.ToString()
            End If
        Catch ex As Exception
         
        End Try       
    End Function


Second way
-------------------------------------------------------------------------------
VB
<WebMethod()>
    Public Shared Function get_broker(ByVal amc As String, ByVal schema As String) As DataTable

        Dim dt As DataTable
       
        Try

           dt= here i am get the datatable using stored procedure
            If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
                Return dt

            End If
        Catch ex As Exception

        End Try
    End Function



I have tried with first way and second way... both are getting error alert msg
so please any one help me

my requirements
---------------
i have to get the string values from the datatable . while i m typing the related values should be come under the textbox

i m strugling with 2 days...

thanks in advansce

Velsamy
Posted
Updated 4-Nov-14 1:29am
v2
Comments
Sinisa Hajnal 4-Nov-14 7:29am    
You should return JsonResult from your controller / method or at least string in JSON format, not datatable

1 solution

See here for easy-to-use JSON library[^]

You should return JSON from your method, that is all there should be to it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900