Read txt file into DataTable vb 2008

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2007
Posts: 45
Reputation: cellus205 is an unknown quantity at this point 
Solved Threads: 1
cellus205 cellus205 is offline Offline
Light Poster

Read txt file into DataTable vb 2008

 
0
  #1
Jun 5th, 2008
Hows it going everyone. Right now I am trying to read data from a text file into a datatable, and display the datatable in a new form. I am trying to use a string array to store the variables before adding them to the table. Everything compiles etc correctly, but when the new form with the datagrid that the table is bound to opens, there is nothing in the table.

  1. Private Sub excludebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles excludebtn.Click
  2. Dim openFileDialog1 As New OpenFileDialog()
  3. openFileDialog1.Filter = "Text File|*.txt|Excel Spreadsheet|*.xls"
  4. openFileDialog1.Title = "Open File..."
  5. Dim line() As String
  6.  
  7. Dim J As Integer = 0
  8. Dim K As Integer = 0
  9.  
  10. If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
  11.  
  12. Dim myStream As System.IO.StreamReader = New System.IO.StreamReader(openFileDialog1.FileName)
  13. line = myStream.ReadToEnd.Split("\S+")
  14.  
  15. myStream.Close()
  16. Do While J < line.Count()
  17. Dim lStringReader As New System.IO.StringReader(line(J))
  18. TestdbDataSet.ExcludeTable.Rows.Add(lStringReader)
  19. J = J + 1
  20. Loop
  21. TestdbDataSet.ExcludeTable.AcceptChanges()
  22.  
  23. Dim ex As exclude
  24. ex = New exclude
  25. ex.ShowDialog()
  26. ex.ExcludeTableDataGridView.PerformLayout()
  27.  
  28.  
  29.  
  30. End If
  31.  
  32.  
  33. End Sub
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 22
Reputation: dadelsen is an unknown quantity at this point 
Solved Threads: 5
dadelsen dadelsen is offline Offline
Newbie Poster

Re: Read txt file into DataTable vb 2008

 
0
  #2
Jun 6th, 2008
Maybe its possible but I never read code that used a stringreader to feed the DataRowCollections.Add Method. Typically a DataRow generated by the tables NewRow method or an array of objects is used as parameter.

If you read the datafields from a textfile and have other datatypes than "string" in the DataTable, you will need to convert these fields to the requested datatypes.

The example table has 2 String and 1 integer column, I used ";" as separator for the fields that make up one row.

Sample Datafile:
  1. Row1Col1;Row1Col2;1003
  2. Row2Col1;Row2Col2;2003
  3. Row3Col1;Row3Col2;3003

  1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  2. Dim tbl As New DataTable("mytable")
  3. tbl.Columns.Add("col1", GetType(String))
  4. tbl.Columns.Add("col2", GetType(String))
  5. tbl.Columns.Add("col3", GetType(Integer))
  6. Dim sFilename As String = "C:\TEMP\Dani\DataAccess\somedata.txt"
  7. Dim myStream As System.IO.StreamReader = New System.IO.StreamReader(sFilename)
  8. Dim line As String
  9. Dim aRow As DataRow
  10. Do
  11. line = myStream.ReadLine()
  12. If line Is Nothing Then
  13. Exit Do
  14. End If
  15.  
  16. Dim sAry As String() = Split(line, ";") ' separate the fields of the current row
  17. aRow = tbl.NewRow 'get a DataRow that has the required structure
  18. aRow(0) = sAry(0)
  19. aRow(1) = sAry(1)
  20. aRow(2) = CInt(sAry(2))
  21. tbl.Rows.Add(aRow)
  22. Loop
  23. myStream.Close()
  24. For Each aRow In tbl.Rows
  25. Console.WriteLine("{0} {1} {2}", aRow(0), aRow(1), aRow(2))
  26. Next
  27.  
  28. End Sub
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC