Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Bind Data to Datatable using JQuery or JSON
WhatsApp
Manish Pipaliya
May 02
2015
36.4
k
0
0
HTML File
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
head
id
=
"Head1"
runat
=
"server"
>
<
title
>
Asp.net Bind Data to Datatable using JQuery or JSON
</
title
>
<
script
type
=
"text/javascript"
src
=
"http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"
>
</
script
>
<
script
type
=
"text/javascript"
>
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json;
charset
=
utf
-8",
url: "tablebinding.aspx/BindDatatable",
data: "{}",
dataType: "json",
success: function (data) {
for (var
i
=
0
; i
<
data.d.length
; i++) {
$("#tbDetails").append("
<
tr
>
<
td
>
" + data.d[i].UserId + "
</
td
>
<
td
>
" + data.d[i].UserName + "
</
td
>
<
td
>
" + data.d[i].Location + "
</
td
>
</
tr
>
");
alert(JSON.stringify(data));
}
},
error: function (result) {
alert("Error");
}
});
});
</
script
>
<
style
type
=
"text/css"
>
table,th,td
{
border:1px solid black;
border-collapse:collapse;
}
</
style
>
</
head
>
<
body
>
<
form
id
=
"form1"
runat
=
"server"
>
<
table
id
=
"tbDetails"
cellpadding
=
"0"
cellspacing
=
"0"
>
<
thead
style
=
"background-color:#DC5807; color:White; font-weight:bold"
>
<
tr
style
=
"border:solid 1px #000000"
>
<
td
>
UserId
</
td
>
<
td
>
UserName
</
td
>
<
td
>
Location
</
td
>
<
td
>
Action
</
td
>
</
tr
>
</
thead
>
<
tbody
>
</
tbody
>
</
table
>
</
form
>
</
body
>
</
html
>
ASP.NET File
<WebMethod()> _
Public
Shared
Function
BindDatatable()
As
UserDetails()
Dim
dt
As
New
DataTable()
Dim
details
As
New
List(Of UserDetails)()
Using con
As
New
SqlConnection(
"DatabasePath"
)
Using cmd
As
New
SqlCommand(
"select * from Table_Name"
, con)
con.Open()
Dim
da
As
New
SqlDataAdapter(cmd)
da.Fill(dt)
For
Each
dtrow
As
DataRow
In
dt.Rows
Dim
user
As
New
UserDetails()
user.UserId = dtrow(
"UserId"
).ToString()
user.UserName = dtrow(
"UserName"
).ToString()
user.Location = dtrow(
"Location"
).ToString()
details.Add(user)
Next
End
Using
End
Using
Return
details.ToArray()
End
Function
Public
Class
UserDetails
Public
Property
UserId()
As
String
Get
Return
m_UserId
End
Get
Set
(
ByVal
value
As
String
)
m_UserId = Value
End
Set
End
Property
Private
m_UserId
As
String
Public
Property
UserName()
As
String
Get
Return
m_UserName
End
Get
Set
(
ByVal
value
As
String
)
m_UserName = Value
End
Set
End
Property
Private
m_UserName
As
String
Public
Property
Location()
As
String
Get
Return
m_Location
End
Get
Set
(
ByVal
value
As
String
)
m_Location = Value
End
Set
End
Property
Private
m_Location
As
String
End
Class
Bind Data to Datatable
Bind Data using JQuery or JSON
Up Next
Bind Data to Datatable using JQuery or JSON