Loading ...

How To Insert Data Using Web Service | CodeAsp.Net

How To Insert Data Using Web Service

0 (0votes)

Web Service –
Web Service is a huge thing in ASP.NET. Using web Service, We can create robust web applications. We can create Web service’s methods and that function can be accessible to the rest of the world. ASP.NET provides easy way to develop Web services. With the help of Web services we can share data across different platforms. In this blog I will explain only how to insert data in
database using Web Service.Here is more info about Web service.

This is my aspx code –

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
    <form id="form1" runat="server">
                <td>Name :</td>
                <td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
                <td>Age :</td>
                <td><asp:TextBox ID="txtAge" runat="server"></asp:TextBox></td>
                <td><input type="button" id="btnSubmit" value="Submit" /></td>
    <script type="text/javascript">
    function InsertData()
        var txtNameValue = $('#<%=txtName.ClientID %>').val();
        var txtAgeValue = $('#<%=txtAge.ClientID %>').val();
        var path = 'myWebService.asmx/InsertData'
                   type: 'POST',
                   url: path,
                   data: "{'name':'" + txtNameValue + "','age':'"+ txtAgeValue +"'}",
                   dataType: 'json',
                   contentType: "application/json; charset=utf-8",
                   success: function(response)
                       if (response != null && response.d != null)
                           if (response.d)




In above aspx code there is two textbox, one button, and InsertData () method in script tag. In InsertData () methods, I am calling Web service’s method and passing two parameters(both textbox’s value).

Web service Code –

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

namespace WebApplication1
    /// <summary>
    /// Summary description for myWebService
    /// </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. 
    public class myWebService : System.Web.Services.WebService

        public bool InsertData(string name, string age)
            string strCommandText = "User_Insert_Data";

            SqlConnection con = new SqlConnection("ConnectionString");
            SqlCommand cmd = new SqlCommand(strCommandText, con);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlParameter paramName = new SqlParameter("@Name", SqlDbType.NVarChar);
            paramName.Value = name;

            SqlParameter paramAge = new SqlParameter("@Age", SqlDbType.NVarChar);
            paramAge.Value = age;


            int i = 0;
            i = cmd.ExecuteNonQuery();
                return true;
                return false;

There is Web service’s InsertData () method and in this method I am inserting data in database.

Happy CodingSmile

Comments (no comments yet)

  • :*
  • :*
  • :

Top Posts