views:

416

answers:

1

Hello all, I have tried many ways of executing this but its not happening .. here is the deal .. I have data displayed in table rows, every row has its id representing id of the person I wanna delete. I use jquery to collect this id from row where my button is located, here is the code how I do that, its working I'm just writing so you can get an insight of what I'm trying to accomplish :

var customer_id = $(this).parents("tr").attr('id');

When I test it using alert it works, here is the difficult part I'm simply stuck I have file called Delete.aspx here is its content

<%

string p = Request["value"]; int pInt = Int32.Parse(p);

var dataContext = new CustomersDataContext();
var customer = from m in dataContext.Customers
               where m.id == pInt
               select m;
dataContext.Customers.DeleteAllOnSubmit(customer);
dataContext.SubmitChanges();
%>

Now I'm trying to send the to the Delete.aspx values to delete person with particular id, and it works by typing Delete.aspx?value=7 in the browser, it deletes person with id 7. Now here is where I'm really really stuck. From Default.aspx i'm trying to reach Delete.aspx and pass the person id using jquery like this :

$(".btn-delete").click(function() {
                 var answer = confirm("If you press OK, this customer will be deleted?")
                 var customer_id = $(this).parents("tr").attr('id');
                 if (answer) {


                      $.ajax({
                     type: "POST",
                     url: "Delete.aspx",
                     data: "{value: '" + customer_id + "'}",
                     contentType: "application/json; charset=utf-8",
                     dataType: "json",
                     success: function(msg) {
                     AjaxSucceeded(msg);
                     },
                     error: AjaxFailed

                     });

                     $(this).parents("tr").animate({ backgroundColor: "#fbc7c7" }, "fast")
             .animate({ opacity: "hide" }, "slow")
                     return false;
                 }
                 else {
                     alert("Customer has not been deleted!")
                 }


             });

             function AjaxSucceeded(result) {
                 alert(result.d);
             }
             function AjaxFailed(result) {
                 alert(result.status + ' ' + result.statusText);
             }

         });

So now the result, When I click the button and confirm deletion I get "500 Internal server error", is this fixable or is there anymore other simple way of doing the same thing ?

Thank you


I've modified code .. but still I need some help .. I feel I'm so close so at least point me to right direction ..

here is my Delete.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Second_Question
{
    public partial class Delete : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            string p = Request["value"];
            int pInt = Int32.Parse(p);

            var dataContext = new CustomersDataContext();
            var customer = from m in dataContext.Customers
                           where m.id == pInt
                           select m;
            dataContext.Customers.DeleteAllOnSubmit(customer);
            dataContext.SubmitChanges();


        }
    }
}

here is my Delete.aspx

<%

%>

any ideas ? thank you

+1  A: 

The "500 Internal Server Error" usually occurs because you have some server-side code that either didn't compile correctly, or threw an unhandled exception.

I would suggest you might want to try a couple of changes:

Move your delete routine to a specific method that is decorated with the [WebMethod] attribute. Ensure that your code includes System.Web.Services.

Have your jQuery ajax call include "/Delete.aspx/MyDeleteMethod" instead of the entire page.

Have you thought about the security for this application? What happens if someone catches your customer_id and changes it on-the-fly with Firebug?

EDIT: Ok, here's what I would suggest for the server-side code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services

namespace Second_Question
{
    public partial class Delete : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static void DeleteCustomer(int CustID)
        {

            var dataContext = new CustomersDataContext();
            var customer = from m in dataContext.Customers
                           where m.id == CustID
                           select m;
            dataContext.Customers.DeleteAllOnSubmit(customer);
            dataContext.SubmitChanges();


        }
    }
}

And my jQuery would look like this:

$.ajax({
      type: "POST",
      url: "Delete.aspx/DeleteCustomer",
      data: "{CustID: " + parseInt(customer_id) + "}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
                     AjaxSucceeded(msg);
               },
      error: AjaxFailed
 });
Phil.Wheeler
can you please elaborate on this "Have your jQuery ajax call include "/Delete.aspx/MyDeleteMethod" instead of the entire page." ? thank you
c0mrade
The example I've added shows jQuery making a call to a specific, purpose-built web method inside Delete.aspx rather than calling the page directly. This way, you can separate your concerns a little more logically.
Phil.Wheeler
Great, it works :) Thank you
c0mrade
Phil.Wheeler
I did thank you again :D
c0mrade