.net - Adding a row to a dataset is not updating the database (C#) -
a simple, yet frustrating issue. i've bound datagrid local database play around , teach myself datasets , c#. binding data, no problem. custom fills (and sql selects), no problem. next comes adding record. so, add in following code:
private void badd_click(object sender, eventargs e) { datarow newrow = abbtestdataset.tables["main"].newrow(); newrow["name"] = "simon"; newrow["age"] = 23; abbtestdataset.tables["main"].rows.add(newrow); }
the datagrid updates no problem, data never committed database. i've searched high , low solution or may missing out on , nothing; @ least i've seen i'm doing right.
so missing? undoubtedly stupid.
i believe need use dataadapter
send updated dataset
database.
check out links below more info:
http://www.java2s.com/tutorial/csharp/0560__ado.net/usedatatabletoinsertarow.htm
the relevant sample first link is:
string connectionstring ="server=(local)\\sqlexpress;database=mydatabase;integrated security=sspi;"; sqlconnection conn = new sqlconnection(connectionstring); conn.open(); sqldataadapter adapter = new sqldataadapter("select * employee order id", conn); sqlcommandbuilder builder = new sqlcommandbuilder(adapter); // create dataset object dataset ds = new dataset("employeeset"); adapter.fill(ds, "employee"); // create data table object , add new row datatable employeetable = ds.tables["employee"]; datarow row = employeetable.newrow(); row["firstname"] = "r"; row["lastname"] = "d"; row["id"] = "10"; employeetable.rows.add(row); // update data adapter adapter.update(ds, "employee");