Im pretty new to VB, using .Net 2008, and I am saving two DataGrids, ComboBoxes, and Textbox to a txt file, using "*99*" as a terminator between each, but I am having trouble reading it back into each field. Heres my open file code.

Private Sub openbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openbtn.Click
        Dim openFileDialog1 As New OpenFileDialog()
        openFileDialog1.Filter = "Text File|*.txt|Word Document|*.doc"
        openFileDialog1.Title = "Open File..."
        openFileDialog1.ShowDialog()

        If Windows.Forms.DialogResult.OK Then
            ' Dim strFileName As String
            Clear()
            notebox.Text = ""

            Dim myStream As System.IO.StreamReader = New System.IO.StreamReader(openFileDialog1.FileName)

            Do Until myStream.ReadLine() = "*99*"
                TestdbDataSet.Table1.ReadXml(myStream)
            Loop

            Do Until myStream.ReadLine() = "*99*"
                TestdbDataSet.Table21.ReadXml(myStream)
            Loop

            Do Until myStream.ReadLine() = "*99*"
                srcdrop1.Items.Add(CStr(myStream.ReadLine))
            Loop

            Do Until myStream.ReadLine() = "*99*"
                srcdrop2.Items.Add(CStr(myStream.ReadLine))
            Loop


            notebox.Text = CStr(myStream.ReadLine)

            myStream.Close()
            myStream = Nothing

        End If
    End Sub

Recommended Answers

All 4 Replies

If everything is delimited by "*99*"
try the following

Dim sData() As String
sData = myStream.ReadToEnd.Split("*99*")
'Then set each table as you want 
'for example
Dim oStringReader As New System.IO.StringReader(sData(0))
TestdbDataSet.ReadXml(oStringReader)
commented: Thanks for your help +2

Thanks a lot for your response. I tried the Split, and now it is actually reading the first Table correctly, but when it gets to the Xml for the second it says that the root element is missing. So I tried to peek around the *99*, because I thought maybe it was trying to start reading the Xml there, but it still didnt work. Heres my new code.

Dim openFileDialog1 As New OpenFileDialog()
        openFileDialog1.Filter = "Text File|QAPROJECT*.txt|Word Document|*.doc"
        openFileDialog1.Title = "Open File..."
        Dim t1() As String
        Dim t2() As String


        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            Dim myStream As System.IO.StreamReader = New System.IO.StreamReader(openFileDialog1.FileName)
            Table2TableAdapter.Connection.Open()
            t1 = myStream.ReadToEnd.Split("*99*")
            myStream.Peek()
            myStream.Peek()
            myStream.Peek()
            myStream.Peek()
            t2 = myStream.ReadToEnd.Split("*99*")
            myStream.Peek()
            myStream.Peek()
            myStream.Peek()
            myStream.Peek()
            notebox.Text = myStream.ReadToEnd()

            Dim lStringReader As New System.IO.StringReader(t1(0))
            Dim mStringReader As New System.IO.StringReader(t2(0))
            TestdbDataSet.Table1.ReadXml(lStringReader)
            TestdbDataSet.Table2.ReadXml(mStringReader)

            Table1TableAdapter.Update(TestdbDataSet.Table1)
            Table2TableAdapter.Update(TestdbDataSet.Table2)
            Table2TableAdapter.Connection.Close()
            srcdrop2.Items.Add(System.IO.Path.GetDirectoryName(openFileDialog1.FileName) & "\" & System.IO.Path.GetFileName(openFileDialog1.FileName))
            srcdrop2.SelectedIndex = 0
            myStream.Close()
        End If
        StatusUp()

You shouldn't be reading to the end every time; only the first.
When you call split on the string returned from the stream you should close the stream and use the t1 variable to access the data.
Continuing to call the read to end wouldn't return anything since your at the end of the stream

Ohh ok I get it now. So it splits it into the same array, for some reason I thought that it was stopping the reader there. That got it working for me, Thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.