reading a .txt file for data in VB .NET
-
- how to do i read data from .txt file - pass the data i.e (xx,yy,zz) into my vb .net program - display the data in tables
bizjosh wrote: how to do i read data from .txt file The File class. bizjosh wrote: pass the data i.e (xx,yy,zz) into my vb .net program If you know the file format, then you can use Convert.ToXXX to change strings to other types. bizjosh wrote: display the data in tables Put it in an arraylist and use a datagrid. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
-
- how to do i read data from .txt file - pass the data i.e (xx,yy,zz) into my vb .net program - display the data in tables
Dim reader as new System.IO.Streamreader(full file path) 'This is a serial reader, so once you iterate, you cannot go to the previous line unless you reopen the file and iterate to the patricular line number 'Say you want to read your data which is in the format xx,yy,zz Dim str as String Dim split_str() as String 'Dynamic String Array str = reader.readLine() 'Assuming your data is in the first line of the file split_str = str.split(',') 'You will get an array which made by tokenizing string using "," as the delimiter. Now split_str(0) = xx, split_str(1) = yy, split_str(2) = zz. You can further parse them to the required format that you want for further prcessing. 'Do not forget reader.close() Go to MSDN.com and verify the syntax for String.split(args[]). The context is the same but the delimiter might be a char or string or ASCII code, you might wanna verify that. I think that should help you get started on reading text files.
-
- how to do i read data from .txt file - pass the data i.e (xx,yy,zz) into my vb .net program - display the data in tables
I'm not sure i understand 100% what you are wanting to do, but if you want to read .txt (.csv, etc) file and place the data in a data grid, this is one way to do it. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim data_table As New DataTable("Contacts") ' Define the columns. data_table.Columns.Add("Last Name", GetType(String)) data_table.Columns.Add("First Name", GetType(String)) data_table.Columns.Add("Phone Number", GetType(String)) data_table.Columns.Add("Email Address", GetType(String)) ' Create some data. AddTableRow(data_table, "Jackson", "Jennie", "787-878-8263", "JennieJackson@nowhere.com") AddTableRow(data_table, "Kevlar", "Kurt", "872-348-7263", "kurtk@militant.com") AddTableRow(data_table, "Llama", "Linda", "", "lllama@weaver.com") AddTableRow(data_table, "Mandrake", "Marcus", "398-787-4764", "marcus@marcusrocks.com") ' Bind the DataTable to the DataGrid. dgContacts.DataSource = data_table End Sub Private Sub AddTableRow(ByVal data_table As DataTable, ByVal last_name As String, ByVal first_name As String, ByVal phone_number As String, ByVal email_address As String) Dim data_row As DataRow data_row = data_table.NewRow() data_row.Item("Last Name") = last_name data_row.Item("First Name") = first_name data_row.Item("Phone Number") = phone_number data_row.Item("Email Address") = email_address data_table.Rows.Add(data_row) End Sub If you are using a datagrid you have to give it some fieldname info and that is what the first section (Define the columns) is for. If this isn't what you are looking for, then there is a way you can use a SQL Text Driver and process your .txt file with an SQL table. Let me know if you think that would work better and I will show you that code.