Data not saved in SQL Server database after rerun c# -
i've got connection string working. have written code insert table called customers
in database.mdf
. when click "registreer" button in form insert name in usernametf2 table , after call showcustomers
method reload listbox..
when rerun program, still shows previous made names in listbox, still doesn't show names in server explorer.
code:
using system; using system.collections.generic; using system.configuration; using system.componentmodel; using system.data; using system.data.sqlclient; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using productclient.productservice; using testdb; using system.configuration; using system.data.sqlclient; namespace windowsformsapplication1 { public partial class form1 : form { productserviceclient productproxy = new productserviceclient(); sqlconnection connection; string connectionstring; public form1() { initializecomponent(); connectionstring = configurationmanager.connectionstrings["testdb.properties.settings.databaseconnectionstring"].connectionstring; showcustomers(); } private void showcustomers() { using (connection = new sqlconnection(connectionstring)) using (sqldataadapter adapter = new sqldataadapter("select * customer", connection)) { datatable producttable2 = new datatable(); adapter.fill(producttable2); lstbox.displaymember = "name"; lstbox.valuemember = "id"; lstbox.datasource = producttable2; } } private void registreerbtn_click(object sender, eventargs e) { string response = productproxy.register(usernametf2.text); wachtwoordlab.text = response; string query = "insert customer values(@name, 20)"; //// using (connection = new sqlconnection(connectionstring))//// using (sqlcommand command = new sqlcommand(query, connection))//// { connection.open(); command.parameters.addwithvalue("@name",usernametf2.text);//// command.executenonquery(); messagebox.show("added"); connection.close(); } showcustomers(); } } }
the copy output directory
set copy if newer
.
connectionstring:
in form:
connectionstring = configurationmanager.connectionstrings["testdb.properties.settings.databaseconnectionstring"].connectionstring;
in app config:
connectionstring="data source=(localdb)\v11.0;attachdbfilename=|datadirectory|\database.mdf;integrated security=true"
i believe after looking @ inside code should placing correctly, surprised not getting error while trying connect database though because feel connection problem. without looking @ connection properties hard tell of course.
what suggest putting code in surrounding try catch statements in order see going wrong on connection or input if case. along side can check event viewer in windows why connection not established believe.
i hope helps.
for further information on connection strings believe great website use www.connectionstrings.com double check. if still have problems feel free post more information can more.
Comments
Post a Comment