MbahAgis

Load (.txt) Files To ListBox

Apr 1st, 2020 (edited)
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.27 KB | None | 0 0
  1. 'link https://www.facebook.com/groups/programervbnetindonesia/permalink/10157513255228621
  2.  
  3. Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
  4.         'Open File Dialog
  5.         With OFD
  6.             .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
  7.             .Filter = "Text files |*.txt"
  8.             If .ShowDialog = 1 Then
  9.                 Using MyReader As New TextFieldParser(.FileName)
  10.                     MyReader.TextFieldType = FieldType.Delimited
  11.                     MyReader.SetDelimiters(",")
  12.                     While Not MyReader.EndOfData
  13.                         Try
  14.                             'show all data to listbox
  15.                             ListBox1.Items.AddRange(MyReader.ReadFields())
  16.                         Catch ex As Exception
  17.                             MsgBox(ex.ToString)
  18.                         End Try
  19.                     End While
  20.                 End Using
  21.  
  22.             End If
  23.         End With
  24.  
  25.         'insert the data from listbox to db
  26.         Dim constr As String = "Server=DESKTOP-26OLQLK\MSSQLSERVER01;Database=MYDB;" & _
  27.                                 "User ID=" & "ally" & ";Password=" & "1234567!" & ";" & _
  28.                                 "Pooling='False';Connection Timeout=30"
  29.         Using ConnImport As New SqlConnection(constr)
  30.             Try
  31.                 ConnImport.Open()
  32.  
  33.                 'fetch the data from listbox items
  34.                 For i = 6 To 11
  35.                     Using cmd As New SqlCommand("INSERT INTO dbo.Outlet_Data(store_ort_date,store_header_count, " & _
  36.                                                 "store_details_count,store_total_netsales,store_StoreID,store_storename) " & _
  37.                                                 "VALUES (@col1,@col2,@col3,@col4,@col5,@col6)", ConnImport)
  38.                         With cmd
  39.                             .Parameters.Clear()
  40.                             .Parameters.AddWithValue("@col" & i - 5, ListBox1.Items(i))
  41.                             .ExecuteNonQuery()
  42.                         End With
  43.                     End Using
  44.                 Next
  45.  
  46.                 ConnImport.Close()
  47.  
  48.             Catch ex As Exception
  49.                 MsgBox(ex.ToString)
  50.             End Try
  51.  
  52.         End Using
  53.     End Sub
Add Comment
Please, Sign In to add comment