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

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 57
Reputation: cassie_sanford is an unknown quantity at this point 
Solved Threads: 0
cassie_sanford cassie_sanford is offline Offline
Junior Poster in Training

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

 
0
  #1
Dec 2nd, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

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

 
0
  #2
Dec 2nd, 2008
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:
  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...)
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 57
Reputation: cassie_sanford is an unknown quantity at this point 
Solved Threads: 0
cassie_sanford cassie_sanford is offline Offline
Junior Poster in Training

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

 
0
  #3
Dec 7th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 57
Reputation: cassie_sanford is an unknown quantity at this point 
Solved Threads: 0
cassie_sanford cassie_sanford is offline Offline
Junior Poster in Training

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

 
0
  #4
Dec 7th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

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

 
0
  #5
Dec 8th, 2008
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.
  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.
  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 57
Reputation: cassie_sanford is an unknown quantity at this point 
Solved Threads: 0
cassie_sanford cassie_sanford is offline Offline
Junior Poster in Training

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

 
0
  #6
Dec 8th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

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

 
0
  #7
Dec 8th, 2008
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:
  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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC