Link to home
Create AccountLog in
Avatar of Cubbybulin
Cubbybulin

asked on

Use Ajax, jquery and jSon to get data from SQL database in asp.net and fill a listbox control

Need some help getting data from SQL database using Ajax, Json and JQuery in asp.net 2010.
I need to be able to get a list of items from a table and populate a listbox control. Not sure how to set up the Json and pass back forth the data string. Examples welcomed.
Avatar of Imran Javed Zia
Imran Javed Zia
Flag of Pakistan image

ASKER CERTIFIED SOLUTION
Avatar of uaexpert
uaexpert

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Cubbybulin
Cubbybulin

ASKER

Well, I have this working to get data from a single SQL record.

My script to fill a label and textbox...
 $(document).ready(function () {
             $('#bAddDr').click(function () {
                 var u
                 u = "Help-Support.aspx?f=1&timestamp=" + (new Date()).getTime();
                 $('#Label170').text('Loading...');
                 $.getJSON(u, function (data) {
                     $('#Label170').text(data['topic']);
                     $('#tDesc').text(data['description']);
                 });
                 return false;
             });
         });

In my support page I have...
<DataContract()> Public Class Doc
        <DataMember()> Public topic As String
        <DataMember()> Public description As String
        <DataMember()> Public file As String
    End Class

Private Sub GetHelpList()
        Dim dc As New Doc
        cnn.Open()
        cmd.CommandText = "Select * from tbl_help  order by help_topic"
        dr = cmd.ExecuteReader
        If dr.HasRows Then
            Do While dr.Read()

                dc.topic = nz(dr("help_topic"))
                dc.description = nz(dr("help_description"))
                dc.file = nz(dr("help_file"))

            Loop
            doResponse(GetType(Doc), dc)
        End If
        dr.Close()
        cnn.Close()
    End Sub

    Private Sub doResponse(ByVal t As Type, ByVal o As Object)
        Dim ser As DataContractJsonSerializer
        Dim stream1 As New MemoryStream
        ser = New DataContractJsonSerializer(t)
        ser.WriteObject(stream1, o)
        stream1.Position = 0
        Dim sr As New StreamReader(stream1)
        Response.Write(sr.ReadToEnd())
    End Sub

This works good for a single item. But I want to return a list from sql with many TOPICS and then populate a ListBox on main page. Not sure how to set up and pass the DataContract or do I even need that?
instear of single object you can return list/array too.
I have tried this using a datatable and loading it into a dictionary and then serializing it to a Json string, but I am not sure how to pass it back to my main page and also on my main page how do I put the data into a listbox using jquery?

   
<DataContract()> Public Class Doc
        <DataMember()> Public serial As Integer
        <DataMember()> Public topic As String
        <DataMember()> Public description As String
        <DataMember()> Public file As String
    End Class


private sub GetHelpList()
        da.SelectCommand = cmd
        Dim dc As New Doc
        cnn.Open()
        cmd.CommandText = "Select * from tbl_help  order by help_topic"
        da.Fill(dt)
        cnn.Close()

        Dim stream As New MemoryStream()
        Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
        Dim rows As New List(Of Dictionary(Of String, Object))
        Dim row As Dictionary(Of String, Object)

        For Each dr As DataRow In dt.Rows
            row = New Dictionary(Of String, Object)
            For Each col As DataColumn In dt.Columns
                row.Add(col.ColumnName, dr(col))
            Next
            rows.Add(row)
        Next
        Dim JsonString = serializer.Serialize(rows)

end sub

Even if I were to  build a list array how do i serialize it and pass it back to main page and then also using jquery load it into a listbox?
Thanks