Solved

C# Read from csv-file

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
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…

813 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

14 Experts available now in Live!

Get 1:1 Help Now