Click here to Skip to main content
15,888,089 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have a program in which the data is entered by the user in text-boxes and then on clicking save the program saves it in a binary format in a file. Here is the code for it :
Public Sub BinaryExport()
       Dim export As FileStream
       Try
           export = New FileStream(Application.StartupPath + "\read.dat", FileMode.Create)
       Catch e As Exception
           MsgBox(e.Message)
       End Try

       Dim binExport As BinaryWriter = New BinaryWriter(export)

       Try
           binExport.Write(TextBox1.Text)
           binExport.Write(TextBox2.Text)
           binExport.Write(TextBox3.Text)
           binExport.Close()
           MsgBox("Saved SuccessFully")
       Catch e1 As Exception
           MsgBox(e1.Message)
       End Try

   End Sub


How do I store multiple data inputs and read them using vb.net

I used the following code for reading but failed

Public Sub BinaryImport()
        Dim import As FileStream
        Try
            import = New FileStream(Application.StartupPath + "\read.dat", FileMode.Open)
        Catch e2 As Exception
            MsgBox("File NOT FOUND")
        End Try

        Dim BinaryReader As BinaryReader = New BinaryReader(import)

        Try
            TextBox1.Text = BinaryReader.ReadString
            TextBox2.Text = BinaryReader.ReadString
            TextBox3.Text = BinaryReader.ReadString
            BinaryReader.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

Please Help!
Posted
Updated 21-Apr-11 0:26am
v3
Comments
RaviRanjanKr 21-Apr-11 1:48am    
It works for me! What error you are getting please mention for better response. :)
rahuliyer95 21-Apr-11 6:20am    
I want to store multiple string inputs. If enter two different strings then all the text-boxes show the same data. If I try to save a new value on a previously stored one then it deletes the older one. I want to avoid it
OriginalGriff 21-Apr-11 6:35am    
No, I have read that five or six times and it still doesn't make any sense.
Please, try to give better detail and we will see if we can help.
Patrick Kalkman 21-Apr-11 7:06am    
You open the file with FileMode.Create this recreates the file every time. If you want to overwrite certain parts of the file you will have to open it with FileMode.Append. The file pointer will point to the end of the file though so you will have to seek to the appropriate place in the file.

1 solution

To add more data to your file you should change this line:
VB
export = New FileStream(Application.StartupPath + "\read.dat", FileMode.Create)

to:
VB
export = New FileStream(Application.StartupPath + "\read.dat", FileMode.Append)


This will create the file if it does not exist, otherwise it opens it and positions the write pointer at the end ready to add new data.

Hope this helps.
 
Share this answer
 
Comments
rahuliyer95 22-Apr-11 7:20am    
Thanks. But Now I am not able to read the newly written data. It only reads the old stored Data
Henry Minute 22-Apr-11 8:27am    
Firstly, just checking, the new line replaces the one in your BinaryExport() method.

Secondly, does the size of the file increase when you add new data? If so, the data is being added correctly. If what you really want to do is replace an old value with a new one, even though you indicate that you want to avoid that, you haven't explained your problem very well.

You start with three pieces of text (textBox1, 2 and 3). You save those. Your file now contains 3 pieces of data. You enter 3 new pieces of text into the text boxes and save that. Your file now contains 6 pieces of data (The first 3 followed by the second three). You then read in the first 3 and display them in your TextBoxes. You will get the first three that were saved, because they are the first three in your file.

You basically have two choices.
1. Continue saving new stuff at the end of the file but you will have to read and discard the first sets of data to get to the ones you want.
2. Use the FileStream.Position property (http://msdn.microsoft.com/en-us/library/system.io.filestream.position.aspx) to set the writing position to the start of the file before saving.
export = New FileStream(Application.StartupPath + "\read.dat", FileMode.Append)
export.Position = 0
This means that you will always get the newest data and if you want to see the old stuff you will have to read and discard newer stuff to get to it.
rahuliyer95 22-Apr-11 10:00am    
How do I come to know till which position is the file written
Henry Minute 22-Apr-11 10:01am    
Look at the link to the POSITION property I gave you earlier.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900