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
Get Data Using Web Services and JSON
WhatsApp
Satyajeet Kumar
Apr 04
2016
2.4
k
0
0
<div>
<input id=
"Button1"
type=
"button"
value=
"Search"
/>
<div id =
"results"
></div>
</div>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"
></script>
<script type=
"text/javascript"
>
$(
"#Button1"
).live(
"click"
, function () {
$.ajax({
type:
"POST"
,
url:
'myServices.asmx/getITEM'
,
dataType:
"json"
,
success: function (response) {
console.log(response);
var html =
""
;
$.each(response, function () {
html +=
"<span>COL1: "
+
this
.COL1 +
" COL2: "
+
this
.COL2 +
"</span><br />"
;
});
$(
"#results"
).html(html ==
""
?
"No results"
: html);
},
error: function (response) {
console.log(response);
}
});
});
</script>
[WebMethod]
public
void
getITEM()
{
List<object> ITEM =
new
List<object>();
using
(SqlConnection conn =
new
SqlConnection())
{
conn.ConnectionString =
"Data source=xxxxx; initial catalog=xxxxx; user id=xxxx; password=xxxxx;"
;
using
(SqlCommand cmd =
new
SqlCommand())
{
cmd.CommandText =
"SELECT COL1, COL2, COL3, COL4 FROM TABLE"
;
cmd.Connection = conn;
conn.Open();
using
(SqlDataReader sdr = cmd.ExecuteReader())
{
while
(sdr.Read())
{
ITEM.Add(
new
{
COL1 = sdr[
"COL1"
],
COL2 = sdr[
"COL2"
],
COL3 = sdr[
"COL3"
],
COL4 = sdr[
"COL4"
]
});
}
}
conn.Close();
}
System.Web.Script.Serialization.JavaScriptSerializer JS =
new
JavaScriptSerializer();
Context.Response.Write(JS.Serialize(ITEM));
}
}
JSON
Web Services
Up Next
Get Data Using Web Services and JSON