Searching a file and displaying results

Please HELP Visual Basic question

Page 1 of 1

1 Replies - 577 Views - Last Post: 30 November 2010 - 08:57 AM Rate Topic: -----

#1 gf192w  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-November 10

Searching a file and displaying results

Posted 29 November 2010 - 10:12 PM

Hello, I am in dire need of assistance, if anyone can please give me a moment of your time.
I have to write a program that allows a user to input DVD info such as DVD Name, Year produced, Running Time, and Rating.
The user then clicks a menu item "New" to save that info to a file.
Then the next time the user inputs new DVD info it has to append the new data to the same file. (I got this far and everything is working writing and appending data)

Next the year has to be able to search by DVD Video name and it has to print the info in the text boxes on the form, if not found then a messagebox should say not found. When I use the sub procedure that I have created to search the record it always comes back as record not found although the record in in the file.


Here is my entire code so far can someone please help...

Option Strict On
Imports System.IO

Public Class Form1

'Class level variables 
'Document filename 
Dim strFilename As String = String.Empty
'to notify us when data has changed in the text box 
Dim blnIsChanged As Boolean = False

Structure videoData
Dim strVideoName As String
Dim strYearProduced As String
Dim strRunningTime As String
Dim strRating As String
End Structure



Sub SaveNewData()


'Save the current document under the name stored in documentName

Dim videoRecord As videoData

Dim inputRecord As StreamWriter

videoRecord.strVideoName = txtVideoName.Text

videoRecord.strYearProduced = txtYearProduced.Text

videoRecord.strRunningTime = txtRunningTime.Text

videoRecord.strRating = txtRating.Text



inputRecord = File.CreateText(strFilename)

inputRecord.WriteLine("Video Name: " & (videoRecord.strVideoName))

inputRecord.WriteLine("Year Produced: " & (videoRecord.strYearProduced))

inputRecord.WriteLine("Runing Time: " & videoRecord.strRunningTime)

inputRecord.WriteLine("Rating:" & videoRecord.strRating)

inputRecord.Close()

'update blnIschange variable

blnIsChanged = False


End Sub

Sub appendRecord()


Dim appendFile As videoData

Dim outputdata As StreamWriter

appendFile.strVideoName = txtVideoName.Text

appendFile.strYearProduced = txtYearProduced.Text

appendFile.strRunningTime = txtRunningTime.Text

appendFile.strRating = txtRating.Text



outputdata = File.AppendText(strFilename)

outputdata.WriteLine("Video Name: " & (appendFile.strVideoName))

outputdata.WriteLine("Year Produced: " & (appendFile.strYearProduced))

outputdata.WriteLine("Runing Time: " & appendFile.strRunningTime)

outputdata.WriteLine("Rating:" & appendFile.strRating)

outputdata.Close()

'update blnIschange variable

blnIsChanged = False

End Sub

Sub SearchRecord()
Dim strVideoName As String

Dim strYearProduced As String

Dim strRunningTime As String

Dim strRating As String

Dim intCount As Integer

Dim inputRecord As StreamReader





Dim strInput As String



Dim blnFound As Boolean

strInput = InputBox("Enter Name")







If File.Exists(strFilename) Then

inputRecord = File.OpenText(strFilename)



blnFound = True

Do While inputRecord.Peek = -1

strInput = inputRecord.ReadLine()

For intCount = 1 To 20

'Read records from the file

strVideoName = inputRecord.ReadLine()

strYearProduced = inputRecord.ReadLine()

strRunningTime = txtRunningTime.Text

strRating = txtRating.Text



'Display the data in the text box using vbTab.



txtVideoName.Text = strVideoName



txtYearProduced.Text = strYearProduced



Next intCount

Loop

Else



blnFound = False



MessageBox.Show("Record not found")



End If


End Sub



Private Sub txtVideoName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtVideoName.TextChanged

'update the isChanged variable that the text has changed 
blnIsChanged = True

End Sub

Private Sub mnuFileNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileNew.Click

If sfdSaveFile.ShowDialog = Windows.Forms.DialogResult.OK Then

strFilename = sfdSaveFile.FileName

SaveNewData()

End If

End Sub

Private Sub mnuSearchName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSearchName.Click
SearchRecord()

End Sub


Private Sub mnuFileAppendExist_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileAppendExist.Click

If ofdOpenFile.ShowDialog() = Windows.Forms.DialogResult.OK Then
strFilename = ofdOpenFile.FileName
End If

appendRecord()

End Sub
End Class


This post has been edited by Martyr2: 29 November 2010 - 10:22 PM
Reason for edit:: Please use code tags in the future, thanks!


Is This A Good Question/Topic? 0
  • +

Replies To: Searching a file and displaying results

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9497
  • View blog
  • Posts: 35,844
  • Joined: 12-June 08

Re: Searching a file and displaying results

Posted 30 November 2010 - 08:57 AM

You are conceptually doing this wrong and making it harder than it should. You should be thinking of this in terms of records in a table.

Saving the data to a file is nice, but why not save it as XML or some format that would be easy to load up in say... a dataset? Datasets are super easy to search through because they mimic a database table.


I would spend some time investigating how to add a dataset to your project.. then add a datatable to that dataset.. then add your appropriate columns... learning how to declare a dataset on your form... how to add rows to it... and lastly how to '<dataset name>.saveXML(<string _filelocation>)' and loading the same way.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1