Hello experts,
I want to create a web base application where me have to perform crud operation using
ASP.net Web Service, JQuery and JSON. But I don't know how to do it.
If anybody kindly send me some sample code and instruction, it will be helpful for me.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
public WebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString);
int rowsInserted = 0;
[WebMethod]
public string Insert(int id, string name, string date, float salary)
{
string x = "";
SqlCommand cmd = new SqlCommand("Ins_Upd_tbl_Tst1 " + id + ",'" + name + "','" + date + "'," + salary,cn);
cmd.CommandType = CommandType.Text;
cn.Open();
rowsInserted = cmd.ExecuteNonQuery();
cn.Close();
if (rowsInserted > 0)
{
x = string.Format("Thank you, {0} number of rows inserted!", rowsInserted);
}
else
{
x=string.Format("Failed, to Insert Data!",0);
}
return x;
but when I click on Submit button It doesn't save any record. I checked my web service method. It can save data. I don't know where is my fault. Please Help me.
If you are using MasterPage then there is a small problem in your javascript code
Replace
var id = $('#HiddenField1').val();
var name = $('#_txtname').val();
var date = Date();
var salary = $('#_salary').val();
With
var id = $("input[type='hidden'][id$='HiddenField1']").val();
var name = $("input[type='textbox'][id$='_txtname']").val();
var date = Date();
var salary = $("input[type='textbox'][id$='_salary']'").val();
ELSE IF NOT USING MASTER PAGE, then place a breakpoint in your WebService and see if you are gettin the corrext values as your arguments
At last solved my problem. The problem was at date(). This date was not supported by SQL Server. Finally I Send date to database from Textbox and it worked.
Marked as answer by mhdcpds on Jul 02, 2011 05:11 AM
mhdcpds
Member
30 Points
28 Posts
CRUD Application Using Asp.net Web Service, JQurey, JSON
Jun 27, 2011 07:39 AM|LINK
Hello experts,
I want to create a web base application where me have to perform crud operation using
ASP.net Web Service, JQuery and JSON. But I don't know how to do it.
If anybody kindly send me some sample code and instruction, it will be helpful for me.
my email address is: mojamcpds@hotmail.com
Thanks in advance
Mojam
web
hiren.sharma
Participant
1460 Points
311 Posts
Re: CRUD Application Using Asp.net Web Service, JQurey, JSON
Jun 27, 2011 08:19 AM|LINK
Well your requirement is no big deal, there are thousands of code samples available on the internet.
Like the Encosoa or Code project
http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/
http://www.codeproject.com/KB/webservices/JsonWebServiceJQuery.aspx
HOPE IT HELPS
HAPPY CODING
web
Try out My Blog | Visit My Website
asteranup
All-Star
30184 Points
4906 Posts
Re: CRUD Application Using Asp.net Web Service, JQurey, JSON
Jun 27, 2011 08:22 AM|LINK
Hi,
Check few post below-
http://forums.asp.net/p/1678123/4406093.aspx/1?Re+Send+deserialize+JSON+data+on+server+side+method+in+asp+net
http://forums.asp.net/p/1684175/4433213.aspx/1?Re+hi+all+i+have+a+question+
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
evanorue
Contributor
4839 Points
1306 Posts
Re: CRUD Application Using Asp.net Web Service, JQurey, JSON
Jun 27, 2011 02:26 PM|LINK
hi friend,
maybe that can help you!
Good Luck!
evanorue@gmail.com |Blog
mhdcpds
Member
30 Points
28 Posts
Re: CRUD Application Using Asp.net Web Service, JQurey, JSON
Jun 30, 2011 09:28 AM|LINK
I crated my code as follow :
Web Service Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
public WebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString);
int rowsInserted = 0;
[WebMethod]
public string Insert(int id, string name, string date, float salary)
{
string x = "";
SqlCommand cmd = new SqlCommand("Ins_Upd_tbl_Tst1 " + id + ",'" + name + "','" + date + "'," + salary,cn);
cmd.CommandType = CommandType.Text;
cn.Open();
rowsInserted = cmd.ExecuteNonQuery();
cn.Close();
if (rowsInserted > 0)
{
x = string.Format("Thank you, {0} number of rows inserted!", rowsInserted);
}
else
{
x=string.Format("Failed, to Insert Data!",0);
}
return x;
}
}
And my aspx page I wrote following code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>JASON Insert/Update</title>
<script src="jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
function CallService() {
// Creating variables to hold data from textboxes
var id = $('#HiddenField1').val();
var name = $('#_txtname').val();
var date = Date();
var salary = $('#_salary').val();
$.ajax({
type: "POST",
url: "WebService.asmx/Insert",
data: "{ 'id': '" + id + "','name': '" + name + "', 'date': '" + date + "','salary':'" + salary + "'}",
contentType: "application/json",
async: false,
success: function(data) {
$('#message').text(data.d);
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Name: <asp:TextBox ID="_txtname" runat="server"></asp:TextBox><br />
Salry: <asp:TextBox ID="_salary" runat="server"></asp:TextBox><br />
<asp:HiddenField ID="HiddenField1" runat="server" Value="0" />
<br />
<input id="_btnSubmit" type="button" value="Submit" onclick="CallService();" />
<span id="message">
</span>
</div>
</form>
</body>
</html>
but when I click on Submit button It doesn't save any record. I checked my web service method. It can save data. I don't know where is my fault. Please Help me.
Thanks in advance
Mojam
hiren.sharma
Participant
1460 Points
311 Posts
Re: CRUD Application Using Asp.net Web Service, JQurey, JSON
Jun 30, 2011 06:23 PM|LINK
If you are using MasterPage then there is a small problem in your javascript code
Replace
var id = $('#HiddenField1').val();
var name = $('#_txtname').val();
var date = Date();
var salary = $('#_salary').val();
With
var id = $("input[type='hidden'][id$='HiddenField1']").val();
var name = $("input[type='textbox'][id$='_txtname']").val();
var date = Date();
var salary = $("input[type='textbox'][id$='_salary']'").val();
ELSE IF NOT USING MASTER PAGE, then place a breakpoint in your WebService and see if you are gettin the corrext values as your arguments
Try out My Blog | Visit My Website
mhdcpds
Member
30 Points
28 Posts
Re: CRUD Application Using Asp.net Web Service, JQurey, JSON
Jul 02, 2011 05:11 AM|LINK
At last solved my problem. The problem was at date(). This date was not supported by SQL Server. Finally I Send date to database from Textbox and it worked.
beginner101
Member
4 Points
4 Posts
Re: CRUD Application Using Asp.net Web Service, JQurey, JSON
Mar 30, 2012 02:25 PM|LINK
Never concatenate string like that because of the SQL-injection attacks! Use parameters instead, e.g.:
SqlCommand cmd = new SqlCommand("Ins_Upd_tbl_Tst1 @id",cn);
cmd.Parameters.Add(new SqlParameter("@id", id));