sql server - Couldn't send and store String data to database through WCF -
i have created windows 8 app, have table in sql server database store people's name, " [name] varchar (50)"
i have manage send , save integer values database, when modified coding store string, not work, table data empty. please help!
itemdetail.html
<div> <input id="join1" type="text" /> <button id="joinbtn">insert</button> </div>
itemdetail.js
var joinbutton = document.getelementbyid('joinbtn'); // register click event joinbutton.addeventlistener("click", joinbuttonclick, false); function joinbuttonclick() { // retrieve element var baseuri2 = "http://localhost:45573/addservice.svc/join"; var jointext = document.getelementbyid('join1').value; winjs.xhr({ type: "post", url: baseuri2, headers: { "content-type": "application/json" }, data: '{"namet":' + jointext + '}' }).then(function complete(request) { var resdata = request.responsetext; }, function error(er) { var err = er.statustext; }) }
addservice.svc.cs
public void join(string namet) { string connectionstring = system.configuration.configurationmanager. connectionstrings["database1connectionstring1"].connectionstring; sqlconnection con = new sqlconnection(connectionstring); string sql = "insert table2(name) values (@name)"; sqlcommand cmd = new sqlcommand(sql, con); cmd.parameters.addwithvalue("@name", namet); try { con.open(); int numaff = cmd.executenonquery(); } con.close(); }
iaddservice.cs
[operationcontract] [webinvoke(requestformat = webmessageformat.json, responseformat = webmessageformat.json, bodystyle = webmessagebodystyle.wrapped)] void join(string namet);
thank you!
i think problem may in line
data: '{"namet":' + jointext + '}'
try changing to
data: '{"namet":\'' + jointext + '\'}'
Comments
Post a Comment