c# - Passing a Datatable From One Form to Another Form -
i have form1 , form2. form2 opened form1, has dgv after filling it, it's copied datatable on click button, , form2 closed or visible = false. passed datatable form1 it's giving me null reference. here code in form2:
private void button1_click(object sender, eventargs e) { perclothesdescriptiondt = new datatable(); foreach (datagridviewcolumn col in datagridview3.columns) { perclothesdescriptiondt.columns.add(col.headertext); } foreach (datagridviewrow row in datagridview3.rows) { datarow drow = perclothesdescriptiondt.newrow(); foreach (datagridviewcell cell in row.cells) { drow[cell.columnindex] = cell.value; } perclothesdescriptiondt.columns.removeat(0); perclothesdescriptiondt.rows.add(drow); } this.visible = false; } public datatable mydt { { return perclothesdescriptiondt; } }
in form1:
form2= new form2(); cmd1.commandtext = "insert [dbo].[personclothesdesc](upperpart, lowerpart, belt, socks, shoes, differentsigns) values (@upperpart, @lowerpart, @belt, @socks, @shoes, @differentsigns)"; (int = 0; < form2.mydt.rows.count; i++) { cmd1.parameters.clear(); cmd1.parameters.addwithvalue("@upperpart", form2.mydt.rows[i].itemarray.getvalue(5).tostring()); cmd1.parameters.addwithvalue("@lowerpart", form2.mydt.rows[i].itemarray.getvalue(4).tostring()); cmd1.parameters.addwithvalue("@belt", form2.mydt.rows[i].itemarray.getvalue(3).tostring()); cmd1.parameters.addwithvalue("@socks", form2.mydt.rows[i].itemarray.getvalue(2).tostring()); cmd1.parameters.addwithvalue("@shoes", form2.mydt.rows[i].itemarray.getvalue(1).tostring()); cmd1.parameters.addwithvalue("@differentsigns", form2.mydt.rows[i].itemarray.getvalue(0).tostring()); cmd1.executenonquery(); }
Comments
Post a Comment