0

I am currently working on a Library Inventory System. I wanted it to be able to add, issue, search and delete book records. The records are stored in a text file which looks like this:

(ID), (Name Of Book); (Author); (Price) ; (Book Donor) ; (Language)

E.g.

123 , Journey to the center of the earth ; Jules Verne ; 50 ; Jack ; English
...

This program also works with a datagridview to update to/from the text file, so far my add and issue functions are working but i am having trouble with search and delete.

To search, the user must either enter the ID of the book in a textbox or the language of the book (separate textbox) and it must show the search results in the datagridview.

This is the my attempt for the search function:

    Dim line As String
    Dim fields(7) As String
    Dim found As Boolean = False
    Dim inputStream As StreamReader


    inputStream = File.OpenText("AddBookScreen.txt")
    line = inputStream.ReadLine()

    While (line <> Nothing) And found = False
        fields = Split(line, ",")
        If Trim(fields(0)) = SBidTextbox.Text Then
            SBdatagridview.Rows.Add(Trim(fields(1)), Trim(fields(2)), Trim(fields(3)), Trim(fields(4)), Trim(fields(5)), Trim(fields(6)))
            found = True
        Else
            line = inputStream.ReadLine()
        End If
    End While
    If Not found Then
        MessageBox.Show(SBidTextbox.Text & " not found")

    End If
    inputStream.Close()
End Sub

The delete function should be able to find the specific book by id and then delete that record from the text file.

This is my attempt for the delete function:

    Dim lines() As String
    Dim outputlines As New List(Of String)
    Dim searchString As String = DBidTextbox.Text
    lines = IO.File.ReadAllLines("AddBookScreen.txt")

    For Each line As String In lines
        If line.Contains(searchString) = False Then
            outputlines.Add(line)
        End If
    Next

The search and delete methods are meant to search for a specific book in the text file by either the ID of the book or the language and display the results in the datagridview, the delete method is to search for a specific record through ID and delete that record.

Could someone please guide me through these problems, your help would be much appreciated.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • What exactly is the trouble you're having? i.e. what are the search and delete methods doing? Also, this kind of data access is exactly what databases were invented for; if its possible, moving from a text file to a database will make your life much easier. – F. Stephen Q Sep 02 '15 at 18:01
  • are you re-reading the file over and over (and over)? You could load it into a `List(Of T)`, work with it and then write it back out when the program ends – Ňɏssa Pøngjǣrdenlarp Sep 02 '15 at 18:03
  • 2
    your code shows a `,` delimiter, but your sample data shows both a comma and semi colon – Ňɏssa Pøngjǣrdenlarp Sep 02 '15 at 18:14

1 Answers1

0

If you can change your text file format slightly (Only use one kind of delimiter - either comma or semi-colon), then you can use the Microsoft Text Driver. This allows you to treat the text file like a database table, and you can use SQL statements to SELECT, DELETE, or UPDATE. Below is an example:

Public Sub Delete(ByVal ID As String)
    Dim SQL As String = String.Format("DELETE FROM myfile.csv WHERE ID={0}", ID)
    Dim conn_str As String = String.Format("Driver={{Microsoft Text Driver (*.txt; *.csv)}};Dbq={0};Extensions=asc,csv,tab,tsv,txt;", folder)
    Dim conn As New OdbcConnection(conn_str)
    conn.Open()
    Dim cmd As New OdbcCommand(sqltxt, conn)
    Dim reader As OdbcDataReader = cmd.ExecuteNonQuery()

    ...

End Sub

If this system has any publicly facing user input, I would definitely recommend using a Parameterized Query to prevent SQL injection attacks.

Community
  • 1
  • 1
vbnet3d
  • 1,151
  • 1
  • 19
  • 37