asp.net - Imported Excel sheet not sorted normally -
below code import data excel sheet asp.net web page , saving @ sql server in database.
when import data required sheet not imported it's default sorting in asp web page or database, , affect badly on final result , lose data too. more clarification if first row in excel sheet having first id '1' , activities "new installation" , on...in asp web page , sql database may appear in ninth row , without full data
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.io; using system.data; using system.data.oledb; namespace impexpexc { public partial class default : system.web.ui.page { protected void page_load(object sender, eventargs e) { if (!ispostback) { populatedata(); lblmessage.text = "current database data!"; } } private void populatedata() { using (tedataentities dc = new tedataentities()) { gvdata.datasource = dc.importexportexcels.tolist(); gvdata.databind(); } } protected void btnimport_click(object sender, eventargs e) { if (fileupload1.postedfile.contenttype == "application/vnd.ms-excel" || fileupload1.postedfile.contenttype == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { try { string filename = path.combine(server.mappath("~/importdocument"), guid.newguid().tostring() + path.getextension(fileupload1.postedfile.filename)); fileupload1.postedfile.saveas(filename); string constring = ""; string ext = path.getextension(fileupload1.postedfile.filename); if (ext.tolower() == ".xls") { constring = "provider=microsoft.jet.oledb.4.0;data source=" + filename + ";extended properties=\"excel 8.0;hdr=yes;imex=2\""; } else if (ext.tolower() == ".xlsx") { constring = "provider=microsoft.ace.oledb.12.0;data source=" + filename + ";extended properties=\"excel 12.0;hdr=yes;imex=2\""; } string query = "select [id], [activities], [governorate], [exchange], [client], [technician], [circuitno], [comment], [date] [sheet1$]"; oledbconnection con = new oledbconnection(constring); if (con.state == system.data.connectionstate.closed) { con.open(); } oledbcommand cmd = new oledbcommand(query, con); oledbdataadapter da = new oledbdataadapter(cmd); dataset ds = new dataset(); da.fill(ds); da.dispose(); con.close(); con.dispose(); //import database using (tedataentities dc = new tedataentities()) { foreach (datarow dr in ds.tables[0].rows) { string empid = dr["id"].tostring(); var v = dc.importexportexcels.where(a => a.id.equals(empid)).firstordefault(); if (v != null) { //update here v.activities = dr["activities"].tostring(); v.governorate = dr["governorate"].tostring(); v.exchange = dr["exchange"].tostring(); v.client = dr["client"].tostring(); v.technician = dr["technician"].tostring(); v.circuitno = dr["circuitno"].tostring(); v.comment = dr["comment"].tostring(); v.date = dr["date"].tostring(); } else { //insert here dc.importexportexcels.addobject(new importexportexcel { id = dr["id"].tostring(), activities = dr["activities"].tostring(), exchange = dr["exchange"].tostring(), client = dr["client"].tostring(), technician = dr["technician"].tostring(), circuitno = dr["circuitno"].tostring(), comment = dr["comment"].tostring(), date = dr["date"].tostring() }); } } dc.savechanges(); } populatedata(); lblmessage.text = "successfully data import done!"; } catch (exception) { throw; } } } } }
finally issue of sorting solved adding additional column incremental id in database still of cells related date empty
Comments
Post a Comment