ssis - C# code is failing - trying to get excel cell A3 value -
i running code value fro cell a3 , giving me error .
i using ssis excel cell a3 , want show on message box later on going store in variable.
error: system.reflection.targetinvocationexception: exception has been thrown target of invocation. ---> system.invalidoperationexception: executereader requires open , available connection. connection's current state closed. @ system.data.oledb.oledbconnection.checkstateopen(string method) @ system.data.oledb.oledbcommand.validateconnection(string method) @ system.data.oledb.oledbcommand.validateconnectionandtransaction(string method) @ system.data.oledb.oledbcommand.executereaderinternal(commandbehavior behavior, string method) @ system.data.oledb.oledbcommand.executereader(commandbehavior behavior) @ system.data.oledb.oledbcommand.executereader() @ st_863f36c5697844e3916f1142373f3d3a.csproj.scriptmain.main() --- end of inner exception stack trace --- @ system.runtimemethodhandle._invokemethodfast(object target, object[] arguments, signaturestruct& sig, methodattributes methodattributes, runtimetypehandle typeowner) @ system.runtimemethodhandle.invokemethodfast(object target, object[] arguments, signature sig, methodattributes methodattributes, runtimetypehandle typeowner) @ system.reflection.runtimemethodinfo.invoke(object obj, bindingflags invokeattr, binder binder, object[] parameters, cultureinfo culture, boolean skipvisibilitychecks) @ system.reflection.runtimemethodinfo.invoke(object obj, bindingflags invokeattr, binder binder, object[] parameters, cultureinfo culture) @ system.runtimetype.invokemember(string name, bindingflags bindingflags, binder binder, object target, object[] providedargs, parametermodifier[] modifiers, cultureinfo culture, string[] namedparams) @ system.type.invokemember(string name, bindingflags invokeattr, binder binder, object target, object[] args, cultureinfo culture) @ microsoft.sqlserver.dts.tasks.scripttask.vstataskscriptingengine.executescript()
here code
/* microsoft sql server integration services script task write scripts using microsoft visual c# 2008. scriptmain entry point class of script. */ using system; using system.data; using microsoft.sqlserver.dts.runtime; using system.windows.forms; using system.data.oledb; namespace st_863f36c5697844e3916f1142373f3d3a.csproj { [system.addin.addin("scriptmain", version = "1.0", publisher = "", description = "")] public partial class scriptmain : microsoft.sqlserver.dts.tasks.scripttask.vstartscriptobjectmodelbase { #region vsta generated code enum scriptresults { success = microsoft.sqlserver.dts.runtime.dtsexecresult.success, failure = microsoft.sqlserver.dts.runtime.dtsexecresult.failure }; #endregion /* execution engine calls method when task executes. access object model, use dts property. connections, variables, events, , logging features available members of dts property shown in following examples. reference variable, call dts.variables["mycasesensitivevariablename"].value; post log entry, call dts.log("this log text", 999, null); fire event, call dts.events.fireinformation(99, "test", "hit message", "", 0, true); use connections collection use following: connectionmanager cm = dts.connections.add("oledb"); cm.connectionstring = "data source=localhost;initial catalog=adventureworks;provider=sqlncli10;integrated security=sspi;auto translate=false;"; before returning method, set value of dts.taskresult indicate success or failure. open help, press f1. */ public void main() { string connectionstring = null; oledbconnection excelconnection = null; connectionstring = "provider=microsoft.ace.oledb.12.0;data source=" + "c:\\newfolder\\test.xlsx" + ";extended properties=\"excel 12.0;hdr=no;imex=1\";"; excelconnection = new oledbconnection(connectionstring); // currentable sheetname string strsql = "select * [" + "sheet1$" + "a3:a3]"; int icnt = 4; oledbcommand objcmd = new oledbcommand(strsql, excelconnection); //int endpos = 1; //int startpos = 0; //boolean startflag = true; //boolean flag = true; oledbdatareader objreader = objcmd.executereader(); int nullcount = 0; // messagebox.show(objreader.fieldcount.tostring()); try { // messagebox.show("before while"); while (objreader.read()) { // checking nulls there blank rows in between actual excel row data. getvalue(2) searches in b column of excel if (objreader.getvalue(2) == dbnull.value) { nullcount = nullcount + 1; // messagebox.show("null"); } if (objreader.getvalue(2) != dbnull.value) { // messagebox.show(objreader.getvalue(2).tostring()); icnt = icnt + 1; } } } catch (exception e) { messagebox.show(e.stacktrace.tostring()); } dts.taskresult = (int)scriptresults.success; } } }
try opening connection first
excelconnection.open()
Comments
Post a Comment