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.
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.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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(functio n () {
var u
u = "Help-Support.aspx?f=1&tim estamp=" + (new Date()).getTime();
$('#Label170').text('Loadi ng...');
$.getJSON(u, function (data) {
$('#Label170').text(data[' topic']);
$('#tDesc').text(data['des cription'] );
});
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.ReadToEn d())
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?
My script to fill a label and textbox...
$(document).ready(function
$('#bAddDr').click(functio
var u
u = "Help-Support.aspx?f=1&tim
$('#Label170').text('Loadi
$.getJSON(u, function (data) {
$('#Label170').text(data['
$('#tDesc').text(data['des
});
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
ser.WriteObject(stream1, o)
stream1.Position = 0
Dim sr As New StreamReader(stream1)
Response.Write(sr.ReadToEn
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.
ASKER
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.Serializ ation.Java ScriptSeri alizer = New System.Web.Script.Serializ ation.Java ScriptSeri alizer()
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
<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.Serializ
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
Please go through following articals, they explain step by step:
http://www.ashishblog.com/blog/calling-database-using-jquery-ajax-and-asp-net/
http://code.google.com/p/m-jq-projects/wiki/ajaxPopulate_jQuery
http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/351614/populate-an-asp-list-box-based-on-selection-of-a-previous-list-box-using-ajax
http://www.dotnetcurry.com/ShowArticle.aspx?ID=454
Thanks