0

enter image description here This Ajax method not calling the webmethod and not update the data to database. I also try it without and with JSON.Strigify but it not work properly. I am using the gridview in that O have take the textboxes and performing the update task on it.
My Javascript Code is -

$("body").on("click", "[id*=GridView1] .Update", function () {
                    var row = $(this).closest("tr");
                    $("td", row).each(function () {
                        if ($(this).find("input").length > 0) {
                            var span = $(this).find("span");
                            var input = $(this).find("input");
                            span.html(input.val());
                            span.show();
                            input.hide();
                        }
                    });
                    row.find(".Edit").show();
                    row.find(".Delete").show();
                    row.find(".Cancel").hide();
                    $(this).hide();
                    var id = row.find(".ID").find("span").html();
                    var ledger = row.find(".Ledger").find("span").html();
                    var datafld = row.find(".Datafld").find("span").html();
                    var aDatafldm = row.find(".ADatafld").find("span").html();
                    var lType = row.find(".LType").find("span").html();
                    var cType = row.find(".CType").find("span").html();
                    var lAcNo = row.find(".LAcNo").find("span").html();
                    var type = row.find(".Type").find("span").html();
                    var link = row.find(".Link").find("span").html();
                    var tPer = row.find(".TPer").find("span").html();
                    var tCalc = row.find(".TCalc").find("span").html();

                    $.ajax({
                        type: "Post",
                        contentType: "application/json; charset=utf-8",
                        url: "DaybookMast.aspx/UpdateCustomer121",
                        data:JSON.stringify('{id: ' + id + ',ledger: ' + ledger + ', datafld: "' + datafld + '", aDatafldm: "'
                            + aDatafldm + '", lType: "' + lType + '", cType: "' + cType + '", lAcNo: "'
                            + lAcNo + '", type: "' + type + '", link: "' + link + '", tPer: "'
                            + tPer + '", tCalc: "' + tCalc + '" }'),
                        dataType: "json",                    
                        success: function () {
                            if (confirm("Are you sure you want to change !") == true) {
                                alert("Data Updated successfully");
                            } else {
                                alert("You have canceled the changes");
                            }

                        },
                        error: function () {
                            alert("Error while Updating data of :" );
                        }

                    });

                    return false;
                });

My C# code is-

    [WebMethod]
    public static void UpdateCustomer121(int id,string dayCode, string ledger, string datafld, string aDatafldm, string lType, string cType, string lAcNo, string type, string link, string tPer, string tCalc)
    {
       using (SqlConnection con = new SqlConnection(strConnection))
        {
            using (SqlCommand cmd = new SqlCommand("UPDATE DayBookDetails SET DayCode=@DayCode,Ledger = @Ledger,Datafld = @Datafld,ADatafld = @ADatafld,LType = @LType,CType = @CType,LAcNo = @LAcNo,Type = @Type,Link = @Link,TPer = @TPer,TCalc = @TCalc  WHERE ID = @ID"))
            {
                cmd.Parameters.AddWithValue("@ID",id);
                cmd.Parameters.AddWithValue("@DayCode", dayCode);
                cmd.Parameters.AddWithValue("@Ledger", ledger);
                cmd.Parameters.AddWithValue("@Datafld", datafld);
                cmd.Parameters.AddWithValue("@ADatafld", aDatafldm);
                cmd.Parameters.AddWithValue("@LType", lType);
                cmd.Parameters.AddWithValue("@CType", cType);
                cmd.Parameters.AddWithValue("@LAcNo", lAcNo);
                cmd.Parameters.AddWithValue("@Type", type);
                cmd.Parameters.AddWithValue("@Link", link);
                cmd.Parameters.AddWithValue("@TPer", tPer);
                cmd.Parameters.AddWithValue("@TCalc", tCalc);
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
  • Please give more information, your webmethod, routes and so on. For now everything that i can suggest, that you have incorrect url, `"DaybookMast.aspx/UpdateCustomer121"`, I thing you wont `"DaybookMast.aspx/UpdateCustomer/121"`. But I am not telepathist – Dmitriy Kovalenko Sep 22 '16 at 11:46
  • 1
    Just a side note.. the `success` callback gets executed after your server code's SQL update is already updated. It doesn't make much sense to ask for an approval after an action has already been executed. – Ivan Sivak Sep 22 '16 at 11:47
  • i change in url but it not work and also make changes in success but it shows same error msg – Chetan Sarode Sep 22 '16 at 11:54
  • Check method signatures. They are different. Look at my answare. – zchpit Sep 22 '16 at 12:09

1 Answers1

0

You don't have dayCode in javascript, but you expect it in C# web method

{id: ' + id + ',ledger: ' + ledger + ', datafld: "' + datafld + '", aDatafldm: "

vs

int id,string dayCode, string ledger, string datafld, string aDatafldm,

zchpit
  • 3,071
  • 4
  • 28
  • 43
  • now i remove it from SQL query and also from webmethod but it doesn't work. originally the web method is not calling i already give a breakpoint – Chetan Sarode Sep 22 '16 at 12:12
  • Take this string JSON.stringif("...") as normal string in javascript and look on it if you have some null or undefined or something like that. I had once similar excemtion (code from ajax don't go into web method") and it was parsing problem (in javascript I put string, and web method expected double). – zchpit Sep 22 '16 at 12:15
  • how to apply it.. – Chetan Sarode Sep 22 '16 at 12:26
  • var jsonStr = '{id: ' + id + ',ledger: ' + ledger + ', datafld: "' + datafld + '", aDatafldm: "' + aDatafldm + '", lType: "' + lType + '", cType: "' + cType + '", lAcNo: "' + lAcNo + '", type: "' + type + '", link: "' + link + '", tPer: "' + tPer + '", tCalc: "' + tCalc + '" }' – zchpit Sep 22 '16 at 12:31
  • data:JSON.stringify(jsonStr), – zchpit Sep 22 '16 at 12:31
  • and look in javascript debugger, in example in chrome developer tools of firebug in firefox – zchpit Sep 22 '16 at 12:32
  • in debugger i see all field with changes shows in jsonStr but not going to save to database – Chetan Sarode Sep 22 '16 at 12:38
  • in Console:POST 500 (Internal Server Error) – Chetan Sarode Sep 22 '16 at 12:45
  • Can you put debugger in web method? Debugger enters code? If no, past here what is inside jsonStr. – zchpit Sep 22 '16 at 12:50
  • please check the snapshot of dubugger – Chetan Sarode Sep 22 '16 at 12:58
  • Try use "data" wichout "" (string) but as {} (object) http://stackoverflow.com/questions/36611400/jquery-ajax-post-500-internal-server-error – zchpit Sep 22 '16 at 13:07
  • try it but same error... – Chetan Sarode Sep 22 '16 at 13:20
  • data:JSON.stringify -> data: JSON.stringify and post full url p.s. update your code (javascript and c#) in first post – zchpit Sep 22 '16 at 13:35