asp.net - c# - How to store ArrayList in ViewState? -
i want store arraylist value in viewstate
but getting error:
"type 'system.data.datarow' in assembly 'system.data, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089' not marked serializable"
code:
private void bindgridview() { dbconnection.open(); oledbcommand dbcommand = new oledbcommand("select emp_id,emp_name,father_name,gender,designation,department,location emp_master", dbconnection); oledbdataadapter dbreader1 = new oledbdataadapter(dbcommand); dataset dsemer = new dataset(); dbreader1.fill(dsemer); arraylist arrlist = new arraylist(); foreach (datarow dtrow in dsemer.tables[0].rows) { arrlist.add(dtrow); } //here getting error viewstate["ds"] = arrlist; empmastergrid.datasource = dsemer; empmastergrid.databind(); dbconnection.close(); }
why need store arraylist in viewstate?
by using viewstate export selected gridview column excel using below code
private void getcheckboxstates() { checkbox chkcol0 = (checkbox)empmastergrid.headerrow.cells[0] .findcontrol("chkcol0"); checkbox chkcol1 = (checkbox)empmastergrid.headerrow.cells[0] .findcontrol("chkcol1"); checkbox chkcol2 = (checkbox)empmastergrid.headerrow.cells[0] .findcontrol("chkcol2"); checkbox chkcol3 = (checkbox)empmastergrid.headerrow.cells[0] .findcontrol("chkcol3"); checkbox chkcol4 = (checkbox)empmastergrid.headerrow.cells[0] .findcontrol("chkcol4"); checkbox chkcol5 = (checkbox)empmastergrid.headerrow.cells[0] .findcontrol("chkcol5"); checkbox chkcol6 = (checkbox)empmastergrid.headerrow.cells[0] .findcontrol("chkcol6"); arraylist arr; if (viewstate["ds"] == null) { arr = new arraylist(); } else { arr = (arraylist)viewstate["ds"]; } arr.add(chkcol0.checked); arr.add(chkcol1.checked); arr.add(chkcol2.checked); arr.add(chkcol3.checked); arr.add(chkcol4.checked); arr.add(chkcol5.checked); arr.add(chkcol6.checked); viewstate["ds"] = arr; }
any ideas? in advance. edited!
as error sais system.data.datarow
not serializable can not store in viewstate
.
i suggest make custom class marked serializable
contains values need , save arraylist
of objects in viewstate
. populate these objects in foreach
loop data datarow
.
example:
the data object
[serializable] public class employee { public int emp_id, public string emp_name, //other properties }
in bindgridview method
//other code arraylist arrlist = new arraylist(); foreach (datarow dtrow in dsemer.tables[0].rows) { arrlist.add(new employee { emp_id = dtrow[0], emp_name = dtrow[1], //other properties }; } viewstate["ds"] = arrlist;
Comments
Post a Comment