mysql - SqlDataAdapter Insert Database not working C# winform -
i trying insert data sql server database , show on datagridview c# winforms.
i click on insert button no error shows, no data being insert database , datagridview becomes blank.
i re-debug can see data update in datagridview, database not on visual studio server explorer database(picture1).
on other hand.i set breakpoint when click insert button,that jump on line 42~46.directly line 50.ex(picture2).
edit: question when click insert button,datagridview have update new data.but database no insert new data.database 2 data.
edit2 changed connection string attachdbfilename. attachdbfilename=c:\vis\no4\windowsformsapplication1\app_data\database1.mdf;
value can insert database.
here connection string:
<add name="con1" connectionstring="data source=(localdb)\v11.0;attachdbfilename=|datadirectory|\database1.mdf;integrated security=true" providername="system.data.sqlclient" />
here form_load , load_grid function
sqlconnection con; sqldataadapter da; dataset ds = new dataset(); datatable dt = new datatable(); public form1() { initializecomponent(); string connectionstring = configurationmanager.connectionstrings["con1"].connectionstring; con = new sqlconnection(connectionstring); } private void form1_load(object sender, eventargs e) { load_grid(); } public void load_grid() { dt.clear(); sqldataadapter da1 = new sqldataadapter(); try { string sql = "select * profile"; con.open(); da1.selectcommand = new sqlcommand(sql, con); da1.fill(ds); da1.fill(dt); con.close(); datagridview1.datasource = dt; } catch (exception ex) { messagebox.show(ex.message); } }
here insert button
private void button1_click(object sender, eventargs e) { string ac = textbox1.text; string bd = textbox2.text; string sex = textbox2.text; string sql = "insert profile(ac,bd,sex)values('" + ac + "','" + bd + "','" + sex + "')"; try { con.open(); da = new sqldataadapter(); da.insertcommand = new sqlcommand(sql, con); da.insertcommand.executenonquery(); da.update(dt); messagebox.show("insert success...!!"); load_grid(); con.close(); } catch(exception ex) { messagebox.show(ex.message); } }
here design of costumer table
create table [dbo].[profile] ( [id] int identity (1, 1) not null, [ac] nvarchar (50) null, [bd] nvarchar (50) null, [sex] nvarchar (50) null, primary key clustered ([id] asc));
not sure issue here.
picture2 shows exception connection open so, try following before opening connection. calling load_grid();
before closing connection. have update code, use explained below:
edit- second revision:
public void load_grid() { dt.clear(); sqldataadapter da1 = new sqldataadapter(); try { string sql = "select * profile"; con.open(); da1.selectcommand = new sqlcommand(sql, con); da1.fill(ds); da1.fill(dt); con.close(); datagridview1.datasource = dt; } catch (exception ex) { messagebox.show(ex.message); } { if (con.state != system.data.connectionstate.closed) con.close(); } } private void button1_click(object sender, eventargs e) { string ac = textbox1.text; string bd = textbox2.text; string sex = textbox2.text; string sql = "insert profile(ac,bd,sex)values('" + ac + "','" + bd + "','" + sex + "')"; try { con.open(); da = new sqldataadapter(); da.insertcommand = new sqlcommand(sql, con); da.insertcommand.executenonquery(); da.update(dt); con.close(); messagebox.show("insert success...!!"); load_grid(); } catch (exception ex) { messagebox.show(ex.message); } { if (con.state != system.data.connectionstate.closed) con.close(); } }
Comments
Post a Comment