How to update data using Ajax and webservice ?



Hi
Recently in one of my friend project, there was requirement to update data using ajax and javascript to avoid the postback. Then we did like this.

Step1: Create a webservice and write the code like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;

namespace Ajax_Test
{
///

/// Summary description for WebService2
///

[WebService(Namespace = “http://tempuri.org/”)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 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 WebService2 : System.Web.Services.WebService
{

[WebMethod]
public string UpdateEmpDetail(int Id,string EmpName)
{

using (SqlConnection con = new SqlConnection(“Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True;User Instance=True”))
{
using (SqlCommand cmd = new SqlCommand(“Update tblEmp set EmpName=@EmpName where Id=@Id”, con))
{
cmd.Parameters.AddWithValue(“@Id”, Id);
cmd.Parameters.AddWithValue(“@EmpName”, EmpName);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
string flag = “Record has been updated sucessfully”;
return flag;
}
}
}
}
}

Step2: Take one Ajax Script manager control and configure like this

<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebService2.asmx" />
</Services>
</asp:ScriptManager>

Step3: Write the Javacript code like this

<script type="text/javascript">
function updateEmpDB() {

var name = document.getElementById("<%=txtName.ClientID %>").value;

Ajax_Test.WebService2.UpdateEmpDetail(Id, name, OnComplete, OnTimeOut, onerror);
}
function OnComplete(args) {
document.getElementById('Label1').innerHTML = args;
document.getElementById("<%=txtName.ClientID %>").value = "";
}
function OnTimeOut(args) {
alert("Service call timed out.");
}
function OnError(args)
{
alert("Error calling service method.");
}
</script>

Now the code working code will be like this

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Ajax_Test.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
<title></title>

<script type="text/javascript">
function updateEmpDB() {

var name = document.getElementById("<%=txtName.ClientID %>").value;

Ajax_Test.WebService2.UpdateEmpDetail(Id, name, OnComplete, OnTimeOut, onerror);
}
function OnComplete(args) {
document.getElementById('Label1').innerHTML = args;
document.getElementById("<%=txtName.ClientID %>").value = "";
}
function OnTimeOut(args) {
alert("Service call timed out.");
}
function OnError(args)
{
alert("Error calling service method.");
}
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtName" runat="server" />
<br />
<input id="Button1" type="button" value="Update" onclick="return updateEmpDB();" />
<%–<asp:Button ID="btnClick" Text="Update" runat="server" OnClientClick="return updateEmpDB();" />–%>
<br />
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebService2.asmx" />
</Services>
</asp:ScriptManager>

<asp:Label ID="Label1" runat="server" Font-Bold="true" ForeColor="Red" Text=""></asp:Label>

<br />

</div>
</form>
</body>
</html>

Advertisement

6 thoughts on “How to update data using Ajax and webservice ?

  1. Chakradhar October 5, 2012 / 5:12 am

    Hi, Chandra

    Please let me know, Have you passed Id as static or dynamic?
    Regards,
    Chakradhar.

    • Chandra Dev October 5, 2012 / 7:16 am

      Thanks for pointing my mistake. While testing i was passing that Value as Var Id=1 in javascript. We can also make as dynamic on basis of our requirement.

  2. Bharath Kumar October 7, 2012 / 7:10 am

    Iam using this in my project but iam getting “Service call timed out.” always….. Please help me out…

  3. Bharath Kumar October 11, 2012 / 6:03 am

    K thanx buddy…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.