Imports Newtonsoft.Json
Imports System.Data.SqlClient
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Session("ConnString") = "Server=10.1.60.34\PEAK10SQL; Database=V014U07NUI; user id=V014U07NUI_SMTOnline_2; password=xxxxxxxxx; Connection Timeout=60;"
Dim objConn As New SqlConnection(Session("ConnString"))
Dim objCommand As New SqlCommand
Dim objDa As New SqlDataAdapter
Dim objDs As New DataSet
Dim sSQL As String = ""
sSQL = sSQL & " Select id_user,userid From users where id_user < 10"
objCommand.CommandText = sSQL
objCommand.Connection = objConn
objDa.SelectCommand = objCommand
objDa.Fill(objDs, "xUsers") 'this will be the JSon Object Name: {"xUsers":[{"id_user":1,"userid":"HKruse@smttesting.com"},{"id_user":2,"userid":"SamSampler@SMTtesting.com"},
objConn.Close()
'this will convert the DataSet to Json…
Dim jSon As String = JsonConvert.SerializeObject(objDs)
'this will convert the Json back to an object described in our UsersWrapper Class
Dim obj As UsersWrapper = JsonConvert.DeserializeObject(Of UsersWrapper)(jSon)
Dim i As Integer
For i = 0 To obj.xUsers.Count - 1
Response.Write("Uid=" & obj.xUsers(i).UserID & "<br>")
Next
End Sub
Public Class UsersWrapper
'This name: xUsers, MUST match the json object name created in sql query like this: objDa.Fill(objDs, "xUsers")
Public xUsers As List(Of OneUser)
End Class
'This class describes the xUsers fields
Public Class OneUser
Public id_user As Integer
Public UserID As String
End Class