Solved

C# Read from csv-file

Posted on 2009-03-30
2
1,113 Views
Last Modified: 2012-05-06
I want to read a CSV file and put the data into an ArrayList. I use HtmlInputFile. It works fine when the user browse for the file and select the file. It works fine when I test this local on my machine.
But when I test it at the webserver, the filename refer to (for example) C:\myFile.csv but at the server and not the client.
Is it possible to get the file (stream) or filename in codebehind or do I have to use client script/JavaScript?
I want to read the csv file, validate that it is correct and then save the data into a database.
<INPUT id="inputFileName" type="file" runat="server">

---------------------

protected HtmlInputFile inputFileName;
 

private void btnValidateFile_Click(object sender, System.EventArgs e)

{

  if( inputFileName.PostedFile != null )

     LoadFromFile(inputFileName.PostedFile.FileName);

  else

  {

      // Output no fileName...

  }

}
 

protected void LoadFromFile(string _fileName)

{

  ArrayList RecordList = new ArrayList();

  string[] recordCollection;

  StreamReader cr = new StreamReader(_fileName)

  {

        while (cr.Peek() >=0)

	{

	  string tempString = cr.ReadLine();

	  recordCollection = tempString.Split(';');

	  RecordList.Add(recordCollection[0]);

	}

	cr.Close();

       .......

}

Open in new window

0
Comment
Question by:Oskowich
  • 2
2 Comments
 

Author Comment

by:Oskowich
ID: 24020077
.NET 1.1
Visual Studio 2003
0
 

Accepted Solution

by:
Oskowich earned 0 total points
ID: 24021310
ArrayList RecordList = new ArrayList();
if (inputFileName.PostedFile != null)
{
      string[] recordCollection;
      using (System.IO.StreamReader sr = new System.IO.StreamReader(inputFileName.PostedFile.InputStream))
      {
            while (sr.Peek() >=0)
            {
                  string tempString = sr.ReadLine();
                  recordCollection = tempString.Split(';');
                  RecordList.Add(recordCollection[0]);
            }
            sr.Close();
      }
}
...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now