Saturday, 4 April 2015

Insert Data through html button using webservice

No comments
First Create calculation.asmx web service


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

 [System.Web.Script.Services.ScriptService]
public class calculation : System.Web.Services.WebService {

    public calculation ()
   {     
    }

    [WebMethod]
    public string Cal(int a, int b)
    {       
        SqlConnection con = new SqlConnection("Connection");
        con.Open();
        string str =  "insert into t1(id,xol1) values('"+a+"','"+b+"')";
        SqlCommand cmd = new SqlCommand(str, con);             
        cmd.ExecuteNonQuery();
        con.Close();
        return "Inserted Sucessfully";
    }   
}







.ASPX

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" language="javascript">
        function callService(f) {
            document.getElementById("c").innerHTML = "";           
            calculation.Cal(
                        parseInt(f.elements["a"].value),
                        parseInt(f.elements["b"].value),
                        callComplete,
                        callError);
                        }
                        function callComplete(result) {
                            document.getElementById("c").innerHTML = result;
                        }
                        function callError(result) {
                            document.getElementById("c").innerHTML = "error";
                        }
                    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference Path="~/calculation.asmx" />
            </Services>
        </asp:ScriptManager>
     
        <asp:TextBox ID="a" runat="server"></asp:TextBox>     
        <asp:Label ID="Label1" runat="server"> / </asp:Label>
        <asp:TextBox ID="b" runat="server"></asp:TextBox>
        <input type="button" value="Divide Numbers" onclick="callService(this.form);" />
        <asp:Button ID="Button1" runat="server" Text="=" OnClientClick="return callService(this.form);"/>
        <asp:Label ID="c" runat="server"></asp:Label>   
    </div>
    </form>
</body>
</html>