c# - Importing Excel Data using Oledb -
i using following code display excel data datagrid using oledb. getting error fill: selectcommand.connection property has not been initialized.
can tell me did go wrong?
thanks
code:
using system; using system.data; using system.configuration; using system.web; using system.web.security; using system.web.ui; using system.web.ui.webcontrols; using system.web.ui.webcontrols.webparts; using system.web.ui.htmlcontrols; using system.data.sqlclient; using system.data.oledb; public partial class _default : system.web.ui.page { protected void page_load(object sender, eventargs e) { oledbconnection conn = new oledbconnection(@"provider=microsoft.ace.oledb.12.0;data source=d:\testing.xlsx;extended properties='excel 12.0 xml;hdr=yes;imex=1;'"); oledbcommand cmd = new oledbcommand("select * [sheet1$]"); if (conn.state == system.data.connectionstate.open) { conn.close(); } conn.open(); oledbdataadapter adap = new oledbdataadapter(cmd); dataset ds = new dataset(); adap.fill(ds); this.gridview1.datasource = ds.tables[0].defaultview; conn.close(); } }
you've neglected assign connection command object.
move below conn.open();
oledbcommand cmd = new oledbcommand("select * [sheet1$]", conn);
Comments
Post a Comment