Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Web Forms / HTTP File Upload / String.Split a StreamReader.ReadLine() string

Reply
Thread Tools

Web Forms / HTTP File Upload / String.Split a StreamReader.ReadLine() string

 
 
Andy Mee
Guest
Posts: n/a
 
      11-18-2003
Hello one and all,

I'm developing an Asp.NET system to take a CSV file uploaded via the web,
parse it, and insert the values into an SQL database. My sticking point
comes when I try to split() the string returned by readline() on the file.

The following code snippet works for me:
tokens = "one,two,three,four".Split(",")
for each token in tokens
response.write("<td>"+token+"</td>")
next

However, if I take the next line in the CSV, read using
StreamReader.ReadLine on the PostedFile.InputStream, I receive "Object
reference not set to an instance of an object." which I have narrowed down
to be my string holding the line. Further investigation reveals that no
other string member functions work on my line (.ToCharArray, .ToString,
etc).

I suspect that StreamReader.ReadLine is not correctly returning a string,
even though Response.Write(line) displays what I would expect.

I would appreciate any help or suggestions anybody can offer as to what's
going on here.

Thanks,

Andy


-- code --

<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<link href="adwords.css" rel="stylesheet" type="text/css" media="screen" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Upload CSV</title>
</head>
<body>

<script runat="server">
Sub UploadFile_Clicked(Sender as Object, e as EventArgs)
Dim str As Stream

str = loFile.PostedFile.InputStream()
Dim sr As StreamReader = New StreamReader(str)

Dim tokens as String() = Nothing
Dim line, token, t as String

Dim i as integer
for i = 1 to 6
line = sr.ReadLine()
next i
response.write("<table>")

do
line = sr.ReadLine()
response.write("<tr>")
tokens = line.Split(ControlChars.Tab)
for each token in tokens
response.write("<td>"+token+"</td>")
next
response.write("</tr>")
loop until line is nothing
response.write("</table>")

sr.close()

End sub
</script>

<form action="upload.aspx" method="post" enctype="multipart/form-data"
runat="server">
<fieldset>
<legend>Select a file to upload:</legend>
<p><input id="loFile" type="file" runat="server"></p>
<p><input type="submit" OnServerClick="UploadFile_Clicked"
class="clientloginsubmit" name="Submit" value="Upload AdWords CSV"
runat="server" /></p>
</fieldset>
</form>

</body>
</html>


 
Reply With Quote
 
 
 
 
TomB
Guest
Posts: n/a
 
      11-18-2003
This forum is for classic asp questions.

If this were in classic asp, personally I'd use ado to connect to the csv
and treat it like a database table.


"Andy Mee" <> wrote in message
news:bpdbfv$n5$...
> Hello one and all,
>
> I'm developing an Asp.NET system to take a CSV file uploaded via the web,
> parse it, and insert the values into an SQL database. My sticking point
> comes when I try to split() the string returned by readline() on the file.
>
> The following code snippet works for me:
> tokens = "one,two,three,four".Split(",")
> for each token in tokens
> response.write("<td>"+token+"</td>")
> next
>
> However, if I take the next line in the CSV, read using
> StreamReader.ReadLine on the PostedFile.InputStream, I receive "Object
> reference not set to an instance of an object." which I have narrowed down
> to be my string holding the line. Further investigation reveals that no
> other string member functions work on my line (.ToCharArray, .ToString,
> etc).
>
> I suspect that StreamReader.ReadLine is not correctly returning a string,
> even though Response.Write(line) displays what I would expect.
>
> I would appreciate any help or suggestions anybody can offer as to what's
> going on here.
>
> Thanks,
>
> Andy
>
>
> -- code --
>
> <%@ Page Language="VB" Debug="true" %>
> <%@ Import Namespace="System.IO" %>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
> <head>
> <link href="adwords.css" rel="stylesheet" type="text/css" media="screen"

/>
> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
> <title>Upload CSV</title>
> </head>
> <body>
>
> <script runat="server">
> Sub UploadFile_Clicked(Sender as Object, e as EventArgs)
> Dim str As Stream
>
> str = loFile.PostedFile.InputStream()
> Dim sr As StreamReader = New StreamReader(str)
>
> Dim tokens as String() = Nothing
> Dim line, token, t as String
>
> Dim i as integer
> for i = 1 to 6
> line = sr.ReadLine()
> next i
> response.write("<table>")
>
> do
> line = sr.ReadLine()
> response.write("<tr>")
> tokens = line.Split(ControlChars.Tab)
> for each token in tokens
> response.write("<td>"+token+"</td>")
> next
> response.write("</tr>")
> loop until line is nothing
> response.write("</table>")
>
> sr.close()
>
> End sub
> </script>
>
> <form action="upload.aspx" method="post" enctype="multipart/form-data"
> runat="server">
> <fieldset>
> <legend>Select a file to upload:</legend>
> <p><input id="loFile" type="file" runat="server"></p>
> <p><input type="submit" OnServerClick="UploadFile_Clicked"
> class="clientloginsubmit" name="Submit" value="Upload AdWords CSV"
> runat="server" /></p>
> </fieldset>
> </form>
>
> </body>
> </html>
>
>



 
Reply With Quote
 
 
 
 
Andy Mee
Guest
Posts: n/a
 
      11-18-2003
> This forum is for classic asp questions.

Apologies... if I scan my news server for ASP groups, I find precisely five.
Discounting Italian, German and Hebrew groups, I'm left with three. None of
them seemed to specify what ASP they were dealing with...


> If this were in classic asp, personally I'd use ado to connect to the csv
> and treat it like a database table.


Thanks for that -- I'll look into the ADO stuff in .NET.

Still baffled by the completely nonsensical error though

Andy.


 
Reply With Quote
 
Ray at
Guest
Posts: n/a
 
      11-18-2003
Use these "words" as your search through the newsgroups:

aspnet
adonet
dotnet (broad)

Ray at work

"Andy Mee" <> wrote in message
news:bpddsd$4h0$...
> > This forum is for classic asp questions.

>
> Apologies... if I scan my news server for ASP groups, I find precisely

five.
> Discounting Italian, German and Hebrew groups, I'm left with three. None

of
> them seemed to specify what ASP they were dealing with...
>
>
> > If this were in classic asp, personally I'd use ado to connect to the

csv
> > and treat it like a database table.

>
> Thanks for that -- I'll look into the ADO stuff in .NET.
>
> Still baffled by the completely nonsensical error though
>
> Andy.
>
>



 
Reply With Quote
 
Bob Barrows
Guest
Posts: n/a
 
      11-18-2003
Andy Mee wrote:
>> This forum is for classic asp questions.

>
> Apologies... if I scan my news server for ASP groups, I find
> precisely five. Discounting Italian, German and Hebrew groups, I'm
> left with three. None of them seemed to specify what ASP they were
> dealing with...
>
>

The .dotnet.* groups are set up for .Net questions.

Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


 
Reply With Quote
 
TomB
Guest
Posts: n/a
 
      11-18-2003
http://www.able-consulting.com/MDAC/...crosoftJetText


"Andy Mee" <> wrote in message
news:bpddsd$4h0$...
> > This forum is for classic asp questions.

>
> Apologies... if I scan my news server for ASP groups, I find precisely

five.
> Discounting Italian, German and Hebrew groups, I'm left with three. None

of
> them seemed to specify what ASP they were dealing with...
>
>
> > If this were in classic asp, personally I'd use ado to connect to the

csv
> > and treat it like a database table.

>
> Thanks for that -- I'll look into the ADO stuff in .NET.
>
> Still baffled by the completely nonsensical error though
>
> Andy.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HTTP SOAP/HTTP GET/HTTP POST milan_9211 Software 0 01-10-2011 02:10 PM
The Web server reported the following error when attempting to create or open the Web project located at the following URL: 'http://localhost/822319ev1'. 'HTTP/1.1 500 Internal Server Error'. chanmm ASP .Net 2 09-07-2010 07:37 AM
Sample code for doing a “chunked” webfile HTTP d ownload, andthen stream to HTTP upload to another web app? Greg Hauptmann Ruby 1 12-08-2009 06:37 PM
forms authentication -- expired forms cookie vs. not provided forms cookie Eric ASP .Net Security 2 01-27-2006 10:09 PM
Upload a file without file Upload control - ASP.Net =?Utf-8?B?U2FyYXY=?= ASP .Net 3 08-03-2005 01:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57