Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / June 2007

Tip: Looking for answers? Try searching our database.

Parsing a text file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JJ - 06 Jun 2007 15:31 GMT
Whats the best way for me to pull out records from a tab delimited text
file?

Or rather HOW do I parse the text, knowing that the tabs are field
delimiters and a return (I image) signifies a new record
?
JJ
Alexey Smirnov - 06 Jun 2007 15:40 GMT
> Whats the best way for me to pull out records from a tab delimited text
> file?
[quoted text clipped - 3 lines]
> ?
> JJ

something like this

DataTable dt = new DataTable();

dt.Columns.Add(new DataColumn("column1"));
dt.Columns.Add(new DataColumn("column2"));

string[] lines = TextBox1.Text.Trim().Split('\r');
string[] s = null;

foreach (string line in lines)
{
DataRow row = dt.NewRow();

string[] fields = line.Split('\t');

s[0] = fields[0];
s[1] = fields[1];

row.ItemArray = s;
dt.Rows.Add(row);
}

GridView1.DataSource = dt;
GridView1.DataBind();
Alexey Smirnov - 06 Jun 2007 15:44 GMT
> string[] fields = line.Split('\t');
>
[quoted text clipped - 3 lines]
> row.ItemArray = s;
> dt.Rows.Add(row);

Well... this should be optimized:

string[] fields = line.Split('\t');
row.ItemArray = fields;
dt.Rows.Add(row);
JJ - 06 Jun 2007 15:49 GMT
Actually, another question:
Could I open the text file from a path to the users file on their computer,
or do I have to (or is it best to) upload the text file first?
JJ

>> string[] fields = line.Split('\t');
>>
[quoted text clipped - 9 lines]
> row.ItemArray = fields;
> dt.Rows.Add(row);
Alexey Smirnov - 06 Jun 2007 18:27 GMT
> or do I have to (or is it best to) upload the text file first?

Yes, user should upload the file.

Another way is to use a TextBox Control where user could copy/paste an
entire content of the file
JJ - 06 Jun 2007 19:31 GMT
I'd prefer it if they could paste as I'd not have to worry about storage
space, but the files in question will have around 3000 lines, so I'm not
sure if that is going to be possible/practical...?
JJ
>> or do I have to (or is it best to) upload the text file first?
>
> Yes, user should upload the file.
>
> Another way is to use a TextBox Control where user could copy/paste an
> entire content of the file
Alexey Smirnov - 06 Jun 2007 19:44 GMT
> possible

yes

> practical...?

don't know :-) but it can be a good option, I think.
JJ - 06 Jun 2007 20:00 GMT
I guess to avoid my worry of too many files being uploaded, I could just get
them to choose the file on their computer, then read the file and store it
in a static filename (so it keeps over-writing the old one); as I won't need
the file after I've parsed it (and input the fields into a database), that
should be ok.

Hang on though - that wouldn't work if someone else tried to do another
upload at exactly the same time.

I'm not sure their going to like copying and pasting such large files. I
agree though, I'd much rather copying and pasting myself - a lot simpler.
It looks like its either that or an ever growing number of files that I
haven't given them permission to delete.

JJ

>> possible
>
[quoted text clipped - 3 lines]
>
> don't know :-) but it can be a good option, I think.
Alexey Smirnov - 06 Jun 2007 20:25 GMT
> I guess to avoid my worry of too many files being uploaded, I could just get
> them to choose the file on their computer, then read the file and store it
[quoted text clipped - 9 lines]
> It looks like its either that or an ever growing number of files that I
> haven't given them permission to delete.

A copy/paste approach is good when you have an Excel file and you can
open it, modify and copy the entire contents directly from Excel to
the web site (with a TextBox) without saving the file as a tab-
delimeted (or CSV) export and doing an upload.

But if you already have such file the way with upload is fast and
secure...
JJ - 06 Jun 2007 20:33 GMT
Yes - it will be an excel file they'll have. I guess 3000 lines is possible
to cut and paste....
I'm not sure what the limitations are in terms of memory within excels
clipboard.

I would have though 3000 lines was well within limits, would you?
JJ

>> I guess to avoid my worry of too many files being uploaded, I could just
>> get
[quoted text clipped - 21 lines]
> But if you already have such file the way with upload is fast and
> secure...
Alexey Smirnov - 06 Jun 2007 21:17 GMT
> Yes - it will be an excel file they'll have. I guess 3000 lines is possible
> to cut and paste....
[quoted text clipped - 3 lines]
> I would have though 3000 lines was well within limits, would you?
> JJ

The Office clipboard is limited, but the Windows clipboard depends on
the RAM only.

I think, you should test if users will not have any problems with copy
and paste. I think 3000-lines-file is big, but not because of its size
in bytes but because you cannot be sure that you have pasted the whole
file in the small textbox area on the form. Though you can show the
number of submitted lines using a client js, and/ or on a postback
JJ - 07 Jun 2007 03:02 GMT
What method should I use to sort/edit/delete the data, in case the user
wants to adjust the data before inputting it to the database?

JJ

>> Yes - it will be an excel file they'll have. I guess 3000 lines is
>> possible
[quoted text clipped - 13 lines]
> file in the small textbox area on the form. Though you can show the
> number of submitted lines using a client js, and/ or on a postback
JJ - 07 Jun 2007 03:27 GMT
What I mean is how would I temporarily hold the data whilst any
sorting/deleting/editing can take place?

> What method should I use to sort/edit/delete the data, in case the user
> wants to adjust the data before inputting it to the database?
[quoted text clipped - 18 lines]
>> file in the small textbox area on the form. Though you can show the
>> number of submitted lines using a client js, and/ or on a postback
David - 07 Jun 2007 16:30 GMT
You can use ODBC and (I think) oledb to open files like this. It shouldn't
matter wether it is a comma or tab delimited as I think you can define that
when you open it.

(I have used ODBC in classic ASP to do it, but the method should be there in
.NET)

Signature

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

> What I mean is how would I temporarily hold the data whilst any
> sorting/deleting/editing can take place?
[quoted text clipped - 21 lines]
>>> file in the small textbox area on the form. Though you can show the
>>> number of submitted lines using a client js, and/ or on a postback
JJ - 07 Jun 2007 16:49 GMT
I may well be wrong by saying this, but don't you have to have excel
installed on the server in order to treate the use the file as an excel
file?
(if that was what you were meaning).
Its a shared server on this occassion, so I cannot make sure any excel
components are on it.
(or have I completely got the wrong end of the stick....?)
JJ
> You can use ODBC and (I think) oledb to open files like this. It shouldn't
> matter wether it is a comma or tab delimited as I think you can define
[quoted text clipped - 28 lines]
>>>> file in the small textbox area on the form. Though you can show the
>>>> number of submitted lines using a client js, and/ or on a postback
Mark Rae - 07 Jun 2007 17:09 GMT
>I may well be wrong by saying this, but don't you have to have excel
>installed on the server in order to treate the use the file as an excel
>file?

No.

http://www.google.co.uk/search?sourceid=navclient&hl=en-GB&ie=UTF-8&rlz=1T4GGIH_
en-GBGB220GB220&q=%22ADO%2eNET%22+Excel

http://www.aspose.com/Products/Aspose.Cells/Default.aspx

If you're even considering installing Excel (or any other Office component)
on a webserver, then you need to start again...

Signature

http://www.markrae.net

JJ - 07 Jun 2007 17:30 GMT
Thanks Mark.

No I'm not considering installing any office component. I was merely
confused. I think its likely I'll stick to manipulating this input as a
plain text file. I just need to work out how to operate with it- i.e.
sort/page and allow the user to edit the fields _prior_ to inputting it into
the database.

I can do all that with objectdatasources fetching data from the database,
but not sure how I'd go about it in this case.
JJ

>>I may well be wrong by saying this, but don't you have to have excel
>>installed on the server in order to treate the use the file as an excel
[quoted text clipped - 7 lines]
> If you're even considering installing Excel (or any other Office
> component) on a webserver, then you need to start again...
Mark Rae - 07 Jun 2007 17:52 GMT
> I can do all that with objectdatasources fetching data from the database,
> but not sure how I'd go about it in this case.

Have you considered something like this:?
http://www.fpoint.com/netproducts/spreadweb/spread.aspx

Signature

http://www.markrae.net

JJ - 07 Jun 2007 17:59 GMT
This is a 'trial project' at the moment - so no budget. And as I'm merely a
poor, 'programmer' (meant in the loosest sense), I cannot afford such
things.

Thanks though, now I know what I'm missing....

JJ

>> I can do all that with objectdatasources fetching data from the database,
>> but not sure how I'd go about it in this case.
>
> Have you considered something like this:?
> http://www.fpoint.com/netproducts/spreadweb/spread.aspx
Mark Rae - 07 Jun 2007 18:38 GMT
> I cannot afford such things.

http://www.gemboxsoftware.com/GBSpreadsheetFree.htm

Signature

http://www.markrae.net

JJ - 07 Jun 2007 19:00 GMT
For a second there I thought you'd answered my prayers. Unfortunately I need
to test the trial system on a full size text file, so using the free
component with its limitations won't do the job.

Thanks though.

I'm actually there with the importing of the text - just not there with the
manipulation of it. Because it's so large, I'm at a loss as to how to keep
hold of the datatable so that I can sort/page it. Not sure whether things
like viewstate could hold such a large item...?
JJ

"Limitations
GemBox.Spreadsheet Free delivers the same performance and set of features as
the Professional version. However, the following limitations are imposed:

Maximum number of rows per sheet is 150.
Maximum number of sheets per workbook is 5.

The above limitations are enforced during writing or reading XLS or CSV
files."

>> I cannot afford such things.
>
> http://www.gemboxsoftware.com/GBSpreadsheetFree.htm
David - 08 Jun 2007 17:53 GMT
Here is how I do this in VBScript. You can do similar in .NET

It works, it will read txt and csv files just like a database. You can query
it.

set conn=CreateObject("adodb.connection")
conn.Open "DRIVER={Microsoft Text Driver (*.txt;
*.csv)};DefaultDir=/WorkingDir;"

set rs=CreateObject("adodb.recordset")
strSQL = "SELECT * FROM [MyFileName.csv] "
rs.Open strSQL, conn, 3, 3

if not rs.EOF then
     do while not rs.EOF

'Do what you need to do with the data.

        rs.movenext
     loop
end if

'Close your connection and recordset

This is in VBScript. I have not done it yet it .NET though that will be
trivial. It will save all the effort of parsing the file to get what you
want. You can set up the ODBC parameters to however the data should be
parsed (delimited).

Signature

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

> For a second there I thought you'd answered my prayers. Unfortunately I
> need to test the trial system on a full size text file, so using the free
[quoted text clipped - 22 lines]
>>
>> http://www.gemboxsoftware.com/GBSpreadsheetFree.htm
JJ - 06 Jun 2007 15:44 GMT
Great - thanks.
JJ
>> Whats the best way for me to pull out records from a tab delimited text
>> file?
[quoted text clipped - 29 lines]
> GridView1.DataSource = dt;
> GridView1.DataBind();

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.