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:
-
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)
-
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
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)
-
'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
-
'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 SelectedIndexChanged 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.
-
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!