c# - Calculate 2 dataGridView and Update an SQL Data Table -
i have 2 datatables table_1 , table_2. what's wrong code? can't add in table_2 sum of stock table_1?
when update once, have correct output.
http://tinypic.com/view.php?pic=1632bs&s=5
when press update button once more, updates first row, shown in following pic:
http://tinypic.com/r/350p6hd/5
private void txtid_textchanged(object sender, eventargs e) { sqlconnection csq = new sqlconnection("workstation id=;initial catalog=iridadb; integrated security=sspi"); sqldataadapter dsaq = new sqldataadapter(); dataset dsq = new dataset(); dsaq.selectcommand = new sqlcommand("select * table_1 left(id, " + txtid.text.length + ") = '" + txtid.text + "' order itemid asc", csq); dsq.clear(); dsaq.fill(dsq); datagridview1.datasource = dsq.tables[0]; datagridview1.allowusertoaddrows = false; binddatagridview2(); } public void binddatagridview2() { sqlconnection csq = new sqlconnection("workstation id=;initial catalog=iridadb; integrated security=sspi"); sqldataadapter dsaq = new sqldataadapter(); dataset dsq = new dataset(); dsaq.selectcommand = new sqlcommand("select * table_2 ", csq); dsq.clear(); dsaq.fill(dsq); datagridview2.datasource = dsq.tables[0]; datagridview2.allowusertoaddrows = false; } private void btnupdate_click(object sender, eventargs e) { (int = 0; <= datagridview1.rows.count - 1; i++) { if (!datagridview1.rows[i].isnewrow) { sqlconnection connan = new sqlconnection("data source=;initial catalog=iridadb; integrated security=sspi"); sqldataadapter danan = new sqldataadapter(); danan.updatecommand = new sqlcommand("update table_2 set stock = @stock itemid = @itemid", connan); danan.updatecommand.parameters.add("@itemid", sqldbtype.varchar).value = convert.tostring(datagridview1.rows[i].cells[1].value).tostring(); danan.updatecommand.parameters.add("@stock", sqldbtype.varchar).value = (double.parse(datagridview1.rows[i].cells[2].value.tostring()) + double.parse(datagridview2.rows[i].cells[1].value.tostring())).tostring(); connan.open(); danan.updatecommand.executenonquery(); connan.close(); binddatagridview2(); } } }
check calculations of sum:
(double.parse(datagridview1.rows[i].cells[2].value.tostring()) + double.parse(datagridview2.rows[i].cells[1].value.tostring())).tostring();
you calculating sum of rows same rowindex
, updating database itemid
when in looping index i = 2
count stock(where itemid = 03) + stock(where itemid = 02) -> stock of item 02 not updating stay on 100. thats why item 03 stock 100 + 1
because total stock in database already, use query this
update table_2 set stock = cast( cast(stock decimal(19,4)) + cast(@stock decimal(19,4)) varchar(max)) itemid = @itemid"
and take values datagridview1
danan.updatecommand.parameters.add("@itemid", sqldbtype.varchar).value = convert.tostring(datagridview1.rows[i].cells[1].value).tostring(); // danan.updatecommand.parameters.add("@stock", sqldbtype.varchar).value = (double.parse(datagridview1.rows[i].cells[2].value.tostring())
p.s. sorry, seems strangely, when stored values of double
/decimal
varchar
...?
Comments
Post a Comment