Reading a word from text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Clark Teh

    Reading a word from text file

    Hi,
    I am lost in coming up with an algorithm and syntax for vb.net to perform a task similar to vlookup function in excel.

    The text file contains a series of data with each data in a new line. The data should consist of 2 columns. The first word is written immediately as the file opens and the second word is written after a tab. It is something like below:

    Username Password
    Alexander 12345

    The program will have a drop list and user can select Alexander while the program will automatically shows 12345 in text box in the same form.
    Anyone can help? I am newbie in this. Thanks.
  • marcellus7
    New Member
    • Oct 2008
    • 33

    #2
    Here's some information on how you'd read it.

    First, you'd read in the file, where inFilename is the path to your text file, using FileStream to access the file, and StreamReader to read the data from the text file:

    Code:
            Dim fs As New IO.FileStream(inFilename, IO.FileMode.Open)
            Dim sr As New IO.StreamReader(fs)
    Next, you'd read the data from the text file into a string array, splitting the array on each new line character. (Each line in the text file is a new member of the array)

    Code:
    Dim x() As String = sr.ReadToEnd().Split(CType(Chr(10), Char))
    Now that you're done reading in the text file, you can close the streamreader and filestream so that the text file isn't still using resources or holding any type of lock(s) on the file

    Code:
    sr.close()
    fs.close()
    Now, you just have to read each line in the array, split it on a tab, and add it to the dataset that you want it saved in. In this case we can just add it to a datatable so that it will be easier to access later. (it would probably be best to declare this datatable in the class, so that your other functions will also be able to access it)

    Code:
    'Declares the table, columns, table name, and primary key
    Dim userData as New DataTable
    userData.Columns.Add("key")
    userData.Columns.Add("Username")
    userData.Columns.Add("Password")
    userData.TableName = "UserData"
    userData.PrimaryKey = New DataColumn() {userData.Columns(0)}
    
    'Reads each line from the string array we created earlier
    'and save them to the datatable, starting with 1 so that
    'we dont get the column names
    Dim j as integer = 1
    Dim userRow as DataRow
    While j < x.Length
       'Gets the row and splits it on the tab
       Dim rw() As String = x(j).Split(CType(vbTab, Char))
       
       'Saves to datatable (we start saving the data at index
       ' 1 because 0 is the column with the primary key we 
       ' created earlier
       userRow = userData.NewRow()
       userRow(1) = rw(0)
       userRow(2) = rw(1)
       userData.Rows.Add(userRow)
       userData.AcceptChanges()
       userRow.AcceptChanges()
       j = j + 1
    End While
    Ok, now once the datatable is setup and filled, you can simply use the datatable to reference the data.

    Add the items from the datatable to the drop-downlist
    Code:
    'Adds all the usernames to the drop-down list
    For each row in userData
       dropdownlist.items.add(row(1))
    Next
    Now you can modify the SelectedIndexCh anged event to fill the textbox with the password when someone clicks the item from the drop-down list. It will be a simple piece of code if you added the usernames from the table like above, because the index of the username/password in the datatable will be the same as their index in your drop-down list.
    Code:
     Private Sub dropdownlist_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dropdownlist.SelectedIndexChanged
    Textbox1.Text = userData(dropdownlist.SelectedIndex, 2)
        End Sub
    And there you have it!
    Last edited by marcellus7; Nov 11 '10, 03:43 PM. Reason: edit code

    Comment

    Working...