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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy Mee

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

    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>"+toke n+"</td>")
    next

    However, if I take the next line in the CSV, read using
    StreamReader.Re adLine on the PostedFile.Inpu tStream, 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.Re adLine 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="Syst em.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.c ss" 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_Clic ked(Sender as Object, e as EventArgs)
    Dim str As Stream

    str = loFile.PostedFi le.InputStream( )
    Dim sr As StreamReader = New StreamReader(st r)

    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(Cont rolChars.Tab)
    for each token in tokens
    response.write( "<td>"+toke n+"</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="multip art/form-data"
    runat="server">
    <fieldset>
    <legend>Selec t a file to upload:</legend>
    <p><input id="loFile" type="file" runat="server"> </p>
    <p><input type="submit" OnServerClick=" UploadFile_Clic ked"
    class="clientlo ginsubmit" name="Submit" value="Upload AdWords CSV"
    runat="server" /></p>
    </fieldset>
    </form>

    </body>
    </html>


  • TomB

    #2
    Re: Web Forms / HTTP File Upload / String.Split a StreamReader.Re adLine() string

    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" <andy@RemoveThi sSubDomain.itqs olutions.net> wrote in message
    news:bpdbfv$n5$ 1@hercules.btin ternet.com...[color=blue]
    > 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>"+toke n+"</td>")
    > next
    >
    > However, if I take the next line in the CSV, read using
    > StreamReader.Re adLine on the PostedFile.Inpu tStream, 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.Re adLine 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="Syst em.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.c ss" rel="stylesheet " type="text/css" media="screen"[/color]
    />[color=blue]
    > <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    > <title>Upload CSV</title>
    > </head>
    > <body>
    >
    > <script runat="server">
    > Sub UploadFile_Clic ked(Sender as Object, e as EventArgs)
    > Dim str As Stream
    >
    > str = loFile.PostedFi le.InputStream( )
    > Dim sr As StreamReader = New StreamReader(st r)
    >
    > 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(Cont rolChars.Tab)
    > for each token in tokens
    > response.write( "<td>"+toke n+"</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="multip art/form-data"
    > runat="server">
    > <fieldset>
    > <legend>Selec t a file to upload:</legend>
    > <p><input id="loFile" type="file" runat="server"> </p>
    > <p><input type="submit" OnServerClick=" UploadFile_Clic ked"
    > class="clientlo ginsubmit" name="Submit" value="Upload AdWords CSV"
    > runat="server" /></p>
    > </fieldset>
    > </form>
    >
    > </body>
    > </html>
    >
    >[/color]


    Comment

    • Andy Mee

      #3
      Re: Web Forms / HTTP File Upload / String.Split a StreamReader.Re adLine() string

      > 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...

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

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

      Still baffled by the completely nonsensical error though :)

      Andy.


      Comment

      • Ray at

        #4
        Re: Web Forms / HTTP File Upload / String.Split a StreamReader.Re adLine() string

        Use these "words" as your search through the newsgroups:

        aspnet
        adonet
        dotnet (broad)

        Ray at work

        "Andy Mee" <andy@RemoveThi sSubDomain.itqs olutions.net> wrote in message
        news:bpddsd$4h0 $1@sparta.btint ernet.com...[color=blue][color=green]
        > > This forum is for classic asp questions.[/color]
        >
        > Apologies... if I scan my news server for ASP groups, I find precisely[/color]
        five.[color=blue]
        > Discounting Italian, German and Hebrew groups, I'm left with three. None[/color]
        of[color=blue]
        > them seemed to specify what ASP they were dealing with...
        >
        >[color=green]
        > > If this were in classic asp, personally I'd use ado to connect to the[/color][/color]
        csv[color=blue][color=green]
        > > and treat it like a database table.[/color]
        >
        > Thanks for that -- I'll look into the ADO stuff in .NET.
        >
        > Still baffled by the completely nonsensical error though :)
        >
        > Andy.
        >
        >[/color]


        Comment

        • Bob Barrows

          #5
          Re: Web Forms / HTTP File Upload / String.Split a StreamReader.Re adLine() string

          Andy Mee wrote:[color=blue][color=green]
          >> This forum is for classic asp questions.[/color]
          >
          > 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...
          >
          >[/color]
          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.


          Comment

          • TomB

            #6
            Re: Web Forms / HTTP File Upload / String.Split a StreamReader.Re adLine() string




            "Andy Mee" <andy@RemoveThi sSubDomain.itqs olutions.net> wrote in message
            news:bpddsd$4h0 $1@sparta.btint ernet.com...[color=blue][color=green]
            > > This forum is for classic asp questions.[/color]
            >
            > Apologies... if I scan my news server for ASP groups, I find precisely[/color]
            five.[color=blue]
            > Discounting Italian, German and Hebrew groups, I'm left with three. None[/color]
            of[color=blue]
            > them seemed to specify what ASP they were dealing with...
            >
            >[color=green]
            > > If this were in classic asp, personally I'd use ado to connect to the[/color][/color]
            csv[color=blue][color=green]
            > > and treat it like a database table.[/color]
            >
            > Thanks for that -- I'll look into the ADO stuff in .NET.
            >
            > Still baffled by the completely nonsensical error though :)
            >
            > Andy.
            >
            >[/color]


            Comment

            Working...