Click here to Skip to main content
15,921,884 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A simple example needed to me.I want to gain some idea from that.plz any one any idea or simple code.
i want to throug ajax i delete a record from data base or another example to add a record through ajax my code are below but it not work.
its my backend method
///
C#
[WebMethod]
   public static void DeleteRec(string DeleteRecord1)
   {
       SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["strcon"].ConnectionString);
       if (con.State == ConnectionState.Closed)
       {
           con.Open();
       }
       SqlCommand cmd = new SqlCommand("delete from tbl_Login where userID=2", con);
       cmd.ExecuteNonQuery();


       return;

   }

///
its front end code
//
JavaScript
<script>
        function DeleteRec(DeleteRecord1) {
            $.ajax({
                type: "POST",
                url: "frmNewRegisteration.aspx/DeleteRec",
                data: '{DeleteRecord1: ' + DeleteRecord1 + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function () {
                    alert("delete succesful");
                },
                error: function (response) {
                    alert("unable to delete");
                }
            });
        };

        </script>
Posted
Updated 14-Apr-14 23:57pm
v3
Comments
Sanket Saxena 15-Apr-14 6:10am    
post your function DeleteRec calling code
Mian Sahib Jan 15-Apr-14 6:49am    
<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="DeleteRec()" />
Sanket Saxena 15-Apr-14 7:16am    
Dear your method is "DeleteRec(DeleteRecord1)" and you are passing "data: '{DeleteRecord1: ' + DeleteRecord1 + '}'"

You called OnClick="DeleteRec()", So you have to check what is the value in DeleteRecord1 is seems null.

Check what is the value in DeleteRecord1 is seems null in

data: '{DeleteRecord1: ' + DeleteRecord1 + '}'

Also i have noticed that in the backend method you are getting DeleteRecord1 from the front end and in the delete query you are passing 2 as a parameter. Make sure you can use DeleteRecord1 (not null).
 
Share this answer
 
 
Share this answer
 
code might be looking good but the page your calling might have the HTML content so remove the whole html in .aspx page(the page which you are calling) keep only the <@page directive in that and remaining all u remove
 
Share this answer
 

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