Reputation: 179
The alert message is not displayed after add record into the database using Asp.net Ajax.record is add into the database successfully.Alert is not displaying. I attached the code below what I tried so far below.after add record how to return to the ajex success function to display alert("success");
Form Design
<form id="frmProject" runat="server">
<div>
<label class="form-label">First Name</label>
<input type="text" id="fname" class="form-control" />
</div>
<div class="form-group" align="left">
<label class="form-label">Age</label>
<input type="text" id="age" class="form-control" />
</div>
<div>
<input type="button" id="b1" value="add" class="form-control" onclick="addProject()" />
</div>
</form>
Ajex
function addProject() {
$.ajax({
type: 'POST',
url: 'insert.aspx',
dataType: 'JSON',
data: {fname: $('#fname').val(), age: $('#age').val()},
success: function (data) {
alert("success");
get_all();
},
error: function (xhr, status, error) {
console.log(xhr.responseText);
}
});
}
insert.aspx
public static string GetData(string fname, int age)
{
SqlConnection con = new SqlConnection("server=.; Initial Catalog = jds; Integrated Security= true;");
string sql = "insert into record values('" + fname + "','" + age + "')";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
return HttpContext.Current.Session.SessionID;
}
Upvotes: 0
Views: 534
Reputation: 143
use web method for Ajax request
[WebMethod(EnableSession= true)]
public static string GetData(string fname,int age)
{
// place your logic here
return HttpContext.Current.Session.SessionID;
}
Your Ajax request
$('#b1').click(function () {
jQuery.ajax({
url: 'insert.aspx/GetData',
type: "POST",
data: {fname: $('#fname').val(), age: $('#age').val()},
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(JSON.stringify(data));
}
});
});
Upvotes: 1