After Insert the record at index.aspx page redirect to ajax success function and show the message alert("success");
I wrote function addProject() to do the task. i didn't get any error. record is added successfully in the database. but alert("success") message is not displayed.
i tried the code below
error displayed an object reference is required for non-static field, method
error was displayed this lines get error
string fname = Request.Form["fname"];
string age = Request.Form["age"]
insert.aspx
public static string doSomething(int id)
{
string fname = Request.Form["fname"];
string age = Request.Form["age"];
string sql = "insert into record values('" + fname + "','" + age + "')";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
return "Sucess";
}
Ajex
function addProject() {
$.ajax({
type: 'POST',
url: 'insert.aspx/doSomething',
dataType: 'JSON',
data: {fname: $('#fname').val(), age: $('#age').val()},
success: function (data) {
alert("success");
},
error: function (xhr, status, error) {
console.log(xhr.responseText);
}
});
}