database - Insert SQL (Access) not working using webmethods Asp.net -
i trying insert fields local ms access database using webmethod(services) , website. have tried looking cannot seem spot i've gone wrong. can tell me if doing right. code below not add new data database nor direct me page requested.
services webmethod:
[webmethod] public void addnewposts(string postusername, string posttitle, datetime postmessagepostdatetime, int subtopicid, string postmessage) { //connection string datbase string database = "provider=microsoft.ace.oledb.12.0;data source=|datadirectory|/forum.accdb;persist security info=true"; oledbconnection myconn = new oledbconnection(database); //execute query string querystr = "insert posts (topicid, posttitle, postusername, postdatetime) values (" + subtopicid + ",'" + posttitle + "','" + postusername + "'," + postmessagepostdatetime + ")"; // create command object oledbcommand mycommand = new oledbcommand(querystr, myconn); // open connection mycommand.connection.open(); mycommand.executenonquery(); mycommand.connection.close(); }
calling above method website:
protected void btnsubmit_click(object sender, eventargs e) { //string postusername = page.user.identity.name; string postusername = "tom123"; string posttitle = txttitle.text; string postmessage = txtmessage.text; datetime postdatetime = datetime.now; int subtopicid = int.parse(request.querystring["id"]); service fs = new service(); fs.addnewposts(postusername, posttitle, postdatetime, subtopicid, postmessage); //redirect subtopic page response.redirect("subtopic.aspx?id=" + subtopicid.tostring()); }
can try quotes around date time string querystr = "insert posts (topicid, posttitle, postusername, postdatetime) values (" + subtopicid + ",'" + posttitle + "','" + postusername + "','" + postmessagepostdatetime + "')";