Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

problem with edit button

Posted on 2006-05-01
33
Medium Priority
?
286 Views
Last Modified: 2010-04-23
hello,
the edit button is not editing my data from the ini file correctly.
when you click on the edit buttom from main.vb you go there
and then you see in the textbox with the data ID and Name.
when you save its makes a new line in the .ini file and
thats the problem

ini file is like this
ID_Here;Name_Here

code

Public Class frmEdit

    Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
        frmMain.Enabled = True
        frmMain.Activate()
        Me.Close()
    End Sub

    Public Sub Populate(ByVal Record As Integer)
        On Error Resume Next
        Dim FileNo As Integer
        Dim strLine As String = ""
        FileNo = FreeFile
        FileOpen(FileNo, "C:\Accounts.ini", OpenMode.Input)
        Dim intRecord As Integer
        For intRecord = 0 To Record
            strLine = LineInput(FileNo)
        Next
        txtID.Text = Split(strLine, ";")(0)
        txtName.Text = Split(strLine, ";")(1)
        FileClose(FileNo)

    End Sub

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Dim FileNo As Short
        On Error Resume Next

        FileNo = FreeFile()
        FileOpen(FileNo, "C:\Accounts.ini", OpenMode.Append, , OpenShare.Shared)
        PrintLine(FileNo, txtID.Text & ";" & txtName.Text)
        FileClose(FileNo)

        frmMain.Enabled = True
        frmMain.Activate()
        Me.Close()
    End Sub
End Class
0
Comment
Question by:aplelois
  • 19
  • 9
  • 4
  • +1
33 Comments
 
LVL 5

Expert Comment

by:MageDribble
ID: 16579126
the problem is in your fileopen call

    FileOpen(FileNo, "C:\Accounts.ini", OpenMode.Append, , OpenShare.Shared)

this will append (add) a new line to the end of your file.  What you want to do is open it for reading, find the line that needs to be changed then overwrite that data.
0
 

Author Comment

by:aplelois
ID: 16579746
ok, how do I do that ?
0
 
LVL 5

Expert Comment

by:MageDribble
ID: 16579840
easiest way is to read the entire ini file into a string using filestream.  Alter the data you need to change.  Overwrite the original file with the string.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:aplelois
ID: 16579883
i'm not an expert of VB sorry but can you show me how to do it?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16582482
Imports System.IO

...


Dim reader As New StreamReader(fileName)
Dim fileText As String = reader.ReadToEnd()
Dim writer As New StreamWriter(fileName)
writer.Write(fileText.Replace("This text", "That text")
writer.Close()
reader.Close()

Bob
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16582692
Hi aplelois;

See if this works for you.

    Private Sub cmdSave_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        ' Open file for reading
        Dim sr As New StreamReader("C:\Accounts.ini")
        ' Get all the info from the file
        Dim input As String = sr.ReadToEnd()
        sr.Close()
        ' Set to true when record found
        Dim FoundRec As Boolean = False
        ' Replace all CrLf with a | character
        input = Regex.Replace(input, "(\r\n)", "|")
        Dim Lines() As String = input.Split("|")
        ' Used to access the arrPersons
        Dim idx As Integer
        ' Find the record to edit
        For idx = 0 To Lines.Length - 1
            Dim editline As String = Lines(idx)
            If editline.Substring(0, editline.IndexOf(";")).Trim = txtID.Text.Trim Then
                ' Record was found. Replace the data with new data
                Lines(idx) = txtID.Text.Trim & ";" & txtFirst.Text.Trim & ";" & _
                    txtLast.Text.Trim & ";" & txtEmail.Text.Trim & ";" & _
                    txtPhone.Text.Trim
                FoundRec = True
                Exit For
            End If
        Next
        ' If a modification was found write it out to INI file
        If FoundRec Then
            Dim sw As New StreamWriter("C:\Accounts.ini", False)
            input = String.Join(ControlChars.CrLf, Lines)
            sw.WriteLine(input)
            sw.Close()

            ' Get the record from the arrPersons ArrayList
            Dim p As frmMain.Persons = CType(frmMain.arrPersons(idx), frmMain.Persons)
            p.first = txtFirst.Text.Trim
            p.last = txtLast.Text.Trim
            p.email = txtEmail.Text.Trim
            p.phone = txtPhone.Text.Trim
            ' Update the ArrayList
            frmMain.arrPersons.Item(idx) = p
            ' Refresh the ListBox
            frmMain.lstAccounts.BeginUpdate()
            frmMain.lstAccounts.DataSource = Nothing
            frmMain.lstAccounts.Items.Clear()
            frmMain.lstAccounts.DisplayMember = "first"
            frmMain.lstAccounts.DataSource = frmMain.arrPersons
            frmMain.lstAccounts.EndUpdate()

        End If


        frmMain.Enabled = True
        frmMain.Activate()
        Me.Close()

    End Sub

Fernando
0
 

Author Comment

by:aplelois
ID: 16584416
Error      3      Type 'StreamWriter' is not defined.
Error      2      Name 'Regex' is not declared.
Error      4      'Populate' is not a member of 'Browser.frmEdit'.
0
 

Author Comment

by:aplelois
ID: 16584466
Only.
Error     3     Type 'StreamWriter' is not defined.
Error     2     Name 'Regex' is not declared.
0
 

Author Comment

by:aplelois
ID: 16584546
I added these two lines now I dont get errors
Imports System.IO
Imports System.Text.RegularExpressions

now I get this error
{"Length cannot be less than zero. Parameter name: length"}

whats this for
If editline.Substring(0, editline.IndexOf(";")).Trim = txtID.Text.Trim Then
0
 

Author Comment

by:aplelois
ID: 16584591
my original code is, sorry for the confusion!

        Next
        txtName.Text = Split(strLine, ";")(0)
        txtLast.Text = Split(strLine, ";")(1)
        txtPhone.Text = Split(strLine, ";")(2)
        FileClose(FileNo)

    End Sub

----
        FileNo = FreeFile()
        FileOpen(FileNo, "C:\Accounts.ini", OpenMode.Append, , OpenShare.Shared)
        PrintLine(FileNo, txtName.Text & ";" & txtLast.Text & ";" & txtPhone.Text)
        FileClose(FileNo)
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16585256
Hi aplelois;

Glad you found the needed Imports statements.

To your question: "whats this for"
If editline.Substring(0, editline.IndexOf(";")).Trim = txtID.Text.Trim Then

Answer:
If you are editing the info in the Accounts.ini file that means you are updating a record and not adding a record. We need to find the record to be updated and update the fields. The editline is the current in the Accounts.ini file being looked at. The Substring(0, editline.IndexOf(";")) picks the part of the string starting at position 0 and ending at the first ";" which should be the ID that matches the txtID.Text text box for the record to be edited. The .Trim remove any spaces before and after the string.

To your question: "now I get this error"
{"Length cannot be less than zero. Parameter name: length"}

Answer:
Need to know on what line you are getting this error on.

To your last post, Date: 05/02/2006 04:02AM PDT, I am not sure what your question is.

Fernando
0
 

Author Comment

by:aplelois
ID: 16585296
here
If editline.Substring(0, editline.IndexOf(";")).Trim = txtID.Text.Trim Then

{"Length cannot be less than zero. Parameter name: length"}
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16585430
Hi aplelois;

The most likely cause of this is that there is a blank line in the Accounts.ini or the split has placed a blank line into the Lines aray. You can add the If Not editline = "" Then statement as shown below to bypass blank lines.

            Dim editline As String = Lines(idx)
            If Not editline = "" Then
                If editline.Substring(0, editline.IndexOf(";")).Trim = txtID.Text.Trim Then
                    ' Record was found. Replace the data with new data
                    Lines(idx) = txtID.Text.Trim & ";" & txtFirst.Text.Trim & ";" & _
                        txtLast.Text.Trim & ";" & txtEmail.Text.Trim & ";" & _
                        txtPhone.Text.Trim
                    FoundRec = True
                    Exit For
                End If
            End If

Fernando
0
 

Author Comment

by:aplelois
ID: 16585569
Error      1      Variable 'editline' hides a variable in an enclosing block.


                ' Find the record to edit
                   For idx = 0 To Lines.Length - 1
 error           Dim editline As String = Lines(idx)
                     If editline = "" Then
                      Dim editline As String = Lines(idx)
                       If Not editline = "" Then
                        If editline.Substring(0, editline.IndexOf(";")).Trim = txtID.Text.Trim Then
                         ' Record was found. Replace the data with new data
                          Lines(idx) = txtName.Text.Trim & ";" & txtLast.Text.Trim & ";" & txtPhone.Text.Trim
                         FoundRec = True
                        Exit For
                    End If
                End If
            End If
0
 

Author Comment

by:aplelois
ID: 16585578
I dont have anything txtID
0
 

Author Comment

by:aplelois
ID: 16585585
everytime I edit a record it make a new line to the .ini file
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16588082
Hi aplelois;

This is the code you posted above at Date: 05/02/2006 06:17AM PDT. But this is not what I gave you. You do not need the Lines I marked.

                ' Find the record to edit
                   For idx = 0 To Lines.Length - 1

'==Do Not Need ===============================
error           Dim editline As String = Lines(idx)
                     If editline = "" Then
'========================================

                      Dim editline As String = Lines(idx)
                       If Not editline = "" Then
                        If editline.Substring(0, editline.IndexOf(";")).Trim = txtID.Text.Trim Then
                         ' Record was found. Replace the data with new data
                          Lines(idx) = txtName.Text.Trim & ";" & txtLast.Text.Trim & ";" & txtPhone.Text.Trim
                         FoundRec = True
                        Exit For
                    End If
                End If

'==Do Not Need ===============================
            End If
'==========================================

Now a couple of questions back I answered a question and you stated the format of the Accounts.ini file. Which if memory serves me right it was:

ID;First Name;Last Name;Email Address;Phone Number

Now to modify any given part of a line you must replace the whole line and not just the part that changes. So when you state in the following post, "I dont have anything txtID", How did you find the record you want to modify? This is why in my post which give the solution I had all the fields of the line and replace the old info in Lines array.

To the last post you had posted, if you coded it some what as I gave you then the whole file would have been re-written and therefore not added.

Fernando
0
 

Author Comment

by:aplelois
ID: 16588311
sorry, is not working and is adding a blank line at the end

my ini file is
Name;Last;Phone

My whole code

Imports System.IO
Imports System.Text.RegularExpressions

Public Class frmEdit

    Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
        frmMain.Enabled = True
        frmMain.Activate()
        Me.Close()
    End Sub

    Public Sub Populate(ByVal Record As Integer)
        On Error Resume Next
        Dim FileNo As Integer
        Dim strLine As String = ""
        FileNo = FreeFile
        FileOpen(FileNo, "C:\Accounts.ini", OpenMode.Input)
        Dim intRecord As Integer
        For intRecord = 0 To Record
            strLine = LineInput(FileNo)
        Next
        txtName.Text = Split(strLine, ";")(0)
        txtLast.Text = Split(strLine, ";")(1)
        txtPhone.Text = Split(strLine, ";")(2)
        FileClose(FileNo)

    End Sub

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click

        ' Open file for reading
        Dim sr As New StreamReader("C:\Accounts.ini")
        ' Get all the info from the file
        Dim input As String = sr.ReadToEnd()
        sr.Close()
        ' Set to true when record found
        Dim FoundRec As Boolean = False
        ' Replace all CrLf with a | character
        input = Regex.Replace(input, "(\r\n)", "|")
        Dim Lines() As String = input.Split("|")
        ' Used to access the arrPersons
        Dim idx As Integer
        ' Find the record to edit
        For idx = 0 To Lines.Length - 1
            Dim editline As String = Lines(idx)
            If Not editline = "" Then
                If editline.Substring(0, editline.IndexOf(";")).Trim = txtName.Text.Trim Then
                    ' Record was found. Replace the data with new data
                    Lines(idx) = txtName.Text.Trim & ";" & txtLast.Text.Trim & ";" & txtPhone.Text.Trim
                    FoundRec = True
                    Exit For
                End If
            End If
        Next
        ' If a modification was found write it out to INI file
        If FoundRec Then
            Dim sw As New StreamWriter("C:\Accounts.ini", False)
            input = String.Join(ControlChars.CrLf, Lines)
            sw.WriteLine(input)
            sw.Close()

            ' Get the record from the arrPersons ArrayList
            Dim p As frmMain.Persons = CType(frmMain.arrPersons(idx), frmMain.Persons)
            p.IntName = txtName.Text
            p.IntLast = txtLast.Text
            p.IntPhone = txtPhone.Text
            ' Update the ArrayList
            frmMain.arrPersons.Item(idx) = p
            ' Refresh the ListBox
            frmMain.lstAccounts.BeginUpdate()
            frmMain.lstAccounts.DataSource = Nothing
            frmMain.lstAccounts.Items.Clear()
            frmMain.lstAccounts.DisplayMember = "IntName"
            frmMain.lstAccounts.DataSource = frmMain.arrPersons
            frmMain.lstAccounts.EndUpdate()

        End If

        frmMain.Enabled = True
        frmMain.Activate()
        Me.Close()

    End Sub

End Class
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16588512
Questions that need to be answered.

1. Before the user click on the cmdSave button on the edit form
    what must they do? For example fill in text boxes or select an
    item from a list box.

2. What are the names of the controls the user modifies?

3. You state that the INI file is formatted as follows:
    Name;Last;Phone
    How do you select the record to modify from the above fields?

0
 

Author Comment

by:aplelois
ID: 16588611
the TextBoxes contain the information already!
he is just maybe changing from name1 to name2
or last2 to last1, cause when you click on edit you see:

Name: Bob
Last: Hiew
Phone: 12456
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16588687
1. Before the user click on the cmdSave button on the edit form
    what must they do? For example fill in text boxes or select an
    item from a list box.

the TextBoxes contain the information already!
he is just maybe changing from name1 to name2
or last2 to last1, cause when you click on edit you see:

Name: Bob
Last: Hiew
Phone: 12456

So I am assuming that all three Text box's are filled in befor the save button is click? True or False?

0
 

Author Comment

by:aplelois
ID: 16588712
True!
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 16589001
I have change one line in the code you have posted, from this :

If editline.Substring(0, editline.IndexOf(";")).Trim = txtName.Text.Trim Then

To this :

If editline.Substring(editline.LastIndexOf(";")+1).Trim = txtPhone.Text.Trim Then

I changes from looking for the persons name to telephone number because that is more likely to be unique then a name is. If you modify the Phone then no record will be modified or if the phone number is already in the INI file the wrong record will be modified. So be aware of that fact.

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click

        Dim sr As New StreamReader("C:\Accounts.ini")
        Dim input As String = sr.ReadToEnd()
        sr.Close()
        Dim FoundRec As Boolean = False
        input = Regex.Replace(input, "(\r\n)", "|")
        Dim Lines() As String = input.Split("|")
        Dim idx As Integer
        For idx = 0 To Lines.Length - 1
            Dim editline As String = Lines(idx)
            If Not editline = "" Then
                If editline.Substring(editline.LastIndexOf(";")+1).Trim = txtPhone.Text.Trim Then
                    Lines(idx) = txtName.Text.Trim & ";" & txtLast.Text.Trim & ";" & txtPhone.Text.Trim
                    FoundRec = True
                    Exit For
                End If
            End If
        Next
        If FoundRec Then
            Dim sw As New StreamWriter("C:\Accounts.ini", False)
            input = String.Join(ControlChars.CrLf, Lines)
            sw.WriteLine(input)
            sw.Close()

            ' Get the record from the arrPersons ArrayList
            Dim p As frmMain.Persons = CType(frmMain.arrPersons(idx), frmMain.Persons)
            p.IntName = txtName.Text
            p.IntLast = txtLast.Text
            p.IntPhone = txtPhone.Text
            frmMain.arrPersons.Item(idx) = p
            frmMain.lstAccounts.BeginUpdate()
            frmMain.lstAccounts.DataSource = Nothing
            frmMain.lstAccounts.Items.Clear()
            frmMain.lstAccounts.DisplayMember = "IntName"
            frmMain.lstAccounts.DataSource = frmMain.arrPersons
            frmMain.lstAccounts.EndUpdate()

        End If

        frmMain.Enabled = True
        frmMain.Activate()
        Me.Close()

    End Sub


Fernando
0
 

Author Comment

by:aplelois
ID: 16592055
Fernando, im still having trouble with your code. I meant edit to change from

Bob to Mike
Anderson to Piterson
123-123-123 to 456-456-456

but with your code im only allowed to change first name and last name!
is there anyway to make it work for everything ?
0
 

Author Comment

by:aplelois
ID: 16592134
check here to answer http:Q_21835753.html
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16595978
Hi aplelois;

You have a design problem as I had explained in one of my earlier post. The problem is you have an Accounts.ini file and you wish to modify one of the records. The three fields in the record are First Name; Last Name; Phone Number. As I see it you have two options. The first is to add another field to the Accounts.ini file this will unique identify the record within the file. This will also allow you to uniquely identify the row and to change any of the other fields in the record. The second is to place another set of text boxes on the edit form, one set will be used to locate the record in the Accounts.ini file and the other set to enter the information to update the record. I hope that this is of some help to correcting you issue.

Fernando
0
 

Author Comment

by:aplelois
ID: 16596138
thanks. do you think that making something like this will be a better way??

>>Accounts.ini

[Account_01]
Text_Name=Bob
Text_Last=Anderson
Text_Phone=123-123-1234

[Account_02]
Text_Name=Dan
Text_Last=Pit
Text_Phone=654-658-6548
0
 

Author Comment

by:aplelois
ID: 16596148
I just looking for the best way, it doesnt really have to be First Name; Last Name; Phone Number
0
 
LVL 5

Expert Comment

by:MageDribble
ID: 16596242
The ini format you have above would be fine however it takes 5 lines to put 1 account into the ini file which uses space fast!

The method that I've found to be most productive is the format below.  However, whichever way you find easiest to code is your best bet.

The format below is pipe delimited ( | ) and has Unique_ID, First Name, Last Name, Phone.  You can change it anyway you want but a unique ID is ideal to have.

Account_01=Bob|Anderson|123-123-1234
Account_02=Dan|Pit|654-658-6548
0
 

Author Comment

by:aplelois
ID: 16596279
ohh great, so what do you think FernandoSoto ? will it be easy to code?
lets move on into http:Q_21835753.html since this is closed already!
0
 
LVL 5

Expert Comment

by:MageDribble
ID: 16596355
aplelois,

i'd suggest that you go with whatever you find easiest to implement.  If you implement other people's ideas and suggestions it can be a maintanence nightmare if you don't fully understand what's going on.  However, if you implement your own ideas then you (ideally) will be able to maintain your own code and start pumping out code quickly and accurately.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16596603
I think that the way you had it before, on a different post, is the way to go.

UniqueID;FirstName;LastName;Email;Phone

The code I originally gave above, Date: 05/01/2006 07:37PM PDT, gave the solution for that format.

Fernando
0
 

Author Comment

by:aplelois
ID: 16597104
I have uploaded the source to a site so you can check it, here.
http://rapidshare.de/files/19529169/Accounts.zip.html
please help me!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question