Results 1 to 8 of 8

Thread: Writing string into specifc index in already exisiting file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Writing string into specifc index in already exisiting file

    Hello, I was trying to insert the value from combo box to specific index in already existing text file by using stream reader and stream writer. First read the entire file and then write to specific index. However, It doesn't work. The format of text file: abc|1234|value from combo box Can someone give an idea how I should do this, Thanks.

    PS: I am getting error when I try to include my code in the post:
    Something went wrong!

    Sadly, you’ve reached a page that can’t be displayed.

    We’ve logged this action, so we are aware there is an issue!

    At this time, please hit your browser’s back button or simply close this page!

    The incident ID is: N/A.
    Event id: 412797217690

    [CODE]
    Using reader As StreamReader = New StreamReader("..\..\TextFile\users.txt", True)
    Dim amount As String = cboTop.SelectedItem

    While Not reader.EndOfStream
    Dim AmountTop As String() = reader.ReadLine.Split("|")
    amount = AmountTop(2)


    End While

    End Using

    Using writerTopUp As StreamWriter = New StreamWriter(
    Last edited by Shaggy Hiker; Jan 1st, 2018 at 10:54 AM.

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,758

    Re: Writing string into specifc index in already exisiting file

    Regarding your error, do you have the WYSIWYG edito enabled in your account settings? If so, turn it off.
    Name:  Capture.jpg
Views: 148
Size:  14.1 KB
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Writing string into specifc index in already exisiting file

    Your code doesn't really make sense as it is. You're reading text in line by line, making a change to the data you just read and then imm3ediately discarding it. When it comes time to write, you don't even have the modified data to write so you obviously can't write it.

    Inside your While loop, you read a line in and split it into an array and then modify that array. Once you've done that, you simply discard the array. You need to actually put it somewhere that you can access it again. The obvious choice is a collection. Each item you want to store is a String array so that collection should be a List(Of String()).

    Once you've finished reading and modifying the data, you can then loop through that collection and recombine each array and write it as a line to the file.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Writing string into specifc index in already exisiting file

    What I want to create is a system where the user is filling the registration form once everything is correct and he presses the button, the next form will appear with credit card details and the amount he wants to top up. The username and password is going to be stored in text file in format username|password|money. Then user can log into their account and add more balance.


    By using jmcilhinney suggestion with a list (of strings) () read last and split the last line, and then insert the money. However, when user login to their account they have to top their account but this time, the login|password|money could at the very first line.

    Could anyone would give a hint what would be the best way of implementing this system, I have registration part done I just need to get an idea of how to finish the rest. I would really appreciate any suggestions/ Thank you.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Writing string into specifc index in already exisiting file

    For everybodies benefit: That initial post can't be edited, nor is it complete. It isn't even what Bloody posted. Any attempt to fix it gives an error. Therefore, I think that a new post will be needed to see any code. I tried closing the CODE tag, but that just ended up deleting the last few lines of the post and doing nothing else useful. It's one of those Something Went Wrong issues.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Writing string into specifc index in already exisiting file

    Please close the thread I'll create a new one. Thank you.

  7. #7
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Writing string into specifc index in already exisiting file

    Hi

    here s small sample with a Textfile to a DGV with a Combobox added to DGV
    you can change whatever line and the save

    create a Text file with the Format
    1;Chris;Mr
    2;Tom;Mr
    3;Betty;Mrs
    4;Howard;Dr.
    etc...

    Code:
    Imports System.IO
    Imports System.Text
    
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            With DataGridView1
                .Columns.Add("Column1", "Column1")
                .Columns.Add("Column2", "Column2")
                .Columns.Add(CreateComboBoxColumn) 'the Combobox
    
            End With
            'Load the Textfile
           
          
            For Each line As String In System.IO.File.ReadAllLines("C:\myDGVText.txt")
                DataGridView1.Rows.Add(line.Split(";"))
            Next
    
        End Sub
    
        Private Function CreateComboBoxColumn() As DataGridViewComboBoxColumn
            Dim column As New DataGridViewComboBoxColumn()
            With column
                .Name = "Title"
                .HeaderText = .Name
                .Items.AddRange(New String() {"Mr", "Mrs", "Dr."})
                .FlatStyle = FlatStyle.Flat
            End With
            Return column
        End Function
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Save the Datagrid, and show the result
            Dim filePath As String = "C:\myDGVText.txt"
            Dim delimeter As String = ";"
            Dim sb As New StringBuilder
            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                Dim array As String() = New String(DataGridView1.Columns.Count - 1) {}
    
                'this is for the Header Text:
                'If i.Equals(0) Then
                'For j As Integer = 0 To DataGridView1.Columns.Count - 1
                '    array(j) = DataGridView1.Columns(j).HeaderText
                'Next
                'sb.AppendLine(String.Join(delimeter, array))
                'End If
    
                For j As Integer = 0 To DataGridView1.Columns.Count - 1
                    If Not DataGridView1.Rows(i).IsNewRow Then
                        'you should use Value.ToString !
                        'using FormattedValue.ToSting will allow empty Cells
                        'to be saved
                        array(j) = DataGridView1(j, i).FormattedValue.ToString
                    End If
                Next
                If Not DataGridView1.Rows(i).IsNewRow Then
                    sb.AppendLine(String.Join(delimeter, array))
                End If
            Next
            File.WriteAllText(filePath, sb.ToString)
            'Opens the file immediately if you want to see it
            Process.Start(filePath)
        End Sub
    End Class
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Writing string into specifc index in already exisiting file

    This thread continues over here:

    http://www.vbforums.com/showthread.p...ting-text-file

    Normally, I'd merge the threads, but there has been a real issue with getting the proper code into this thread. It kept giving Something Went Wrong errors. Therefore, let's let this thread die and carry on the conversation in the other thread, where the code appears to be posting correctly.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width