Help with streamwriter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ukgrl_yr79@yahoo.com

    #1

    Help with streamwriter

    hi

    fairly new to vb.net . I have created a filesystem where a manager
    and co workers hve limted access.

    the manager can read members files. i have used streamreader to get
    the details from a text file on my c drive into relevent text boxes eg
    name, address by using arrays.


    what i need to know is , how do i make it possible for the manager to
    add a new record on the same page that will be appended to the same
    text file on my c drive. and them read bak from it.

    I also need to know how make my array bigger so say if there where
    more records added.

    any help gratfull- this is driving me mad

    this is my code.

    Public Class Form2
    Public BookArray As Book() 'Class file
    Public NumRex As Integer
    Dim Current As Integer

    Private Sub Form2_Load(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load

    Dim datafile As String = "C:\BooksFile.t xt"
    Dim oRead As New System.IO.Strea mReader(datafil e) ' Should
    check that the file exists
    Dim i As Integer = 0
    Dim linein As String
    Dim arr As String()

    'Find out how many lines we've got
    While oRead.Peek <-1
    linein = oRead.ReadLine( )
    i = i + 1
    'MsgBox(linein & " i = " & i)
    End While
    NumRex = i - 1
    'Redim ItemArray so it'll hold all the items
    ReDim BookArray(NumRe x)



    oRead.Close()
    oRead = New System.IO.Strea mReader(datafil e)

    For i = 0 To CastArray.Lengt h - 1
    'Grab a line
    linein = oRead.ReadLine( )
    ' Split the line at the commas and feed it into the arr
    array
    arr = linein.Split(", ")
    'Now feed the elements of arr into the fields of
    ItemArray(i)
    BookArray(i) = New Book
    BookArray(i).Bo okId = Int(arr(0))
    BookArray(i).Ti tle = arr(1)
    BookArray(i).Au thor = arr(2)
    BookArray(i).Pu blisher = arr(3)
    BookArray(i).Da tepublished = arr(4)



    Next i

    oRead.Close()

    txtBookId.Text = BookArray(0).Bo okId
    txtTitle.Text = BookArray(0).Ti tle
    txtAuthor.Text = BookArray(0).Au thor
    txtPublisher.Te xt = BookArray(0).Pu blisher
    txtDatePublishe d.Text = BookArray(0).Da tepublished
    Current = 0

    Private Sub btnLastRecord_C lick(ByVal sender As System.Object, ByVal
    e As System.EventArg s) Handles btnLastRecord.C lick

    txtBookId.Text = BookArray(NumRe x).BookId
    txtTitle.Text = BookArray(NumRe x).Title
    txtAuthor.Text = BookArray(NumRe x).Author
    txtPublisher.Te xt = BookArray(NumRe x).Publisher
    txtDatePublishe d.Text = BookArray(NumRe x).Datepublishe d
    Current = NumRex



    Private Sub btnNextRecord_C lick(ByVal sender As System.Object, ByVal
    e As System.EventArg s) Handles btnNextRecord.C lick
    If Current < NumRex Then
    Current = Current + 1
    txtBookId.Text = BookArray(Curre nt).BookId
    txtTitle.Text = BookArray(Curre nt).Title
    txtAuthor.Text = BookArray(Curre nt).Author
    txtPublisher.Te xt = BookArray(Curre nt).Publisher
    txtDatePublishe d.Text = BookArray(Curre nt).Datepublish ed


    End If
    End Sub

  • Branco Medeiros

    #2
    Re: Help with streamwriter

    pm wrote:
    <backposted/>

    To resize the array, you can either:

    a) use Redim Preserve:

    'assuming that NumRex has the new line count
    Redim Preserve BookArray(0 To NumRex-1)

    b) use some collection instead of an array. This way you don't need to
    pre-fetch the lines to properly dim the array previouly to actually
    filling it:

    <aircode>
    Dim Books As New List(Of Book)

    'In your Load event:
    Books.Clear
    Do While oRead.Peek <-1
    Dim Items() As String = oRead.ReadLine( ).Split(","c)
    Dim NewBook As New Book
    With NewBook
    .BookId = CInt(Items(0))
    .Title = Items(1)
    .Author = Items(2)
    .Publisher = Items(3)
    .Datepublished = Items(4)
    End With
    Books.Add(NewBo ok)
    Loop
    oRead.Close
    NumRex = Books.Count
    </aircode>

    Now, for adding a new record and saving it to the original file, you
    can either

    a) implement a "save all" functionality, where you'd overwrite the
    original file with the current lines taken from the books:

    <aircode>
    'Create a backup and recover from errors is left as an exercise
    Dim Out as New System.IO.Strea mWriter(STR_OUT FILE)
    For Each B As Book in Books
    Dim Line As String = String.Format(" {0},{1},{2},{3} ,{4}", _
    B.BookId, B.Title, B.Author, _
    B.Publisher, B.Datepublished )

    Out.WriteLine(L ine)
    Next
    Out.Close
    </aircode>

    b) Open the file for appending and write just the new book:

    <aircode>
    Sub AddBook(NewBook As Book)
    Books.Add(NewBo ok)
    NumRex = Books.Count
    Dim Out As Suystem.IO.Stre amWriter = _
    System.IO.File. AppendText(STR_ OUTFILE)

    Dim Line As String = String.Format(" {0},{1},{2},{3} ,{4}", _
    NewBook.BookId, NewBook.Title, _
    NewBook.Author, NewBook.Publish er, _
    NewBook.Datepub lished)

    Out.WriteLine(L ine)
    Out.Close
    End Sub
    </aircode>

    HTH.

    Regards,

    Branco.
    hi
    >
    fairly new to vb.net . I have created a filesystem where a manager
    and co workers hve limted access.
    >
    the manager can read members files. i have used streamreader to get
    the details from a text file on my c drive into relevent text boxes eg
    name, address by using arrays.
    >
    what i need to know is , how do i make it possible for the manager to
    add a new record on the same page that will be appended to the same
    text file on my c drive. and them read bak from it.
    >
    I also need to know how make my array bigger so say if there where
    more records added.
    >
    any help gratfull- this is driving me mad
    >
    this is my code.
    >
    Public Class Form2
    Public BookArray As Book() 'Class file
    Public NumRex As Integer
    Dim Current As Integer
    >
    Private Sub Form2_Load(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load
    >
    Dim datafile As String = "C:\BooksFile.t xt"
    Dim oRead As New System.IO.Strea mReader(datafil e) ' Should
    check that the file exists
    Dim i As Integer = 0
    Dim linein As String
    Dim arr As String()
    >
    'Find out how many lines we've got
    While oRead.Peek <-1
    linein = oRead.ReadLine( )
    i = i + 1
    'MsgBox(linein & " i = " & i)
    End While
    NumRex = i - 1
    'Redim ItemArray so it'll hold all the items
    ReDim BookArray(NumRe x)
    >
    oRead.Close()
    oRead = New System.IO.Strea mReader(datafil e)
    >
    For i = 0 To CastArray.Lengt h - 1
    'Grab a line
    linein = oRead.ReadLine( )
    ' Split the line at the commas and feed it into the arr
    array
    arr = linein.Split(", ")
    'Now feed the elements of arr into the fields of
    ItemArray(i)
    BookArray(i) = New Book
    BookArray(i).Bo okId = Int(arr(0))
    BookArray(i).Ti tle = arr(1)
    BookArray(i).Au thor = arr(2)
    BookArray(i).Pu blisher = arr(3)
    BookArray(i).Da tepublished = arr(4)
    >
    Next i
    >
    oRead.Close()
    >
    txtBookId.Text = BookArray(0).Bo okId
    txtTitle.Text = BookArray(0).Ti tle
    txtAuthor.Text = BookArray(0).Au thor
    txtPublisher.Te xt = BookArray(0).Pu blisher
    txtDatePublishe d.Text = BookArray(0).Da tepublished
    Current = 0
    >
    Private Sub btnLastRecord_C lick(ByVal sender As System.Object, ByVal
    e As System.EventArg s) Handles btnLastRecord.C lick
    >
    txtBookId.Text = BookArray(NumRe x).BookId
    txtTitle.Text = BookArray(NumRe x).Title
    txtAuthor.Text = BookArray(NumRe x).Author
    txtPublisher.Te xt = BookArray(NumRe x).Publisher
    txtDatePublishe d.Text = BookArray(NumRe x).Datepublishe d
    Current = NumRex
    >
    Private Sub btnNextRecord_C lick(ByVal sender As System.Object, ByVal
    e As System.EventArg s) Handles btnNextRecord.C lick
    If Current < NumRex Then
    Current = Current + 1
    txtBookId.Text = BookArray(Curre nt).BookId
    txtTitle.Text = BookArray(Curre nt).Title
    txtAuthor.Text = BookArray(Curre nt).Author
    txtPublisher.Te xt = BookArray(Curre nt).Publisher
    txtDatePublishe d.Text = BookArray(Curre nt).Datepublish ed
    >
    End If
    End Sub

    Comment

    Working...