Reputation: 19
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
Upvotes: 1
Views: 569
Reputation: 74700
Your task, the easy way:
personBindingSource.Filter = '[ic] LIKE '" & searchTextBox.Text & "'"
If personBindingSource.Count = 0 Then MessageBox.Show("No records")
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
Upvotes: 0