i am trying to use juqery -ajax with web servce in asp.net to insert data in database ..
i came across the blog that show the example of same ..
http://devtechie.com/post/2011/05/26/Insert-Data-using-JSON-ASPNET-Web-services-and-jQuery.aspx
but nothing is happening when i click submit button . i want to know my errors ..
here is my code:
default2.aspx :
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript" src="jquery-latest.min.js"></script>
<style type="text/css">
.addbutton
{
color:Red;
}
</style>
<script type="text/javascript">
function CallService() {
// Creating variables to hold data from textboxes
var name = $('#_txtname').val();
var email = $('#_email').val();
var phone = $('#_phone').val();
var addr = $('#_addr').val();
$.ajax({
type: "POST",
url: "MultipleUpdateWebService.asmx/ReceiveUpdatesWebservice",
data: "{ 'name': '" + name + "','email': '" + email + "', 'phone': '" + phone + "','address':'" + addr + "'}",
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 />
Email: <asp:TextBox ID="_email" runat="server"></asp:TextBox><br />
Phone: <asp:TextBox ID="_phone" runat="server"></asp:TextBox><br />
Address: <asp:TextBox ID="_addr" runat="server"></asp:TextBox><br />
<input id="_btnSubmit" type="button" value="Submit" class="addbutton" onclick="CallService();" />
<span id="message">
</span>
</div>
</form>
</body>
</html>
MultipleUpdateWebService.asmx.cs:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for MultipleUpdateWebService
/// </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 MultipleUpdateWebService : System.Web.Services.WebService
{
// getting connection string
string conStr = WebConfigurationManager.ConnectionStrings["eshopConnectionString"].ConnectionString;
int rowsInserted = 0;
[WebMethod]
public string ReceiveUpdatesWebservice(string name, string email, string phone, string address)
{
// Creating Sql Connection
using (SqlConnection conn = new SqlConnection(conStr))
{
// Creating insert statement
string sql = string.Format(@"INSERT INTO sampleinfotable
([Name]
,[Email]
,[Phone]
,[Address])
VALUES
('{0}'
,'{1}'
,'{2}'
,'{3}')", name, email, phone, address);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
conn.Open();
rowsInserted = cmd.ExecuteNonQuery();
conn.Close();
cmd = null;
}
return string.Format("Thank you, {0} number of rows inserted!", rowsInserted);
}
}