Hi Here is another test piece of code to try. The changes are very minimal from the first version. This is only testing the Integer and Decimal aspects of the data - the overall loop limits is a separate issue and not changed in this version. (NOTE: the code input in the forum is completely broken and I can not be bothered to try and find a workaround) Option Strict On Option Explicit On Public Class Form1 Dim lines As New List(Of String) Dim linestoremove As New List(Of String) Dim dt As New DataTable("Freddy") Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load With dt .Columns.Add("Village", GetType(String)) .Columns.Add("Grower", GetType(String)) .Columns.Add("Adhar", GetType(String)) .Columns.Add("Mobile", GetType(String)) End With DataGridView1.DataSource = dt lines = IO.File.ReadAllLines(LabelFile.Text).ToList For i As Integer = 0 To 10 ' NOT a valid idea !!!! Dim f1 As String = FindData("Village Code / Name(6/7)") Dim f2 As String = FindData("Grower Code / Name(8)") Dim f3 As String = FindData("Adhar Number(14)") Dim f4 As String = FindData("Mobile Number(13)") If f1.Trim.Length > 0 Then dt.Rows.Add(f1, f2, f3, f4) For Each line As String In linestoremove lines.Remove(line) Next End If Next End Sub Private Function FindData(s As String) As String Dim allowed() As Char = "1234567890.".ToCharArray Dim fld As String = String.Empty For Each line As String In lines Dim st As Integer = line.IndexOf(s) If st > -1 Then Dim num As String = String.Empty For i As Integer = st + s.Length To line.Length - 1 Dim ch As Char = line(i) If allowed.Contains(ch) Then num &= ch Else If IsNumeric(num) Then linestoremove.Add(line) Return num End If End If Next End If Next Return String.Empty End Function End Class
How To Read Specific Lines of String From Notepad(Text File) and Add Specific Content in Data Table
Hello All, I am trying to read a text file in visual basic. The problem is contains has no delimiter. and only start lines data is added my data table. I am a Beginner and doing this task for my own work. I am not a professional. I learned Visual Basic from many online platform like this and figure out my daily works. I am Attaching File and Screenshot also, hope someone help to figure out. I hope you All great minds help me in this task. I will be highly obliged to you. The Text File is DummyKh.txt
Regards
The Code I am Trying is below
Private Sub FillData()
Try
For Each line As String In IO.File.ReadAllLines(LabelFile.Text)
If line.StartsWith("(1) |Village Code / Name(6/7)") Or line.Contains("|Grower Code / Name(8)") Or line.StartsWith("|Mobile Number(13)") Then
'Or line.Contains("|Grower Code / Name(8)") Or line.StartsWith("|Mobile Number(13)") Or line.Contains("|Adhar Number(14)") Then
Dim l2 As String = line.Replace("/", String.Empty)
Dim VillCode As String = String.Empty
Dim GCode As String = String.Empty
Dim VILL As Integer = l2.IndexOf(StrDup(16, " "), 1)
l2 = l2.Substring(32, VILL)
VillCode = Trim(LTrim(l2)).Substring(0, 5)
NR(0) = VillCode
dt.Rows.Add(NR)
dt.AcceptChanges()
NR = dt.NewRow
End If
Next
Dgv_Khatauni.DataSource = dt
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
2 additional answers
Sort by: Most helpful
-
Anonymous
2024-02-17T06:03:55.01+00:00 Hi
==== EDIT: I can't be bothered to try and reformat the answer to suit this dumb forum that wants to do nothing but cause user problems. This code block malfunction is a regression, and may never be fixed. ====
Here is an alternative possibility. Please note: parsing a particular file is fraught with possible errors/problems/exceptions and any other fault you can think of. For example, breaking out of loops is so dependant on known factors - such as number of records and/or many other possibilities that errors are very possible. This is a stand alone example. It works with the test data file, but will likely fail with any dataset that is not EXACTLY the same format. Anyway, if you want to try my example, start a new project and add a DataGridView1 and a Label named LabelFile in the Designer. Copy/replace existing code with the code below.
Option Strict On Option Explicit On Public Class Form1 Dim lines As New List(Of String) Dim linestoremove As New List(Of String) Dim dt As New DataTable("Freddy") Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load With dt .Columns.Add("Village", GetType(String)) .Columns.Add("Grower", GetType(String)) .Columns.Add("Adhar", GetType(String)) .Columns.Add("Mobile", GetType(String)) End With DataGridView1.DataSource = dt lines = IO.File.ReadAllLines(LabelFile.Text).ToList For i As Integer = 0 To 10 ' NOT a valid idea !!!! Dim f1 As String = FindData("Village Code / Name(6/7)") Dim f2 As String = FindData("Grower Code / Name(8)") Dim f3 As String = FindData("Adhar Number(14)") Dim f4 As String = FindData("Mobile Number(13)") If f1.Trim.Length > 0 Then dt.Rows.Add(f1, f2, f3, f4) For Each line As String In linestoremove lines.Remove(line) Next End If Next End Sub Private Function FindData(s As String) As String Dim fld As String = String.Empty For Each line As String In lines Dim st As Integer = line.IndexOf(s) If st > -1 Then Dim num As String = String.Empty For i As Integer = st + s.Length To line.Length - 1 Dim ch As Char = line(i) If IsNumeric(ch) Then num &= ch Else If IsNumeric(num) Then linestoremove.Add(line) Return num End If End If Next End If Next Return String.Empty End Function End Class
-
Ahamed Musthafa Careem 461 Reputation points
2024-02-17T02:07:58.9466667+00:00 Hi @Amit kumar Yadava Try running the below code and see whether this will resolve or not
- I've added Dim dt As New DataTable to create a new DataTable. I've defined a column "Village Code" for the DataTable. Instead of manipulating the line directly, I split the line into parts using Split("|"c) to get the village code. I've trimmed the village code before adding it to the DataTable.
- I've set the DataSource of the DataGridView to the populated DataTable.
Private Sub FillData()
Try
Dim dt As New DataTable
dt.Columns.Add("Village Code")
Dim lines = IO.File.ReadAllLines(LabelFile.Text)
For Each line As String In lines
If line.StartsWith("|Village Code / Name(6/7)") Then
Dim parts = line.Split("|"c)
Dim villagePart = parts(1)
Dim villageCode = villagePart.Substring(0, villagePart.IndexOf("/"))
dt.Rows.Add(villageCode.Trim())
End If
Next
Dgv_Khatauni.DataSource = dt
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub