asp.net - Using SqlBulkCopy SqlRowsCopied to update a label -
i have simple web application reading records csv file , storing them in database table. using sqlbulkcopy
copy records sql database using batches. fine insert. trying give user feedback using onsqlrowscopied
, notifyafter
. goal update label contained in updatepanel
display number of records copied @ current notifyafter
interval. however, label not update until sqlbulkcopy
has complete. can see s_onsqlrowscopied
event firing using debug.writeline
. reason why label won't update , how can overcome this?
code behind
imports system.data.sqlclient public class webform1 inherits system.web.ui.page protected sub page_load(byval sender object, byval e system.eventargs) handles me.load end sub dim filepath string dim rowscopied string public sub btngetcsv_click(sender object, e eventargs) handles btngetcsv.click filepath = system.io.path.getfullpath(fileupload1.postedfile.filename) lblinfo.text = filepath end sub protected sub btntosql_click(sender object, e eventargs) handles btntosql.click dim cs string = system.web.configuration.webconfigurationmanager.connectionstrings("csmediaportal").connectionstring copydata(csvtodatatable(lblinfo.text.tostring()), cs) end sub private function csvtodatatable(filepath string) datatable dim dt datatable = nothing dim sourcepath string = string.empty dim csvfile string = string.empty dim constring string = string.empty dim conn oledb.oledbconnection = nothing dim adapter oledb.oledbdataadapter = nothing dim selstring string = string.empty try sourcepath = system.io.path.getdirectoryname(filepath) csvfile = system.io.path.getfilename(filepath) constring = "provider=microsoft.jet.oledb.4.0;data source=" & sourcepath & ";extended properties=""text;hdr=no;fmt=fixedlength""" conn = new oledb.oledbconnection(constring) selstring = "select * " & csvfile adapter = new oledb.oledbdataadapter(selstring, conn) dt = new datatable(system.io.path.getfilenamewithoutextension(filepath)) conn.open() adapter.fill(dt) conn.close() catch ex exception lblinfo.text = ex.message adapter.dispose() conn.dispose() end try return dt end function protected sub copydata(sourcetable datatable, cs string) using s sqlbulkcopy = new sqlbulkcopy(cs, sqlbulkcopyoptions.useinternaltransaction) s.destinationtablename = "test" s.batchsize = 1000 try addhandler s.sqlrowscopied, addressof s_onsqlrowscopied s.notifyafter = 900 s.writetoserver(sourcetable) catch ex exception directcast(directcast(httpcontext.current.handler, page).findcontrol("lblinfo"), label).text = "commit error: " & ex.message end try s.close() end using end sub protected sub s_onsqlrowscopied(sender object, e sqlrowscopiedeventargs) me.lblprogress.value = e.rowscopied.tostring() me.updatepanel1.update() debug.writeline(e.rowscopied) end sub end class
web form
<%@ page language="vb" codebehind="webform1.aspx.vb" inherits="csvupload.webform1" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:scriptmanager id="scriptmanager1" runat="server"> </asp:scriptmanager> <div> <asp:fileupload id="fileupload1" runat="server" /> <asp:button id="btngetcsv" runat="server" text="post" onclick="btngetcsv_click" /> <asp:label id="lblinfo" runat="server" text="label"></asp:label> </div> <asp:button id="btntosql" runat="server" text="insert sql" onclick="btntosql_click" /> <div> <asp:updatepanel id="updatepanel1" runat="server" updatemode="conditional"> <contenttemplate> <input runat="server" type="text" id="lblprogress" value="0" /> </contenttemplate> </asp:updatepanel> </div> </form> </body> </html>
Comments
Post a Comment