vb.net - import csv file data in datagridview and reimport if it already exists -


i new file handling using vb.net.please suggest me code check data before importing file whether existing data match file data or not per situation mentioned below.my data looks this:-

2008,20,
3,65,3.48
5,rocky,4.53
6,total,7.65

here,first line year,week,.second line onward product code,description , cost. have read first line , check if data corresponding year , week exists.if display message box whether want re import data or not. here code:-

    private const ext_filter string = "csv (comma delimited) (*.csv)|*.csv|all files (*.*)|*.*"  #region "opencsvfile"     private sub opencsvfile()         openfiledialog1.filter = ext_filter         openfiledialog1.filterindex = 1         openfiledialog1.filename = ""         openfiledialog1.title = ""         if me.openfiledialog1.showdialog(me) = windows.forms.dialogresult.ok             txtfilename.text = openfiledialog1.filename             'bulkinsert(openfiledialog1.filename)             dim split_path() string = openfiledialog1.filename.split("\")             lbl_file_name.text = getfilename(openfiledialog1.filename)              insertimportdata(openfiledialog1.filename)         end if     end sub #end region      private sub insertimportdata(byval file_name string)         dim sqlquery string = ""         dim num_record integer = 0         dim trans sqltransaction = nothing          opennewinstanceofconnection()         try             trans = connection.begintransaction()             dim cmd sqlcommand = connection.createcommand             cmd.connection = connection             cmd.transaction = trans             cmd.commandtype = commandtype.text              sqlquery = "truncate table tbl_product_weekly_costs"             cmd.commandtext = sqlquery             cmd.commandtimeout = 0             num_record = cmd.executenonquery()             '   addtolistview("delete data table", num_record)             'addtolistview("product price", "")              readlinebyline(cmd, file_name)              trans.commit()         catch ex sqlexception             try                 if not trans nothing                     trans.rollback()                 end if             catch ex1 exception                 messagebox.show("rollback record -" & ex.tostring())             end try             messagebox.show(ex.tostring())         catch ex exception             'addtolistview("not valid file", "")             messagebox.show("can not insert " & vbcrlf & "details: " & ex.tostring)                     closeopenconnection()         end try     end sub      private sub readlinebyline(byval cmd sqlcommand, byval file_name string)         dim line object = ""         dim file new system.io.streamreader(file_name)         dim file_imported_yn boolean = false         dim num_records integer = 0          while (file.peek() <> -1)             line = file.readline             dim data() string = line.trim.split(",")              if data.length = 3                 insertproductweeklycost(cmd, line)                 num_records = num_records + 1                 file_imported_yn = true             else                 '    addtolistview("not valid product price file", "")                 messagebox.show("not valid product weekly file.", "invalid file", messageboxbuttons.ok, messageboxicon.error)                 file_imported_yn = false                 exit while             end if         end while          if file_imported_yn             messagebox.show("successfully imported.", "valid file", messageboxbuttons.ok, messageboxicon.error)             'addtolistview("successfully imported", num_records.tostring)         end if     end sub 


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -