简体   繁体   中英

insert data into sql database using jquery ajax in asp.net c#

I am using three tier architecture and tried to store data into a database using jquery ajax but am not getting a response in the success function

here is my code

portalDAL.cs

public DataTable InsertFeedBack(String Name, String Email, string Category, string Message)
  {
  SqlParameter[] parms = new SqlParameter[]{


  new SqlParameter("@Name",Name),
  new SqlParameter("@Email",Email),
  new SqlParameter("@Category",Category),
  new SqlParameter("@Message",Message)

  };
  return Helper.ExecuteParamerizedSelectCommand("insert into feedback(name,email,category,message) values(@Name,@Email,@Category,@Message)", CommandType.Text, parms);
  }

portalBAL.cs

 public DataTable InsertFeedBack(String Name, String Email, string Category, string Message)
  {
  return portalDAL.InsertFeedBack(Name, Email, Category, Message);

  }

portal.asmx.cs

 [WebMethod]
  public String InsertFeedBack(String Name, String Email, string Category, string Message)
  {
  DataTable dt = detailsBAL.InsertFeedBack(Name, Email, Category, Message);
  return JsonConvert.SerializeObject(dt);
  }

My Jquery Function.

$(document).ready(function () {
  $('#submit').click(function () {

  var name = $('#name').val();
  var email = $('#email').val();
  var category = $('#cate').val();
  var msg = $('#msg').val();

  insertFeedback(name,email,category,msg);
  });



function insertFeedback(name,email,cat,msg)
  {
  $.ajax({
  type: "POST",
  url: "portal.asmx/InsertFeedBack",
  data: "{'Name':'" + name + "','Email':'" + email + "','Category':'" + cat + "','Message':'" + msg + "'}",

  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function (data) {
  alert("hi");
  var obj = data.d;
  if (obj == 'true') {
  $('#name').val('');
  $('#email').val('');
  $('#cate').val('');
  $('#msg').val('');
  $('#lblmsg1').html("Details Submitted Successfully");
  window.location.reload();
  }
  },
  error: function (result) {
  alert("Error");
  }
  });

  } 
  });

I am getting error alert message control is not entering to success function its not showing any error on browser

try to use this ajax code format.

$.ajax({
    type: "POST",
    dataType: "json",
    contentType: "application/json; charset=utf-8", 
    data: "{'Name':'" + name + "','Email':'" + email + "','Category':'" + cat + "','Message':'" + msg + "'}",
    url: "portal.asmx/InsertFeedBack",
    success: function (data) {
        console.log(data);
    },
    error: function (error) {
        console.log(error);
    }
});

try with this code

$('#submit').click(function () {
  insertFeedback();
});

function insertFeedback()
{
var model = new Object();
  model.name = $('#name').val();
  model.email = $('#email').val();
  model.category = $('#cate').val();
  model.msg = $('#msg').val();

$.ajax({
type: "POST",
url: "portal.asmx/InsertFeedBack",
data: model,
dataType: "json",
success: function (data) {
     alert("hi");
    // your code
    },
error: function (result) {
    alert("Error");
 }
} 

create one class with four property

public class YourClass
{
 public string name { get; set; }
 public string email { get; set; }
 public string category { get; set; }
 public string message { get; set; }
}

change your method parameter to class object. you can recevie number of parameter from ajax call with one object.

[WebMethod]
public String InsertFeedBack(YourClass model)
{
 DataTable dt = detailsBAL.InsertFeedBack(model.name, model.email, model.category, model.message);
 return JsonConvert.SerializeObject(dt);
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM