943,828 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 22104
  • VB.NET RSS
Jun 5th, 2008
0

Read txt file into DataTable vb 2008

Expand Post »
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.

VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
cellus205 is offline Offline
57 posts
since May 2007
Jun 6th, 2008
0

Re: Read txt file into DataTable vb 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:
VB.NET Syntax (Toggle Plain Text)
  1. Row1Col1;Row1Col2;1003
  2. Row2Col1;Row2Col2;2003
  3. Row3Col1;Row3Col2;3003

VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 25
Solved Threads: 5
Newbie Poster
dadelsen is offline Offline
22 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Hello
Next Thread in VB.NET Forum Timeline: Pass values to html table from datatable





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC