3
Answers

Record is added in to the database but alert message is not

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 ajax success function to display alert("success");
 
Form Design
  1. <form id="frmProject" runat="server">  
  2. <div>  
  3. <label class="form-label">First Name</label>  
  4. <input type="text" id="fname" class="form-control" />  
  5. </div>  
  6. <div class="form-group" >  
  7. <label class="form-label">Age</label>  
  8. <input type="text" id="age" class="form-control" />  
  9. </div>  
  10. <div>  
  11. <input type="button" id="b1" value="add" class="form-control" onclick="addProject()" />  
  12. </div>  
  13. </form>  
Ajax
  1. function addProject() {  
  2. $.ajax({  
  3. type: 'POST',  
  4. url: 'insert.aspx',  
  5. dataType: 'JSON',  
  6. data: {fname: $('#fname').val(), age: $('#age').val()},  
  7. success: function (data) {  
  8. alert("success");  
  9. get_all();  
  10. },  
  11. error: function (xhr, status, error) {  
  12. console.log(xhr.responseText);  
  13. }  
  14. });  
  15. }  
insert.aspx
  1. SqlConnection con = new SqlConnection("server=.; Initial Catalog = jds; Integrated Security= true;");  
  2. protected void Page_Load(object sender, EventArgs e)  
  3. {  
  4. string fname = Request.Form["fname"];  
  5. string age = Request.Form["age"];  
  6. string sql = "insert into record values('" + fname + "','" + age+ "')";  
  7. SqlCommand cmd = new SqlCommand(sql, con);  
  8. con.Open();  
  9. cmd.ExecuteNonQuery();  
  10. Response.Write("success")  
  11. }

Answers (3)