asp.net - How to create global dataTable in C#? -
i need use global datatable in .net project. however, cannot handle between 2 methods..
in example, dt1 global datatable , dt2 local, dt2 direct using call method.
result: dt1: don't know how describe it, likes whole html page in excel.
dt2: well!
can tell me why dt1 wrong ? should perfect.. code:
private datatable dt1;// same result public datatable dt1{ get; private set; }"" protected void page_load(object sender, eventargs e) { if (!page.ispostback) { datatable dt = new datatable(); dt.columns.addrange(new datacolumn[3] { new datacolumn("shorturl", typeof(int)), new datacolumn("longurl", typeof(string)), new datacolumn("creatingdate",typeof(string)) }); dt.rows.add(1, "john hammond", "united states"); dt.rows.add(2, "mudassar khan", "india"); dt.rows.add(3, "suzanne mathews", "france"); dt.rows.add(4, "robert schidner", "russia"); dt1 = dt; } } protected void btn_click(object sender, eventargs e) { datatable dt2 = new datatable(); dt2.columns.addrange(new datacolumn[3] { new datacolumn("shorturl", typeof(int)), new datacolumn("longurl", typeof(string)), new datacolumn("creatingdate",typeof(string)) }); dt2.rows.add(1, "john hammond", "united states"); dt2.rows.add(2, "mudassar khan", "india"); dt2.rows.add(3, "suzanne mathews", "france"); dt2.rows.add(4, "robert schidner", "russia"); exporttoexcel(dt1);// fail :( exporttoexcel(dt2);// success! }
that's cause of condition if (!page.ispostback)
, because of in button click event when postback happening not re-loading or populating dt1
, since it's new page request new page instance dt1
not present more. remove condition altogether , test
private datatable dt1; protected void page_load(object sender, eventargs e) { dt1 = new datatable(); dt.columns.addrange(new datacolumn[3] { new datacolumn("shorturl", typeof(int)), new datacolumn("longurl", typeof(string)), new datacolumn("creatingdate",typeof(string)) }); dt.rows.add(1, "john hammond", "united states"); dt.rows.add(2, "mudassar khan", "india"); dt.rows.add(3, "suzanne mathews", "france"); dt.rows.add(4, "robert schidner", "russia"); }
(or) store datatable session
, re-use like
session["dt"] = dt1; if(session["dt"] != null) exporttoexcel((datatable)session["dt"]);
Comments
Post a Comment