-3

enter image description hereAjax function not updating the data. Data load in dialogbox of bootstrap and i need to update the data with changes.i also check in browser the value is come from dialog box but not posting in database..How to solve..Thanks in advance

Client-side:

$("#UpdateTbl").click(function () {
    var id = $(this).attr("edit-id");
    var user = {};
    debugger;
    user.DayDesc = $("#DaybookDesc1").val();
    user.VoucherNo = $("#Prifix1").val();
    user.VoucherNo1 = $("#Surffix1").val();


    $.ajax({
        type: "Post",
        contentType: "application/json; charset=utf-8",
        url: "DaybookMast.aspx/UpdateData",
        data:JSON.stringify( '{objEmployee: ' + JSON.stringify(user) + ', eid : ' + id + '}'),
        dataType: "json",
        success: function (data) {
            if (confirm("Are you want to change !") == true) {
                alert("Updated successfully");
            } else {
                alert("canceled changes");
            }

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

Server-side:

[WebMethod]
public static void UpdateData(DayBooks objEmployee, string eid) //Update data in database      
{
    using (var con = new SqlConnection(strConnection))
    {
        var query = "update DayBooks set DayDesc='" + objEmployee.DayDesc + "',VoucherNo='" + objEmployee.VoucherNo +
                    "',VoucherNo1='" + objEmployee.VoucherNo1 + "'where DayCode='" + eid + "'";
        con.Open();
        var cmd = new SqlCommand(query, con);
        cmd.ExecuteNonQuery();
        con.Close();
    }
} 

2 Answers2

1

I think you should post your data without stringifying your JSON:

$("#UpdateTbl").click(function () {
    var id = $(this).attr("edit-id");
    var user = {};
    debugger;
    user.DayDesc = $("#DaybookDesc1").val();
    user.VoucherNo = $("#Prifix1").val();
    user.VoucherNo1 = $("#Surffix1").val();


    $.ajax({
        type: "Post",
        contentType: "application/json; charset=utf-8",
        url: "DaybookMast.aspx/UpdateData",
        data: {"objEmployee": user, "eid": id},
        dataType: "json",
        success: function (data) {
            if (confirm("Are you want to change !") == true) {
                alert("Updated successfully");
            } else {
                alert("canceled changes");
            }
        },
        error: function (data) {
            alert("Error while Updating data of :" + id);
        }
    });
});

On a side note: don't do SQL injection in your DB statements... Use parameters instead to avoid possible security breaches. More info @ https://stackoverflow.com/a/6548006/2805121

Community
  • 1
  • 1
Nsevens
  • 2,588
  • 1
  • 17
  • 34
1

First of all, What error message did you have?

Before testing communication between Ajax & C# update function, you need to test if connection is functionnal.

public static void UpdateData(DayBooks objEmployee, string eid) //Update data in database      
{
    using (var con = new SqlConnection(strConnection))
    {
        var query = "UPDATE DayBooks set DayDesc=12345,VoucherNo=54321,VoucherNo1=1 where DayCode=12";
        con.Open();
        var cmd = new SqlCommand(query, con);
        cmd.ExecuteNonQuery();
        con.Close();
    }
} 

And check if your table has updated,if update table success. you need to debug Ajax data request.

thanks to @Nsevens code :

$("#UpdateTbl").click(function () {
    var id = $(this).attr("edit-id");        <- what's value of id
    var user = {};
    debugger;
    user.DayDesc = $("#DaybookDesc1").val(); <- what's value of user.DayDesc
    user.VoucherNo = $("#Prifix1").val();    <- what's value of user.VoucherNo
    user.VoucherNo1 = $("#Surffix1").val();  <- what's value of user.VoucherNo1


    $.ajax({
        type: "Post",
        contentType: "application/json; charset=utf-8",
        url: "DaybookMast.aspx/UpdateData",
        data: {"objEmployee": user, "eid": id}, <- what's value of data 
        dataType: "json",
        success: function (data) {
            if (confirm("Are you want to change !") == true) {
                alert("Updated successfully");
            } else {
                alert("canceled changes");
            }
        },
        error: function (data) {
            alert("Error while Updating data of :" + id);
        }
    });
});

use console.log for debugging:

console.log("userArray : " + user);

it's can be helpful, when you have a problem like that, it's important to check what data do you send.

Kate
  • 320
  • 1
  • 6
  • 19