Recent Posts

Tuesday, September 22, 2009

How to use Json objects and Jqeury in Asp.net? Part -1

This is a very cruital article for all programmers wanting to learn jquery and apply it to their Asp .net web applications. This jquery, json and asp .net 2.0 tutorial explains step by step on how to retrieve the data from the database and render it back as json objects and display it on the web page using jquery.
When I was learning jquery, I actually was looking for similar tutorial where it can be explained using .net platform but i couldn't find exact solution and i spent so much time to get to this. Therefore i am writing this tutorial for all the .net programmers who want to learn jquery and apply it to their .Net web applications.

This tutorial assumes that the reader know basics of jquery and json objects and able to program dynamic websites in .net and knows the concept of three - tier architecture. I am not providing the full source code for this but i will try to explain the best i can with the help of code screenshots. You can definately ask questions and write comments and i will try to answer them as i get time off my work.

Ok lets begin here now:

In this tutorial I am using a table called Carriers that has carriers_code and Description Field containing 30 records and i will just show you how to retrieve these records and display in the grid with paging. I am pretty sure if you can retrieve and display records then you can easily insert, update or delete records.

Ok now lets see database layer code below ( Now if you don't know about Data access layer, business layer and presentation layer then you can learh how to develop three tier web application in .net )

Below function gets the count of the number of records:

Public Function CountTAirlinesJQ() As Integer
Dim count As Integer
Dim strSql As String
Dim dr As OleDb.OleDbDataReader

strSql = "Select Count(*) from Carriers"
db = New DBHelper("SFIA")

dr = db.ExecuteAndGetRowOLEDB(strSql)
If dr.Read() Then
count = dr(0)
Else
dr.Close()
Return 0
End If
dr.Close()

Return count
End Function


Below function retrieves the data and return as the list of TariffAirlines object.

Public Function GetTAirlinesJQ(ByVal PageSize As Integer, _
ByVal StartAt As Integer, _
ByRef ErrMsg As String) As List(Of TarrifAirlines)

Dim ds As DataSet
Dim Airs As New List(Of TarrifAirlines)
Dim strSql As String
Dim Top As Integer

Try
If PageSize > 0 Then
Top = PageSize + StartAt
strSql = "Select Top " + Top.ToString + " carriers_code,Description from Carriers"
Else
strSql = "Select carriers_code,Description from Carriers "
End If

db = New DBHelper("SFIA")
ds = New DataSet

ds = db.getDataset(strSql)

Dim dt As DataTable = ds.Tables(0)
Dim row As DataRow

Dim Airline As TarrifAirlines

Dim i As Integer
For Each row In dt.Rows
Airline = New TarrifAirlines
Airline.AirlineCode = row.Item("carriers_code").ToString()
Airline.AirlineName = row.Item("Description").ToString()

' PAGING here...............................>
'' frist get select top 10 from the table
' net time it will be top 20 and so you will pick up last 10
' because you will start at 10 now...

If i >= StartAt Then
Airs.Add(Airline)
End If
i = i + 1
Next

Return Airs
Catch ex As Exception
ErrMsg = ex.ToString()
Return Nothing
End Try

End Function


Similarly following business layer function return values to the presentation layer:

This returns the count from AirlinesDB containing above code

Public Function CountAirlinesJQ() As Integer

Dim db As New AirlinesDB
Return db.CountTAirlinesJQ()
End Function


Below function returns the list of TAirlines object retrieving from the AirlinesDB to the presentation layer:

Public Function GetAirlinesJQ(ByVal PageSize As Integer, _
ByVal StartAt As Integer, _
ByRef ErrMsg As String) As List(Of TarrifAirlines)

Dim db As New AirlinesDB
Return db.GetTAirlinesJQ(PageSize, StartAt, ErrMsg)

End Function


Ok now we have retrieved the data from the database and returned the count of the tables and list of Tairlines object. This tutorial will get longer therefore i am dividing this to different parts. Ok now Lets go to Second part of learn how to use json objects and jquery in Asp.net? Part 2.

Related Posts by Categories




No comments:

Post a Comment