Ajax 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
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.
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