| | |
Read txt file into DataTable vb 2008
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2007
Posts: 45
Reputation:
Solved Threads: 1
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)
Private Sub excludebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles excludebtn.Click Dim openFileDialog1 As New OpenFileDialog() openFileDialog1.Filter = "Text File|*.txt|Excel Spreadsheet|*.xls" openFileDialog1.Title = "Open File..." Dim line() As String Dim J As Integer = 0 Dim K As Integer = 0 If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then Dim myStream As System.IO.StreamReader = New System.IO.StreamReader(openFileDialog1.FileName) line = myStream.ReadToEnd.Split("\S+") myStream.Close() Do While J < line.Count() Dim lStringReader As New System.IO.StringReader(line(J)) TestdbDataSet.ExcludeTable.Rows.Add(lStringReader) J = J + 1 Loop TestdbDataSet.ExcludeTable.AcceptChanges() Dim ex As exclude ex = New exclude ex.ShowDialog() ex.ExcludeTableDataGridView.PerformLayout() End If End Sub
•
•
Join Date: Jun 2007
Posts: 22
Reputation:
Solved Threads: 5
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:
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)
Row1Col1;Row1Col2;1003 Row2Col1;Row2Col2;2003 Row3Col1;Row3Col2;3003
VB.NET Syntax (Toggle Plain Text)
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim tbl As New DataTable("mytable") tbl.Columns.Add("col1", GetType(String)) tbl.Columns.Add("col2", GetType(String)) tbl.Columns.Add("col3", GetType(Integer)) Dim sFilename As String = "C:\TEMP\Dani\DataAccess\somedata.txt" Dim myStream As System.IO.StreamReader = New System.IO.StreamReader(sFilename) Dim line As String Dim aRow As DataRow Do line = myStream.ReadLine() If line Is Nothing Then Exit Do End If Dim sAry As String() = Split(line, ";") ' separate the fields of the current row aRow = tbl.NewRow 'get a DataRow that has the required structure aRow(0) = sAry(0) aRow(1) = sAry(1) aRow(2) = CInt(sAry(2)) tbl.Rows.Add(aRow) Loop myStream.Close() For Each aRow In tbl.Rows Console.WriteLine("{0} {1} {2}", aRow(0), aRow(1), aRow(2)) Next End Sub
![]() |
Other Threads in the VB.NET Forum
- Previous Thread: Hello
- Next Thread: Pass values to html table from datatable
Views: 10140 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
.net .net2005 30minutes 2005 2008 access account application arithmetic array arrays basic binary bing button buttons c# center check code combobox component connectionstring convert crystalreport data database databasesearch datagrid datagridview design dissertation dissertations dissertationthesis dll dropdownlist excel file-dialog folder ftp google hardcopy highlighting image images inline insert listview login mobile ms net networking output passingparameters peertopeervideostreaming picturebox picturebox1 plugin port print printing problem problemwithinstallation project reports" save savedialog searchbox serial server soap sorting sql string syntax table tcp text textbox timer toolbox trim update updown usercontrol vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web wpf





