1
votes

I need help. I want to check if user exists by entering their ic number and I want to display another rest of their data by using file in visual basic. Unfortunately, an error occurs while doing that. I need help. If the user exists, then It will display automatically name, email, address and so on but if a user doesn't exist, then it shows message box. Here I attached the image of the display screen and the code. Please help me. Thank you.

Public Class Form1

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        Dim userFile As String = "C:\Users\HP\Desktop\userdata.txt"
        Dim inputFile As String

        If System.IO.File.Exists(userFile) = True Then

            Dim objReader As New System.IO.StreamReader(userFile)
            Dim intIc As Integer
            Dim intCount As Integer = 0
            Dim strName As String
            Dim strEmail As String
            Dim intPhoneNum As String
            Dim strAdd1 As String
            Dim strAdd2 As String
            Dim intPostcode As String
            Dim strState As String

            Do While objReader.Peek() <> -1

                intIc(intCount) = Convert.ToInt64(objReader.ReadLine())
                If (intIc(intCount).Convert.ToInt64(objReader.ReadLine())) Then
                    strName(intCount) = objReader.ReadLine()
                    strEmail(intCount) = objReader.ReadLine()
                    intPhoneNum(intCount) = Convert.ToInt32(objReader.ReadLine())
                    strAdd1(intCount) = objReader.ReadLine()
                    strAdd2(intCount) = objReader.ReadLine()
                    intPostcode(intCount) = Convert.ToInt32(objReader.ReadLine())
                    strState(intCount) = objReader.ReadLine()

                    lblName.Text = strName
                    lblEmail.Text = strEmail
                    lblNum.Text = intPhoneNum
                    lblAdd1.Text = strAdd1
                    lblAdd2.Text = strAdd2
                    lblPostcode.Text = intPostcode
                    lblState.Text = strState
                    objReader.Close()
                Else
                    MessageBox.Show("User Does Not Exist")
                End If

                intCount = intCount + 1
            Loop
        Else

            MessageBox.Show("File Does Not Exist")

        End If
    End Sub
End Class

User Display Form

1
You need to include the error message and sample data from userdata.txt file.Honeyboy Wilson
how to do that?Namikaze
@Namikaze Click on 'edit' to start editing the question and remember to include all the exception details.41686d6564
Never post a question up on SO saying only "but I got an error" - always say exactly what the error is. To my mind it looks like the error is you declare a bunch of strings but give them no value, then later try to index them as though they are Char arrays setting the char at position (intCount) to a value. It won't ever work; strings are immutable and even if they were you can't assign a string to a char because it doesn't fit, unless the string is one char long, and its the wrong type (which vb might fudge for you, but it's still wrong).Caius Jard

1 Answers

0
votes

Your task, the easy way:

  • make a new project
  • add a DataSet to this new project
  • open the DataSet, in the properties call it something sensible
  • Right click the surface, add a new datatable, name it Person
  • Right click the datatable, add a column, name it IC. Right click, add column, name it Name. Keep going until you added all the fields you want to track(email,phone,address1 etc)
  • save the DataSet
  • open the form
  • show the datasources window (view menu.. other windows)
  • expand the nodes til you can see Person
  • click the drop down next to Person, switch from DataGridview to Details
  • drag Person onto the form. Text boxes, labels etc appear. In the tray at the bottom more things appear
  • add a textbox to the form and call it searchTextBox
  • add a search button to the form, double click it, add this line of code to the click handler:
    personBindingSource.Filter = '[ic] LIKE '" & searchTextBox.Text & "'"
    If personBindingSource.Count = 0 Then MessageBox.Show("No records")
  • double click the form background to add a form load event handler, put this line of code: If IO.File.Exists("data.xml") Then .ReadXml("data.xml")
  • switch back to designer, single click the form background and switch to event properties of the form, add a handler to the form closing event: .WriteXml("data.xml")

That's it, you now have a program that will open, read and fill the DataSet with data from the data.xml file, it will search it when you type something in the ic box, the text boxes use databinding to show values automatically, and when you close the program it will save updates data. The only task now is to load the xml file with data.

When the textboxes were added to the form you should also have seen a bar appear across the top with some left/right controls in and a green plus. Click the green plus, type some data in, click it again, type more data. Navigating back, if you're adding new data, will commit the data. If you're looking at existing data, editing it then navigating will commit it

After you added some data, you can search for existing data using the search box. When you've searched for a single value it should be the only thing shown and the nav will show "1 of 1". To get back to the mode where all data is showing, put a single asterisk in the search box and hit search; it should show the number records in the top bar and you can scroll them with the arrows.

If you already have lots of data in a file, like you use in your question, you can read it in a loop (like you do in your question, except don't use that code exactly cos it has loads of errors) as a one time thing and assign it into the datatable, or you can manipulate it directly into being XML in a text editor. This is easy to do if you have a capable text editor but I'll not offer any particular advice on it in case you don't have a large amount of existing data. Ask a new question if you do