Wrox Programmer Forums
|
ASP Pro Code Clinic As of Oct 5, 2005, this forum is now locked. No posts have been deleted. Please use "Classic ASP Professional" at: http://p2p.wrox.com/forum.asp?FORUM_ID=56 for discussions similar to the old ASP Pro Code Clinic or one of the other many remaining ASP and ASP.NET forums here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP Pro Code Clinic section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 18th, 2003, 08:25 AM
Registered User
 
Join Date: Nov 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default String.Split a StreamReader.ReadLine() string

Hello one and all,

I'm developing a 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>")
response.write(line.tostring())
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>





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to split string krevathi1912 Access VBA 10 September 6th, 2007 02:21 AM
How to split this string chanduboorla XSLT 1 April 13th, 2006 04:15 AM
How to split a string lily611 General .NET 5 March 18th, 2005 01:16 PM
string split by : crmpicco Classic ASP Basics 3 February 10th, 2005 07:49 AM
String.Split bbhill General .NET 2 April 10th, 2004 05:53 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.