Json Inquiry

ryoka012

Active member
Joined
Oct 13, 2011
Messages
32
Programming Experience
Beginner
Hi Experts,

I have this script to pass a parameters to from my code behind to json and it seems it is not working.

Please see my code in my script
Code:
    <script type="text/javascript">
       
        $(document).ready(function () {
            //var DatafieldText ="Code"
            //var DatafieldValue="Description"
            //var DropDownList="DropDownList1"
            //var tableName="location"
            //var fieldName=""
            var parameter = { DatafieldText: 'Code', DatafieldValue: 'Description', DropDownList: 'DropDownList1', tableName: 'location', fieldName: '' }
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Default.aspx/BindDatatoDropdown",
                    //data: '{"DatafieldText":' + DatafieldText + ',"DatafieldValue":' + DatafieldValue + ',"DropDownList":' + DropDownList + ',"tableName":' + tableName + ',"fieldName":' + fieldName + '}',


                    data: JSON.stringify(parameter),
                    dataType: "json",
                    success: function (data) {
                        $.each(data.d, function (key, value) {
                            $("#DropDownList1").append($("<option></option>").val(value.LocationCode).html(value.LocationDescription));
                        });
                    },
                    error: function (result) {
                        alert("Error");
                    }
                });
            });
    
</script>
code behind
Code:
 <WebMethod()> _
    Public Shared Function BindDatatoDropdown(ByVal DatafieldText As String, ByVal DatafieldValue As String, ByVal DropDownList As DropDownList, ByVal table As String, ByVal fieldName As String) As CountryLocation()
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionGCISVerificationString").ToString())
        Dim where As String
        If fieldName = "" Then
            where = ""
        Else
            where = " where " & fieldName
        End If
        Dim LocationDetails As New List(Of CountryLocation)()
        Try
            con.Open()
            Dim cmd As SqlCommand = con.CreateCommand
            cmd.CommandType = CommandType.Text
            cmd.CommandText = "Select " & DatafieldValue & " , " & DatafieldText & " from " & table & where & " order by " & DatafieldValue & " asc "
            Dim dr As SqlDataReader = cmd.ExecuteReader
            Dim dt As New DataTable
            dt.Load(dr)
            For Each dtrow As DataRow In dt.Rows
                Dim country As New CountryLocation()
                country.LocationCode = Convert.ToInt32(dtrow(DatafieldText).ToString())
                country.LocationDescription = dtrow(DatafieldValue).ToString()
                LocationDetails.Add(country)
            Next
        Catch ex As Exception
            HttpContext.Current.Session("error_message") = ex.Message & "</br>" & "Please check the Data Entry of Civil Status."
        Finally
            If con IsNot Nothing Then
                con.Close()
                con.Dispose()
            End If
        End Try
        Return LocationDetails.ToArray()
    End Function
    Public Class CountryLocation
        Private Code As Integer
        Private Description As String
        Public Property LocationCode() As Integer
            Get
                Return Code
            End Get
            Set(ByVal value As Integer)
                Code = value
            End Set
        End Property


        Public Property LocationDescription() As String
            Get
                Return Description
            End Get
            Set(ByVal value As String)
                Description = value
            End Set
        End Property


    End Class
Can anyone direct me to to solved this issue.
Thanks
 
Top Bottom