c# - Saving to DATABASE MVC pattern -
i new programming please bear me. have written following code in different classes throughout project, , wondering why input not stored in database. hope can me, since have been struggling problem few days now, scavenging darkest corners of web.
first of should tell application using wcf - therefore methods should called through wcfservice client. appreciated!
ps: if helps here screenshot of solution overview: http://imgur.com/jv9xl3k
client code:
using client.wcfserviceref; //my wcf service reference namespace client { public partial class mainwindow : window { private wcfserviceclient client; public mainwindow() { initializecomponent(); client = new wcfserviceclient(); } public string getmoviename() { string moviename = txtname.text; return moviename; } public string getmovielength() { string movielength = txtlength.text; return movielength; } public string getmoviedesc() { string moviedesc = txtdescription.text; return moviedesc; } private void btnmovies_click(object sender, routedeventargs e) { client.addmovie(getmoviename(), getmovielength(), getmoviedesc()); } } }
my moviecontroller code:
using server.db; namespace server.control { class moviectr { public void addmovie(string moviename, string movielength, string moviedesc) { dbmovie conobj = new dbmovie(); conobj.addmovie(moviename, movielength, moviedesc); } } }
my dbmovie class
namespace server.db { public class dbmovie { dbconnection dbcon = new dbconnection(); public void addmovie(string moviename, string movielength, string moviedesc) { dbcon.openconnection(); sqlcommand com = new sqlcommand(); string query = "insert movies (name, runtime, description) values ('" + moviename + "','" + movielength + "','" + moviedesc + "');"; com.commandtext = query; } } }
finally dbconnection class
namespace server.db { public class dbconnection { sqlconnection sc = new sqlconnection(); public void openconnection() { try { sc.connectionstring = ("data source=balder.ucn.dk;initial catalog=dmaa0213_6;user id=dmaa0213_6;password=xxxxxx"); sc.open(); } //endtry catch (sqlexception ex) { console.writeline("could not open connection database"); console.writeline(ex); } } public void closeconnection() { sc.close(); } } }
you need call executenonquery on sqlcommand object.
string query = "insert movies (name, runtime, description) values ('" + moviename + "','" + movielength + "','" + moviedesc + "');"; com.commandtext = query; com.executenonquery();
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx
one more thing, need associate connection command object. example in web page above has sqlconnection object being passed sqlcommand constructor. need rewrite code work properly.
Comments
Post a Comment