473,395 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.
Nov 11 '10 #1
1 3795
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:

Expand|Select|Wrap|Line Numbers
  1.         Dim fs As New IO.FileStream(inFilename, IO.FileMode.Open)
  2.         Dim sr As New IO.StreamReader(fs)
  3.  
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)

Expand|Select|Wrap|Line Numbers
  1. Dim x() As String = sr.ReadToEnd().Split(CType(Chr(10), Char))
  2.  
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

Expand|Select|Wrap|Line Numbers
  1. sr.close()
  2. fs.close()
  3.  
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)

Expand|Select|Wrap|Line Numbers
  1. 'Declares the table, columns, table name, and primary key
  2. Dim userData as New DataTable
  3. userData.Columns.Add("key")
  4. userData.Columns.Add("Username")
  5. userData.Columns.Add("Password")
  6. userData.TableName = "UserData"
  7. userData.PrimaryKey = New DataColumn() {userData.Columns(0)}
  8.  
  9. 'Reads each line from the string array we created earlier
  10. 'and save them to the datatable, starting with 1 so that
  11. 'we dont get the column names
  12. Dim j as integer = 1
  13. Dim userRow as DataRow
  14. While j < x.Length
  15.    'Gets the row and splits it on the tab
  16.    Dim rw() As String = x(j).Split(CType(vbTab, Char))
  17.  
  18.    'Saves to datatable (we start saving the data at index
  19.    ' 1 because 0 is the column with the primary key we 
  20.    ' created earlier
  21.    userRow = userData.NewRow()
  22.    userRow(1) = rw(0)
  23.    userRow(2) = rw(1)
  24.    userData.Rows.Add(userRow)
  25.    userData.AcceptChanges()
  26.    userRow.AcceptChanges()
  27.    j = j + 1
  28. End While
  29.  
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
Expand|Select|Wrap|Line Numbers
  1. 'Adds all the usernames to the drop-down list
  2. For each row in userData
  3.    dropdownlist.items.add(row(1))
  4. Next
  5.  
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.
Expand|Select|Wrap|Line Numbers
  1.  Private Sub dropdownlist_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dropdownlist.SelectedIndexChanged
  2. Textbox1.Text = userData(dropdownlist.SelectedIndex, 2)
  3.     End Sub
  4.  
And there you have it!
Nov 11 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: mail2atulmehta | last post by:
Hi, I do not know if this is right place for this, but i need some help. I have a text file, whose values are seprated by a delimiter. I want to open this file in excel, ( not import it) . I have...
1
by: rodchar | last post by:
hey all, is there a quick way to read.all the contents of a text file, search for the text, and if it finds that text to read the entire line? thanks, rodchar
1
by: mart2006 | last post by:
Hi, I'm currently reading a text file via PHP which, in itself, is very easy. However I want to specifically get one word from the text file and assign it as a variable and I'm struggling like...
1
by: Sudhesh | last post by:
Hi, I know its fairly easy in .NET to read a text file (fixed or delimited) using a Microsoft Text driver. My question is, is there a similar easy way to read text files if they have different...
5
by: Z.K. | last post by:
In C#, using the StreamReader, how do I detect when you get to the end of line. I am reading a text file using the Read() function and I need to detect the \n\r, but everything I try does not...
2
by: bambataa | last post by:
Hellloww...i am new to Java and i am facing a problem, i am reading a text file, i am using string tokenizer class,..now at some point when i read a first token i have to check it if its an integer...
1
by: engggirl3000 | last post by:
Another question I have, what is the difference between reading a text file to a program and opening a text file in the program? A sample of one of the text files is formatted like this: 3 ...
1
by: stoogots2 | last post by:
I have written a Windows App in C# that needs to read a text file over the network, starting from the end of the file and reading backwards toward the beginning (looking for the last occurrence of a...
2
by: thanawala27 | last post by:
Hi, I'm facign a strange problem in reading a text file. The contents of my text file is: A1;B1;C1;D1 A2;B2;C2;D2 A3;B3;C3;D3
2
by: friend.blah | last post by:
i have a text file lets say in this format abc abs ajfhg agjfh fhs ghg jhgjs fjhg dj djk djghd dkfdf .... .... ...... i want to read the first line at certain time for eg : at 10clk
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.