ASP.Net jQuery Ajax Error: Unknown web method. Parameter name: methodName

Last Reply 23 days ago By arunkurmi

Posted 26 days ago

Dear sir,

I have small aspx form. But it doesn't insert in to table i got error my jquery alert "Error Occured, Try Again".

How to insert data into database using JQuery

Kindly help me because i am an new comer in JQuery.

        <script type="text/javascript">
            $(function () {
                $('#Button1').click(function () {
                    var name = $('#TextBox1').val();
                    var email = $('#TextBox2').val();
                    var designation = $('#TextBox3').val();
                    var city = $('#TextBox4').val();
                    if (name != '' && email != '' && designation != '' && city != '') {
                        $.ajax
                            ({
                                type: 'POST',
                                url: 'Home.aspx/insertdata',
                                async: false,
                                data: "{'name':'" + name + "','email':'" + email + "','designation':'" + designation + "','city':'" + city + "'}",
                                contentType: 'application/json; charset =utf-8',
                                success: function (data) {
                                    var obj = data.d;
                                    if (obj == 'true') {
                                        $('#TextBox1').val('');
                                        $('#TextBox2').val('');
                                        $('#TextBox3').val('');
                                        $('#TextBox4').val('');
                                        alert("Data Saved Successfully");
                                    }
                                },
                                error: function (result) {
                                    alert("Error Occured, Try Again");
                                }
                            });
                    } else {
                        alert("Pleae Fill all the Fields");
                        return false;
                    }
                })
            });
        </script>  

 

            <div>  
                <table style="width:100%;">  
                    <tr>  
                        <td class="auto-style1"> </td>  
                        <td> </td>  
                        <td> </td>  
                    </tr>  
                    <tr>  
                        <td class="auto-style1">Name </td>  
                        <td>  
                            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
                        </td>  
                        <td> </td>  
                    </tr>  
                    <tr>  
                        <td class="auto-style1">Email </td>  
                        <td>  
                            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>  
                        </td>  
                        <td> </td>  
                    </tr>  
                    <tr>  
                        <td class="auto-style1">Designation</td>  
                        <td>  
                            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>  
                        </td>  
                        <td> </td>  
                    </tr>  
                    <tr>  
                        <td class="auto-style1">City</td>  
                        <td>  
                            <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>  
                        </td>  
                        <td> </td>  
                    </tr>  
                    <tr>  
                        <td class="auto-style1"> </td>  
                        <td> </td>  
                        <td> </td>  
                    </tr>  
                    <tr>  
                        <td class="auto-style1"> </td>  
                        <td>  
                            <%--<asp:Button ID="Button1" runat="server" Text="Submit"  /> --%>
                            <input id="Button1" value="Submit" type="button" onclick="insertdata()" /> 
                        </td>  
                        <td> </td>  
                    </tr>  
                </table>
            </div>

 

        public static string insertdata(string name, string email, string designation, string city)
        {
            string msg = "";
            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Insertdatausingjquery\IDITODBUJQUERY\IDITODBUJQUERY\App_Data\IDITODBUJ.mdf;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("insert into tbemployee values(@name, @email,@designation,@city)", con);
            cmd.Parameters.AddWithValue("@name", name);
            cmd.Parameters.AddWithValue("@email", email);
            cmd.Parameters.AddWithValue("@designation", designation);
            cmd.Parameters.AddWithValue("@city", city);
            con.Open();
            int i = cmd.ExecuteNonQuery();
            if (i == 1)
            {
                msg = "true";
            }
            else
            {
                msg = "false";
            }

            return msg;
        }

 

Posted 23 days ago

Hi rajeesh,

Your code is working correctly. You have missed the [System.Web.Services.WebMethod] attribute.

[System.Web.Services.WebMethod]
public static string insertdata(string name, string email, string designation, string city)
{
    string msg = "";
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
    SqlCommand cmd = new SqlCommand("insert into tbemployee values(@name, @email,@designation,@city)", con);
    cmd.Parameters.AddWithValue("@name", name);
    cmd.Parameters.AddWithValue("@email", email);
    cmd.Parameters.AddWithValue("@designation", designation);
    cmd.Parameters.AddWithValue("@city", city);
    con.Open();
    int i = cmd.ExecuteNonQuery();
    if (i == 1)
    {
        msg = "true";
    }
    else
    {
        msg = "false";
    }

    return msg;

}