Loading ...

How To Insert Data Using Web Service | CodeAsp.Net

How To Insert Data Using Web Service

 /5
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">
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>Name :</td>
                <td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Age :</td>
                <td><asp:TextBox ID="txtAge" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td><input type="button" id="btnSubmit" value="Submit" /></td>
            </tr>
        </table>
    </div>
    <script type="text/javascript">
        
    
    $('#btnSubmit').click(function(e)
    {
       
      InsertData();
      e.preventDefault();
    });
    
    function InsertData()
    {
        var txtNameValue = $('#<%=txtName.ClientID %>').val();
        var txtAgeValue = $('#<%=txtAge.ClientID %>').val();
        
        var path = 'myWebService.asmx/InsertData'
        $.ajax
               ({
                   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)
                           {
                             alert('Success')

                           }
                           else
                           {
                               alert('Failure')
                           }

                       }
                   }

               }); 
    }
    
    </script>
    </form>
    
</body>
</html>

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)]
    [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 myWebService : System.Web.Services.WebService
    {

        [WebMethod]
        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;
            cmd.Parameters.Add(paramName);

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

            con.Open();

            int i = 0;
            i = cmd.ExecuteNonQuery();
            con.Close();
            if(i>0)
            {
                return true;
            }
            else
            {
                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