You are in: Home > Articles > Reading a Delimited File using ASP.NET and VB.NET | ||
Reading a Delimited File using ASP.NET and VB.NETIntroductionThis article will show you how to read a delimited text file using asp.net or vb.net, which will explain about various aspects of a delimited file, code explanation and source code. We must be aware of importing data into a database. Data used for importing is normally in the form of a text file. Sometimes these files are called a comma-separated value file, which will normally have a .csv extension. In either of the files whether it be a text file or a .csv file, all the values are delimited using either a word, letter, number, tab or space. These are known as delimiters. The name of the file is given according to the respective delimiter used. For example if the values are delimited using commas (","), the file will be known as a comma-delimited file. Similarly, if the values are delimited using a delimiter hash ("#") then that file will be called a hash-delimited file. Let us use an example to see what these delimited files look like. Example: Comma-delimited fileValue1, Value2, Value3, Value4, Value5 In the above example we can see a comma between the words: Value1 Value2 until Value 5. Example: TAB delimited fileValue1 Value2 Value3 Value4 Value5 In the above example we are able to see space, the size of one tab between each value. Now we have seen what a delimited text file looks like, let’s have a look at other uses of the delimited file.
Let us go into detail of how these delimited files are used in both cases. Data import into databasesIn DBMS and RDBMS we create databases and tables. In a huge database consisting of millions of records, in order to transfer data from one database or transport a database from one location to another, data is normally exported into a text file. When we try to export data to a text file, the system will ask which delimiter to be used. When we specify the delimiter to be used, each piece of data stored in the fields will get appended with the delimiter. While importing the data back to the database, the system will ask for the delimiter which is used, based on the delimiter, data gets imported into the database. Used as a databaseIn some areas where there is no support for databases, delimited text files are used as a replacement, albeit with limited functionality. Data is directly read from the delimited text file into arrays and manipulated. Usually these types of delimited text files are used in Web applications, where hiring Web hosting with database support is a costly affair. Instead, these delimited text files are used as an alternative to an expensive database. We have seen what a delimited text file is, what their important uses and applications are. Now we shall go on and see how to read these delimited text files using ASP.NET and VB.NET Note: Delimiters are sometimes known as splitters To read files using .NET we need to declare a namespace called SYSTEM.IO, which contains a set of classes, and methods, which is used for reading as well as writing files. Let us now declare a name space, which will have System.IO
Next we open the script tag, which will run on the server. We will be specifying our scripting language as VB in the tag
Next we shall think about where to write the function, we shall write coding when the page loads, we shall declare the Subroutine, which fires when the page loads. Let us now declare the subroutine
We need to specify a file name which is to be read, whether it is a text
file or a
Before we assign the file name to the variable, we will be using a server variable, which is used to trace the path of the specified file. Using Server.mappath we can trace the absolute path of the file, which is specified.
In the above code we map the path of the text file, readtest.txt where the path is stored into the variable, filetoread. Next we need to initiate the Stream Reader class, where we declare filestream as a stream reader.
We need to assign a file to the streamreader to open the file:
In the above code we use the Opentext method of the streamer and pass the variable, filetoread. The variable, filetoread contains the filepath. Streamer reads the content, now we need a place to store the contents for the file. We will have to declare a variable named readcontents with data type as string, which will store the contents of the file
Using the ReadToEnd method of the streamer we read the entire contents of to the variable readcontents:
Now we will have to declare a variable, which will store the delimiter, that is the separator text, let us declare the variable named as textdelimiter with the data type as string.
Now the time has come for us to assign the delimiter, which might be a letter, word, number etc. Now let us assign the delimiter to be "geeyes"
In the above code you can change the value of the variable textdelimiter as you wish, for example if you wish to change the delimiter to "#" your variable will look like this:
If you want to read a space-delimited text file then you will need to provide a space between the double quotes of the variable as shown:
Note: The value of the variable is mandatory and is very important, as the reading of the delimited file depends on the value that is stored in the variable, which is the delimiter. After we have specified the delimiter, now the time has come to split the contents of the files according to the delimiter. To do that, first we shall declare a variable named splitout, then we will use the split function to split the text .
In the above code we have declared a variable named splitout, to that variable we are assigning split functionality
Split function contains two parameters; one is the content and the other is the split text (delimiter). In the above script we have used the readcontents variable, which contains the contents. We then pass the variable, textdelimiter, which contains the delimiter text. Passing two variables the contents are split according to the delimiter passed. The split contents are stored in the form of arrays. To exhibit the output we will be two asp: label whose text will be assigned dynamically. Let us place a label out of the </script> tag
Note: the above tag should be placed out of the </script> tag. Now we declare a variable, i with its datatype set to integer. This is used for looping.
Now we will be using a for loop to read the contents which are stored in the arrays.
In the above code, Ubound(splitout) specifies the upper limit of the array splitout, splitout is the variable which contains the split text. Here you might wonder how splitout has become an array. Whenever you use a split function, the variable that is used for splitting is automatically converted into an array. Here in our case, splitout is automatically converted to an array. Starting from zero, which is our lower limit until the upper limit we write the text to the asp:label .
In the above code, lblsplittext.text, we dynamically assign the value to the asp:label named lblsplittext. Let us have a look at the next part. Here we use the <b> bold tag which is a normal HTML tag. This is merely used to brighten the test then we concatenate the variable, i which is in the loop , which displays serial number.
in the next part
We are concatenating the variable splitout(i), we would see that variable, i is passed to the splitout variable. Here the concept of passing i is as follows: We know that splitout is an array, the lower limit of and array starts at zero. Since this splitout(i) is within the loop, the variable passes from the lower limit to the upper limit, when it goes through the loop it looks like, splitout(0), then splitout(1), then splitout(2) until the upper limit. To navigate to the next array using the for loop, We use the key word, Next In order to differentiate the file contents and the read delimited contents, we shall display the files contents to an asp: label directly. This is raw without any split. Now let us place the tag after the </script> tag:
We dynamically assign the labels text with the contents read from the file.
In the above code we concatenate a "<br>" just to break the line. Now we close the stream class, to release the resources:
After we close the stream class we close the subroutine:
Next we close the script tag:
After closing the script tag, we will place the asp: label as explained previously and some text:
The above tags create two asp: labels that are merely used for display purposes. Their text are assigned dynamically. That's it... We have gone through the complete procedure for reading a delimited text file. Testing the scriptTo test this script you will ceate two files, one .aspx file and one .txt file.
Save the file as readdemilit.apx 3. Now create a textfile named readtest.txt, copy the following text to that file and save it: The history of the world, is the history of a few men who had faith in themselves, that faith calls out the divinity, with in, you can do anything you fail only when you do not strive sufficiently to manifest the infinite power, if you have faith in all the three hundred and thirty millions, of your mythological gods and in all the gods which foreign has now and gain faith, in yourself,what ever you think that you will be if you think weak weak, you will be,if you think yourselves strong strong you will ,be free and hope for nothing from any one. readlimit.aspx is the source file and readtest.txt is the delimited text file 4. Dump these files to your wwwroot directory and run readlimit.aspx Your output should look like the following: Plain OutputThe history of the world, is the history of a few men who had faith in themselves, that faith calls out the divinity, within, you can do anything you fail only when you do not strive sufficiently to manifest the infinite power, if you have faith in all the three hundred and thirty millions, of your mythological gods and in all the gods which foreign has now and gain faith, in yourself,what ever you think that you will be if you think weak weak, you will be,if you think yourselves strong strong you will, be free and hope for nothing from anyone. Delimited OutputSplit 1) The history of the world
My Articles are dedicated to my father |
||