c# - Attempted to read or write protected memory. This is often an indication that other memory is corrupt. when using OpenFileDialog -
i using open file dialog dragged toolbox onto form. file dialog working fine until recently.
i've searched high , low answer online none of them work. started project on home computer , copied whole project folder work computer can work on both @ home , @ work.
the file dialog works fine on work computer after make changes code (at work) , copy new project folder home computer issue occurs. not managed in tfs or other shared location. private project working on. code issue occurs below:
thread th; private void btnuploadtodb_click(object sender, eventargs e) { try { openfiledialog1.title = "select new data file"; openfiledialog1.filter = "excel files |*.xlsx"; if (openfiledialog1.showdialog() == dialogresult.ok) { string filepath = openfiledialog1.filename; } else if (dialogresult == dialogresult.none) { return; } th = new thread(uploadtodb); control.checkforillegalcrossthreadcalls = false; th.start(); } catch (exception ex) { messagebox.show(ex.message); } { gc.collect(); } }
the stack trace when error occurs below:
system.accessviolationexception unhandled message=attempted read or write protected memory. indication other memory corrupt. source=system.windows.forms stacktrace: @ system.windows.forms.filedialognative.ifiledialog.show(intptr parent) @ system.windows.forms.filedialog.rundialogvista(intptr hwndowner) @ system.windows.forms.filedialog.rundialog(intptr hwndowner) @ system.windows.forms.commondialog.showdialog(iwin32window owner) @ system.windows.forms.commondialog.showdialog() @ form1.form1.btnuploaddata_click(object sender, eventargs e) in c:\users\user1\documents\visual studio 2010\projects\c#_projects\c# projects\solution 1\solution 1\form1.cs:line 458 @ system.windows.forms.control.onclick(eventargs e) @ system.windows.forms.button.onclick(eventargs e) @ system.windows.forms.button.onmouseup(mouseeventargs mevent) @ system.windows.forms.control.wmmouseup(message& m, mousebuttons button, int32 clicks) @ system.windows.forms.control.wndproc(message& m) @ system.windows.forms.buttonbase.wndproc(message& m) @ system.windows.forms.button.wndproc(message& m) @ system.windows.forms.control.controlnativewindow.onmessage(message& m) @ system.windows.forms.control.controlnativewindow.wndproc(message& m) @ system.windows.forms.nativewindow.debuggablecallback(intptr hwnd, int32 msg, intptr wparam, intptr lparam) @ system.windows.forms.unsafenativemethods.dispatchmessagew(msg& msg) @ system.windows.forms.application.componentmanager.system.windows.forms.unsafenativemethods.imsocomponentmanager.fpushmessageloop(intptr dwcomponentid, int32 reason, int32 pvloopdata) @ system.windows.forms.application.threadcontext.runmessageloopinner(int32 reason, applicationcontext context) @ system.windows.forms.application.threadcontext.runmessageloop(int32 reason, applicationcontext context) @ system.windows.forms.application.run(form mainform) @ form1.program.main() in c:\users\user1\documents\visual studio 2010\projects\c#_projects\c# projects\solution 1\solution 1\program.cs:line 18 @ system.appdomain._nexecuteassembly(runtimeassembly assembly, string[] args) @ system.appdomain.executeassembly(string assemblyfile, evidence assemblysecurity, string[] args) @ microsoft.visualstudio.hostingprocess.hostproc.runusersassembly() @ system.threading.threadhelper.threadstart_context(object state) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state, boolean ignoresyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state) @ system.threading.threadhelper.threadstart() innerexception:
further, tried creating empty project on home computer issue occurs , added openfiledialog in same manner , works fine. issue seems application , when on home machine.
i adding updatetodb method requested. have modified values there might lines don't make sense, however, believe not relevant issue @ hand.
private void uploadtodb() { try { string values = ""; string excelconstring = @"provider=microsoft.ace.oledb.12.0;data source='" + filepath + "'; extended properties='excel 12.0 xml;hdr=yes;imex=1;'"; string excelsql = "select * [sheet1$]"; string accessconstring = globaloledbcommands.dbconstring; string accesssql = "delete table1"; globaloledbcommands.commandexecutenonquery(accesssql, accessconstring); using (oledbdataadapter adap = new oledbdataadapter(excelsql, excelconstring)) { using (datatable dt = new datatable()) { adap.fill(dt); using (oledbconnection accesscon = new oledbconnection(accessconstring)) { accesscon.open(); (int = 0; < dt.rows.count - 1; i++) { (int j = 0; j <= dt.columns.count - 1; j++) { values = values.trim() + dt.rows[i][j] + ","; } values = strings.left(values, (strings.len(values) - 1)); string value1 = splitandreturntext(values, ",", 0); string value2 = splitandreturntext(values, ",", 1); string value3 = splitandreturntext(values, ",", 2); string value4 = splitandreturntext(values, ",", 3); accesssql = "insert table1 (value1,value2,value3,value4) values(@value1,@value2,@value3,@value4)"; using (oledbcommand dttoaccesscmd = new oledbcommand(accesssql, accesscon)) { dttoaccesscmd.parameters.addwithvalue("@value1", value1); dttoaccesscmd.parameters.addwithvalue("@value2", value2); dttoaccesscmd.parameters.addwithvalue("@value3", system.threading.thread.currentthread.currentculture.textinfo.totitlecase(value3.tolower())); dttoaccesscmd.parameters.addwithvalue("@value4", value4); try { dttoaccesscmd.executenonquery(); } catch (exception) { } this.lblprogress.text = (i + "/" + ((dt.rows.count) - 1)).tostring(); this.lblprogress.refresh(); } // reset value variable null. values = ""; } } } } accesssql = "select max(end_time) table1 user_name='" + environment.username + "'"; datetime start_time = convert.todatetime(globaloledbcommands. commandexecutescalar(accesssql, globaloledbcommands.dbconstring)); datetime end_time = datetime.now; double total_time = (end_time - start_time).totalminutes; string fieldvalues = "start_time, end_time, user_name, task_name, total_time,task_count"; accesssql = "insert table1 (" + fieldvalues + ") values ('" + start_time + "','" + datetime.now + "','" + environment.username + "','update db','" + total_time + "','1')"; globaloledbcommands.commandexecutenonquery(accesssql, globaloledbcommands.dbconstring); messagebox.show("new data added "); this.lblprogress.text = ""; } catch (exception ex) { messagebox.show(ex.message); }
could please advise causing issue not able work on changes code @ home fear not work when transfer work machine.
to run code on deploy computer 1 of 2 things must occur 1) exact same version of net library must on both computers including updates. 2) must publish application on build computer , run setup.exe folder created on build computer on deploy computer.
you can't copy exe 1 computer another.
Comments
Post a Comment