Insert Value in a textbox using Json

ryoka012

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

How can i populate/Insert a value on my textbox.

My scenario is when i select a value on my dropdownlist, pass this value on my json parameter and call the data from server side.
after successfully calling the query get the desired zipcode and pass it on my textbox.

Iam at lost on how i will be doing this.

Please see my code:
Code Behide
VB.NET:
    <WebMethod()> _
    Public Shared Function BindDataZipcode(ByVal LocationCode As String) As List(Of ZipAreaCode)
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim ZipAreaCodes As New List(Of ZipAreaCode)()
        Try
            Using con
                Using cmd As New SqlCommand("usp_GetZipcode")
                    cmd.Connection = con
                    cmd.CommandType = CommandType.StoredProcedure
                    cmd.Parameters.AddWithValue("@LocationCode", LocationCode)
                    con.Open()
                    Using sdr As SqlDataReader = cmd.ExecuteReader()
                        While sdr.Read()
                            ZipAreaCodes.Add(New ZipAreaCode() With { _
                             .ZipCodeNumber = sdr("Zipcode").ToString(), _
                             .AreaCodeNumber = sdr("AreaCode").ToString() })
          
                        End While
                    End Using
                    Return ZipAreaCodes
                End Using
            End Using
        Catch ex As Exception
            HttpContext.Current.Session("error_message") = ex.Message & "</br>" & "Please Location Data in JSON."
            'VBCommonCmd.Redirect("~/ErrorEVPVerificationPage.aspx")
            'Return ZipAreaCodes
        Finally
            If con IsNot Nothing Then
                con.Close()
                con.Dispose()
            End If
        End Try
    End Function
     
    Public Class ZipAreaCode
        Public Property ZipCodeNumber() As String
        Public Property AreaCodeNumber() As String
    End Class

Client Side:
VB.NET:
 $(".DropDownlist").combobox({
                select: function (event, ui) {
                    var f = document.getElementById("<%=DropDownlist.ClientID%>");
                    f.onchange();
                    var LocationCode = $(f).val();              
                    if (LocationCode != null) {
                        var parameter = { LocationCode: LocationCode };
                        $.ajax({
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            url: "Default.aspx/BindDataZipcode",
                            data: JSON.stringify(parameter),
                            dataType: "json",
                             success: function (data) {
                                alert(data.ZipCodeNumber);     
                    },             
                            error: function (data) {
                                alert("Error " + data.status);
                            }
                        });
                    }
                }
                });

Thanks for your expertise
 
Hi,

Just got my problem solved.

I just have a invalid stored proc.

Reminder.
always used msgbox(ex.message) to easily check the error message.
 
Back
Top