c# - DataAdapter: Update unable to find TableMapping['Table'] or DataTable 'Table' -
this code snippet throwing error:
update unable find tablemapping['table'] or datatable 'table'.) on adapter.update(ds); line
why throwing type of error?
sqlconnection con = new sqlconnection(); con.connectionstring = connectionstring(); dataset ds = new dataset(); string strquery = "select * cars"; sqldataadapter adapter = new sqldataadapter(); adapter.selectcommand = new sqlcommand(strquery, con); sqlcommandbuilder builder = new sqlcommandbuilder(adapter); adapter.fill(ds, "cars"); //code modify data in dataset ds.tables["cars"].rows[0]["brand"] = "newbrand"; adapter.updatecommand = builder.getupdatecommand(); adapter.update(ds);
use
adapter.update(ds, "cars"); instead.
i have tested it. got same error without , works if specify tablename. however, must admit yet don't know why dataadapter needs know table-name since has informations needs. if useds.getchanges 1 row correct table-name.
update have found nothing on msdn found in source(ilspy). here implementation of dbdataadapter.update(dataset):
public override int update(dataset dataset) { return this.update(dataset, "table"); } so if don't specify table, table-name "table" used , if you've specified table-name other you'll error, that's strange!
i assume reason dataadapter cannot call getchanges determine table update 2 reasons:
- it inefficient since needs loop tables , of rows find rows
rowstate!=unchanged - it's possible multiple tables needs updated since contain changed rows. not supported via
dataadapter. hencedataadapter.update(dataset)assumes default name"table"table-name.
edit: however, maybe can explain me why dataadapter doesn't use dataset.tables[0].tablename instead.
so in general seems best practise specify name of table want update.
Comments
Post a Comment