insert data into sql database using jquery ajax in asp.net c# -
i using 3 tier architecture , tried store data database using jquery ajax not getting response in success function
here 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 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 getting error alert message control not entering success function not showing error on browser
try 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"); // code }, error: function (result) { alert("error"); } }
create 1 class 4 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 method parameter class object. can recevie number of parameter ajax call 1 object.
[webmethod] public string insertfeedback(yourclass model) { datatable dt = detailsbal.insertfeedback(model.name, model.email, model.category, model.message); return jsonconvert.serializeobject(dt); }
Comments
Post a Comment