I send data through Ajax to update.aspx page along with the id. I don't know whether it is correct or not.
I checked the data, and it is not sent to the update.aspx page.The form consists of fname,age
fields only. This is the way I send the data to update.aspx for update when the user clicks the edit button:
data: {fname: '" + $('#fname').val() + "',age: '" + $('#age').val() + "'}" + "&id=" + id,
Screen shot image.
Ajax full code
$.ajax({
type: 'POST';,
url: 'update.aspx/doSomething',
dataType: 'JSON',
contentType: "application/json; charset=utf-8",
data: { fname: '" + $('#fname').val() + "', age: '" + $('#age').val() + "'}" + "& id=" + id,
success: function(data) {
}
update.aspx page
public class UserClass
{
public string fname { get; set; }
public int age { get; set; }
}
[WebMethod]
public static string doSomething(string fname, int age, int id)
{
SqlConnection con = new SqlConnection("server=.; Initial Catalog = jds; Integrated Security= true;");
string sql = "update record set name ='" + fname + "', age ='" + age + "' where id = '" + id + "')";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
return "Sucess";
}
when i click edit button it goes to the function get_category_details along with record id Edit button code
"sTitle": "Edit",
"mData": "id",
"render": function (mData, type, row, meta) {
return '<button class="btn btn-xs btn-success" onclick="get_category_details(' + mData + ') ">Edit</button>';
get_category_details all data displayed in the textboxs.
function get_category_details(id) {
$.ajax({
type: 'POST',
url: 'edit_return.asmx/doSome',
dataType: 'JSON',
data: "{id: '" + id + "'}",
contentType: "application/json; charset=utf-8",
success: function (data) {
console.log(data);
$("html, body").animate({ scrollTop: 0 }, "slow");
isNew = false;
id = data.d[0].id
$('#fname').attr('value', data.d[0].fname);
$('#age').attr('value', data.d[0].age);
}
});
}
this id varable i used to send for update id = data.d[0].id
change your ajax code as below
var dataValue = { "fname": $('#fname').val(), "age": $('#age').val(), "id": id};
$.ajax({
type:'POST';,
url: 'update.aspx/doSomething',
dataType: 'JSON',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(dataValue),
success: function (data) {
}
});
change your webmethod code as below
public class UserClass
{
public string fname { get; set; }
public int age { get; set; }
public int? id { get; set; }
}
[WebMethod]
public static string doSomething(UserClass userClass)
{
SqlConnection con = new SqlConnection("server=.; Initial Catalog = jds; Integrated Security= true;");
string sql = "update record set name ='" + userClass.fname + "', age ='" + userClass.age + "' where id = '" + userClass.id + "')";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
return "Sucess";
}
You can update only your ajx function like that
$.ajax({
type: 'POST',
url: 'update.aspx/doSomething',
dataType: 'JSON',
contentType: "application/json; charset=utf-8",
data: {
fname:$('#fname').val(),
age:$('#age').val(),
id:id //this id is your like fname or age that.You have get id from your request
}
success: function(data) {
}