c# - Empty database table -


i want insert values in "navn" row , "varenr" row in db table, when i'm clicking on button. have following code:

 private void button2_click(object sender, eventargs e)     {          using (sqlconnection cn = new sqlconnection(@"data source=(localdb)\v11.0;attachdbfilename=|datadirectory|\produkt.mdf;integrated security=true"))         {             try             {              sqlcommand cm = new sqlcommand();             cm.connection = cn;             string col1 = textbox2.text;             string col2 = textbox3.text;              //generate sql statement             cm.commandtext = "insert produkttable (navn,varenr) values (@col1,@col2)";             //add sqlparameters             sqlparameter sp_add_col1 = new sqlparameter();             sp_add_col1.parametername = "@col1";             //data type in sqlserver             sp_add_col1.sqldbtype = sqldbtype.nvarchar;             //if data type not number,this property must set             //sp_add_col1.size = 20;             sp_add_col1.value = textbox2.text;             //add parameter collections             cm.parameters.add(sp_add_col1);             //in insert statement, there how many parameter, must write number of parameter             sqlparameter sp_add_col2 = new sqlparameter();             sp_add_col2.parametername = "@col2";             //data type in sqlserver             sp_add_col2.sqldbtype = sqldbtype.nvarchar;             //if data type not number,this property must set             //sp_add_col2.size = 20;             sp_add_col2.value = textbox2.text;             //add parameter collections             cm.parameters.add(sp_add_col2);              //open db execute sql             cn.open();             cm.executenonquery();             cn.close();                 }             catch (exception ex)             {                 messagebox.show("error\n" + ex.message, "error", messageboxbuttons.ok, messageboxicon.error);             }                         }     } 

but unfortunately, data table still empty:

enter image description here

i have set breakpoint on executenonquery function, , triggered, when pressing on button:

enter image description here

my table definition:

enter image description here

your connection string causing this:

data source=(localdb)\v11.0;attachdbfilename=|datadirectory|\produkt.mdf;integrated security=true" 

|datadirectory| database being updated in method in app data directory while 1 trying retrieve data in project folder...

|datadirectory| substitution string indicates path database. datadirectory makes easy share project , deploy application. pc app data directory is:

c:\users\myusername\appdata\... 

if browse location , go following folders

...\local\apps\2.0\data 

you able find particular application directory stored assembly name, or hash when go there find database there being updated fine. connection string best deployment.

you can try this:

enter image description here

if notice server explorer detecting databases on pc , can notice there couple of mindmuscle.mdf files @ different paths, because there 1 file in debug directory, 1 in project directory, 1 in app data directory. ones starting numbers stored in app data directories... if select respective database file , run select query against it, data.

i made tutorial time ago. may you:


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -