Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
below is the code,for which i am unable to insert data,can any one help me out.

script :-
C#
<script type="text/javascript">
          $(function () {
              $('#btnSubmit').click(function () {
                  $.ajax({
                      type: "POST",
                      contentType: "application/json; charset=utf-8",
                      url: "WorkingWithAjax.aspx/InsertData",
                      dataType: "json",
                      data: "{'FirstName':'" + $('#txtFirstName').val() + "','LastName':'" + $(
                          '#txtLastName').val() + "','City':'" + $('#txtCity').val() + "','EmailID':'" + $('#txtEmailID')
                          .val() + "'}",
                      success: function (data) {
                          var obj = data.d;
                          if (obj == 'true') {
                              $('#txtFirstName').val('');
                              $('#txtLastName').val('');
                              $('#txtCity').val('');
                              $('#txtEmailID').val('');
                              $('#lblmsg').html('Data Inserted Successfully');
                          }
                      },
                      error: function (result) {
                          alert(result);
                      }
                  });
              });
          });
    </script>



Backend :-

C#
[WebMethod]
        public static string InsertData(string FirstName, string LastName, string City, string EmailId)
        {
            string retMessage = string.Empty;
            string ConnectionString = "server=.\\SQLSERVER;uid=sa;pwd=Acer@123;database=data";
            using (SqlConnection con = new SqlConnection(ConnectionString))
            {
                string query = "INSERT INTO sample(FirstName,LastName,City,EmailId) values(@FirstName,@LastName,@City,@EmailId)";
                using (SqlCommand cmd = new SqlCommand(query, con))
                {
                        con.Open();
                    cmd.Parameters.Add("@FirstName", FirstName);
                    cmd.Parameters.Add("@LastName", LastName);
                    cmd.Parameters.Add("@City", City);
                    cmd.Parameters.Add("@EmailID", EmailId);

                    int AffectedRow = cmd.ExecuteNonQuery();
                    if (AffectedRow == 1)
                    {
                        retMessage = "true";
                    }
                    else
                    {
                        retMessage = "false";
                    }

                    return retMessage;

                }
            }
        }
Posted
Updated 28-May-15 2:59am
v3
Comments
ZurdoDev 28-May-15 8:55am    
Why can't you insert data?
Haressh 28-May-15 8:57am    
the back end method is not getting called
ZurdoDev 28-May-15 9:08am    
Are you sure? Did you put a breakpoint in the c#? If the back end is not getting called that likely means your url is wrong.

Either way, implement the full error function
error: function (jqXHR, textStatus, errorThrown)

and especially analyze jqXHR.responseText
F-ES Sitecore 28-May-15 9:11am    
Use the browser's dev tools (f12) or Fiddler to examine the network calls, that often reveals the issue.
[no name] 29-May-15 4:47am    
Please let us know what is the error messages?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900