c# - Use AJAX to send array of JavaScript objects to database -
in project user have opportunity create array of objects properties , properties match database table, properties of object being same columns in database cable. sql looks like:
create table servicedata (serviceid int ,servicedescription varchar(50) ) go create type servicetype table (serviceid int ,servicedescription varchar(50) ) go create proc spinsertservice @service servicetype readonly begin insert servicedata(serviceid,servicedescription) select * @service end
here create custom type , pass custom type stored procedure in form of table valued parameter. sql , following c# code execute , work fine:
[webmethod] public void insertservicedata() { list<servicedata> sdlist = new list<servicedata>(); servicedata sd1 = new servicedata(1, "first"); servicedata sd2 = new servicedata(2, "second"); sdlist.add(sd1); sdlist.add(sd2); datatable dt = new datatable(); dt.columns.add("serviceid"); dt.columns.add("servicedescription"); foreach (var data in sdlist) { dt.rows.add(data.serviceid, data.servicedescription); } string cs = configurationmanager.connectionstrings["dbcs"].connectionstring; using (var con = new sqlconnection(cs)) { using (var cmd = new sqlcommand("spinsertservice",con)) { con.open(); cmd.commandtype = commandtype.storedprocedure; cmd.parameters.addwithvalue("@service", dt); cmd.executenonquery(); } } }
you can see in working example i'm not using ajax call send data web method. code works , inserts data hardcoded list fine. when change code try take javascript array so:
$(document).ready(function () { sd1 = {}; sd1.serviceid = 1; sd1.servicedescription = "test"; sd2 = {}; sd2.serviceid = 2; sd2.servicedescription = "other test"; //create array meant mirror list<servicedata> in //earlier example service = new array(); service.push(sd1); service.push(sd2); //wrap array in data transfer object var dto = {'sdlist': service}; $('#btnsubmit').click(function () { $.ajax( { type: "post", url: "webservice.asmx/insertservicedata", contenttype: "application/json", datatype: "json", //stringify dto data: json.stringify(dto), success: function(data) { console.log('success'); }, error: function(thrownerror) { console.log(thrownerror); } }); }); });
new c#
[webmethod] //this attempts deserialize dto list of servicedata objects //which inserted tvp , database public void insertservicedata(string sdlist) { var jss = new javascriptserializer(); list<servicedata> list = jss.deserialize<list<servicedata>>(sdlist); datatable dt = new datatable(); dt.columns.add("serviceid"); dt.columns.add("servicedescription"); foreach (var data in list) { dt.rows.add(data.serviceid, data.servicedescription); } string cs = configurationmanager.connectionstrings["dbcs"].connectionstring; using (var con = new sqlconnection(cs)) { using (var cmd = new sqlcommand("spinsertservice",con)) { con.open(); cmd.commandtype = commandtype.storedprocedure; cmd.parameters.addwithvalue("@service", dt); cmd.executenonquery(); } } }
currently code gives me error: type\u0027system.string\u0027isnotsupportedfordeserializationofanarray
if don't wrap array in dto object, still stringify
`system.collections.generic.idictionary`2[[system.string,mscorlib,version=4.0.0.0,culture=neutral,publickeytoken`
i not have use sessionstate of viewstate this. since know code works fine if i'm not passing javascript array webmethod, has been somewhere in serialization , deserialization of array that's breaking it. how can fix this? it's been driving me nuts days now
please note following steps , change code lines.
- dont create
sd1={}
,sd2{}
. - create
var list = [];
@ top push json objectlist.push({"serviceid":1,"servicedescription":"test"}, {"serviceid":2,"servicedescription":"other test"})
- create ajax parameter
var data = "{'sdlists':" +json.stringify(list)+"}";
, pass date param. create bean variables map json object added in list above. see blow.
public class sdlist{ private int serviceid; public int serviceid { { return serviceid; } set { serviceid= value; } } private string servicedescription; public string servicedescription { { return servicedescription; } set { servicedescription= value; } } }
now pass
list<sdlist> sdlists
parameter list method undervar data = "{'sdlists':"+json.stringify(list)+"}"; public void insertservicedata(list sdlists)
then convert json list in generic list using java script serializer under,
javascriptserializer jss= new javascriptserializer();
list<sdlist> list = jss.converttotype<list<sdlist>>(sdlists);
i have followed above steps , working fine.
Comments
Post a Comment