| | |
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
| Thread Tools | Search this Thread |
.net .net2008 2008 access advanced application array basic beginner browser button buttons center click client code combo convert cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic eclipse excel exists fade filter forms function generatetags gridview html images input internet listview map mobile module monitor msaccess net number objects open panel pdf picturebox picturebox2 port position print printing problem read regex remove right-to-left save search searchvb.net select serial settings shutdown socket sqldatbase sqlserver survey temperature textbox timer timespan transparency txttoxmlconverter user usercontol vb vb.net vba vbnet visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web winforms winsock wpf wrapingcode xml year





