vb.net - DataTable belongs to another Data set / Saves extra Entries in XML -
i know question has been asked , answered here.
ds.tables.add(dt.copy) ds.tables(0).tablename = "items" ds.writexml("inventory.xml")
dt.copy resolve "datatable belongs data set" message when save, of course screws xml file , saves entries in file, when data grid loads xml file shows entries weren't there before. here's code.
try dt = new datatable() dim ds new dataset 'dtcopy = dt.copy() dt.columns.add(new datacolumn("product_name", type.gettype("system.string"))) dt.columns.add(new datacolumn("product_sku", type.gettype("system.string"))) dt.columns.add(new datacolumn("product_price", type.gettype("system.decimal"))) dt.columns.add(new datacolumn("product_quantity", type.gettype("system.int32"))) dim integer = pinventory.rows.count = - 1 each prow in pinventory.rows if <> 0 or > 0 each pcell in prow.cells if isdbnull(pcell.value) = true if pcell.columnindex = 0 prow.cells(0).value = "(name)" end if if pcell.columnindex = 1 prow.cells(1).value = "(sku)" end if if pcell.columnindex = 2 prow.cells(2).value = 0 end if if pcell.columnindex = 3 prow.cells(3).value = 0 end if end if next fillrows(prow.cells(0).value, prow.cells(1).value, prow.cells(2).value, prow.cells(3).value) = - 1 end if next ds.tables.add(dt) ds.tables(0).tablename = "items" ds.writexml("product.xml") 'msgbox("done") catch ex exception msgbox(ex.message) end try
here's result when loading xml dataset datagridview (inventory). empty cells weren't there before, after grid gets reloaded.
here's code loading xml datagridview
dim xmlfile xmlreader xmlfile = xmlreader.create("product.xml", new xmlreadersettings()) dim ds new dataset ds.readxml(xmlfile) pinventory.datasource = ds.tables(0) xmlfile.close()
i've gone through of other posts this, though while fix table message problem, don't have specifics regarding effects on table themselves. if can me, i'd appreciate it.... lot!!
private sub fillrows(byval pname string, byval psku string, byval pprice decimal, byval pquantity integer) try dim dr datarow dr = dt.newrow() dr("product_name") = pname dr("product_sku") = psku dr("product_price") = pprice dr("product_quantity") = pquantity dt.rows.add(dr) catch ex exception msgbox(ex.message) end try end sub
this xml looks when working correctly.. when doesn't show new entries 2 column (price , quantity) have 0's in them.
<?xml version="1.0" standalone="true"?> -<newdataset> -<items> <product_name>harry potter</product_name> <product_sku>546826584</product_sku> <product_price>10.99</product_price> <product_quantity>2</product_quantity> </items> -<items> <product_name>the last samurai</product_name> <product_sku>326548742</product_sku> <product_price>12.99</product_price> <product_quantity>1</product_quantity> </items> -<items> <product_name>(name)</product_name> <product_sku>545625986</product_sku> <product_price>5.79</product_price> <product_quantity>5</product_quantity> </items> -<items> <product_name>(name)</product_name> <product_sku>365989568</product_sku> <product_price>3.99</product_price> <product_quantity>3</product_quantity> </items> -<items> <product_name>(name)</product_name> <product_sku>(sku)</product_sku> <product_price>0</product_price> <product_quantity>0</product_quantity> </items> -<items> <product_name>(name)</product_name> <product_sku>(sku)</product_sku> <product_price>3.99</product_price> <product_quantity>3</product_quantity> </items> </newdataset>
Comments
Post a Comment