Solved

C# Read from csv-file

Posted on 2009-03-30
2
1,119 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

839 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