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.
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