943,587 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 703
  • VB.NET RSS
Dec 2nd, 2008
0

can someone help me with saving my data in a file program?

Expand Post »
I am writing a project that stores vehicle information such as the model,manuf., year, etc.
I have to create a second project that loads the data from the file into memory and loads a drop down combo box with the VIN numbers. When the number is selected from the box, it is supposed to display the appropriate information in labels. Im having problems storing my information and creating this project because the book doesn't explain it very well. Can anyone point me in the right direction?
Similar Threads
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
cassie_sanford is offline Offline
57 posts
since Oct 2008
Dec 2nd, 2008
0

Re: can someone help me with saving my data in a file program?

You didn't mention, how the information is stored. But if you have numbers in your combo box and you can fetch the information with that number, you need to trap SelectedIndexChanged for the combo box:
VB.NET Syntax (Toggle Plain Text)
  1. Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  2. ' Get selected VIN number
  3. Dim ThisVINNumber As Integer
  4.  
  5. Try
  6. ThisVINNumber = CInt(ComboBox1.Items(ComboBox1.SelectedIndex))
  7. ' Fetch information for this VIN and place it in the labels
  8. Catch ex As Exception
  9. ' Handle exception
  10. End Try
  11.  
  12. End Sub
Hope this gets you started. If you still have a problem, you need to tell more details about how you store your information (database, a file...)
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Dec 7th, 2008
0

Re: can someone help me with saving my data in a file program?

Since i'm storing my information in a file, do i need to make a .txt file first and then write the program? Also, Since i'm creating two projects under the same thing, how would i go about making my forms? My first idea was to have a form for the user to enter in the data and then have another form that loads that information from the .txt file. I have in mind what I want to do but I just dont know the right way to execute it.
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
cassie_sanford is offline Offline
57 posts
since Oct 2008
Dec 7th, 2008
0

Re: can someone help me with saving my data in a file program?

Here is the code that i have so far, but my .txt file won't execute:

Imports System.IO
Public Class createForm

    'Declare module level variables.
    Dim autoStreamWriter As New StreamWriter("Vehicles.txt")


    Private Sub saveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveButton.Click


        If modelTextBox.Text <> "" And manufacturerTextBox.Text <> "" And yearTextBox.Text <> "" And vinNumTextBox.Text <> "" Then
            autoStreamWriter = New StreamWriter("Vehicles.txt", True)
            autoStreamWriter.WriteLine(modelTextBox.Text)
            autoStreamWriter.WriteLine(manufacturerTextBox.Text)
            autoStreamWriter.WriteLine(yearTextBox.Text)
            autoStreamWriter.WriteLine(vinNumTextBox.Text)
            autoStreamWriter.Close()

        Else
            MessageBox.Show("All fields must be entered before the information can be saved.")

            

        End If
        With Me
            autoStreamWriter.WriteLine(modelTextBox.Text)
            autoStreamWriter.WriteLine(manufacturerTextBox.Text)
            autoStreamWriter.WriteLine(yearTextBox.Text)
            autoStreamWriter.WriteLine(vinNumTextBox.Text)
            With .modelTextBox
                .Clear()
                .Focus()

            End With
            .manufacturerTextBox.Clear()
            .yearTextBox.Clear()
            .vinNumTextBox.Clear()

        End With

    End Sub

    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
        'Close the file and the form.

        autoStreamWriter.Close()
        Me.Close()
    End Sub
End Class
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
cassie_sanford is offline Offline
57 posts
since Oct 2008
Dec 8th, 2008
0

Re: can someone help me with saving my data in a file program?

I think its a good idea to keep data entry and data viewing as separate forms.

Here's a bit fixing to data entry form (saveButton_Click). Replace control names (TextBox1 <- modelTextBox etc.) and add missing boxes.
VB.NET Syntax (Toggle Plain Text)
  1. ' Declare writer
  2. Private autoStreamWriter As StreamWriter
  3. ' Set file name as a constant value
  4. Const FileName As String = "C:\Vehicles.txt"
  5.  
  6. ' I use ValidateForm as a separate "form validator"
  7. Private Function ValidateForm() As Boolean
  8. '
  9. If TextBox1.Text.Length = 0 Then
  10. MessageBox.Show("Model is empty. All fields must be entered before the information can be saved.")
  11. TextBox1.Focus() ' Move focus to missing field
  12. Return False
  13. End If
  14. ' Next Textboxes here
  15. ' Validation is Ok
  16. Return True
  17.  
  18. End Function
  19.  
  20. ' Save button
  21. Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  22. '
  23. ' Check fields
  24. If Not ValidateForm() Then
  25. Exit Sub
  26. End If
  27.  
  28. Try
  29. ' Create writer
  30. autoStreamWriter = New StreamWriter(FileName, True)
  31. autoStreamWriter.WriteLine(TextBox1.Text)
  32. autoStreamWriter.WriteLine(TextBox2.Text)
  33. ' Add missing WriteLine... here
  34. autoStreamWriter.Close()
  35. TextBox1.Clear()
  36. TextBox2.Clear()
  37. ' Add missing Clear... here
  38. ' Set focus to Model box
  39. TextBox1.Focus()
  40. Catch ex As Exception
  41. ' Handle exception
  42. ' Can't open file etc. can occur, so trap exceptions
  43. End Try
  44.  
  45. End Sub
And here's the code to read data. You may create an Auto class object but I used structure and array to hold data. This code goes in your "View data"-form.
VB.NET Syntax (Toggle Plain Text)
  1. ' Data reader
  2. Private autoStreamReader As StreamReader
  3. Const FileName As String = "C:\Vehicles.txt"
  4.  
  5. ' Structure to hold information for a one Auto object
  6. Private Structure Auto
  7. Dim Model As String
  8. Dim Manufacturer As String
  9. Dim Year As String
  10. Dim VIN As Integer ' I did assume VIN is an integer number
  11. End Structure
  12.  
  13. ' Array of Auto objects
  14. Private Autos() As Auto
  15. ' Number of items
  16. Private AutoCount As Integer
  17.  
  18. ' This "Read Data"-button
  19. Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
  20. ' Add similar Try...Catch...End Try here too (file can be missing etc.)
  21. Dim i As Integer
  22.  
  23. ' Set count to -1 (no data)
  24. AutoCount = -1
  25. autoStreamReader = New StreamReader(FileName)
  26. ' Read the whole file
  27. Do Until autoStreamReader.EndOfStream
  28. ' Increase counter
  29. AutoCount += 1
  30. ' Redim array
  31. ReDim Preserve Autos(AutoCount)
  32. ' Read one Auto object
  33. Autos(AutoCount).Model = autoStreamReader.ReadLine
  34. Autos(AutoCount).Manufacturer = autoStreamReader.ReadLine
  35. Autos(AutoCount).Year = autoStreamReader.ReadLine
  36. Autos(AutoCount).VIN = CInt(autoStreamReader.ReadLine)
  37. Loop
  38. ' Fill a combo box with VINs
  39. For i = 0 To AutoCount
  40. ComboBox1.Items.Add(Autos(i).VIN)
  41. Next i
  42. '
  43. End Sub
  44.  
  45. ' A modified SelectedIndexChanged to select an Auto object and show its data
  46. Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  47. ' Get selected Auto object
  48. Dim ThisAuto As Auto
  49.  
  50. Try
  51. ThisAuto = Autos(ComboBox1.SelectedIndex)
  52. ' Place information in the labels
  53. Label1.Text = ThisAuto.Model
  54. ' and so on...
  55. Catch ex As Exception
  56. ' Handle exception
  57. End Try
  58.  
  59. End Sub
Hope this gets you started
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Dec 8th, 2008
0

Re: can someone help me with saving my data in a file program?

Since i'm using two forms, how would i get the streamwriter to appear first? My plan was to have the createForm (streamwriter) to have a menu strip so that when the user selects like Open Summary or something, the other form will display for the user to go through the records using the vin number combo box
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
cassie_sanford is offline Offline
57 posts
since Oct 2008
Dec 8th, 2008
0

Re: can someone help me with saving my data in a file program?

You mean the data entry form is the "main" form? You can set menu item to disabled (Enabled = False).

Or you don't even have to do that. If the user selects "Open Summary" (and the file is empty) the user gets an empty summary form.

Here's a fixed "Read Data"-button, which checks first that the file exists. It also closes the StreamReader, a minor bug in my previous posting:
VB.NET Syntax (Toggle Plain Text)
  1. Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
  2. '
  3. Dim i As Integer
  4.  
  5. AutoCount = -1
  6. If Not My.Computer.FileSystem.FileExists(FileName) Then
  7. Exit Sub
  8. End If
  9. Try
  10. autoStreamReader = New StreamReader(FileName)
  11. Do Until autoStreamReader.EndOfStream
  12. AutoCount += 1
  13. ReDim Preserve Autos(AutoCount)
  14. Autos(AutoCount).Model = autoStreamReader.ReadLine
  15. Autos(AutoCount).Manufacturer = autoStreamReader.ReadLine
  16. Autos(AutoCount).Year = autoStreamReader.ReadLine
  17. Autos(AutoCount).VIN = CInt(autoStreamReader.ReadLine)
  18. Loop
  19. Catch ex As Exception
  20. ' Handle exception
  21. Finally
  22. autoStreamReader.Close()
  23. End Try
  24. '
  25. For i = 0 To AutoCount
  26. ComboBox1.Items.Add(Autos(i).VIN)
  27. Next i
  28. '
  29. End Sub
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: deleting the records of the MS access table using VB.NET
Next Thread in VB.NET Forum Timeline: The ConnectionString property has not been initialized





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC