Solved

C# Read from csv-file

Posted on 2009-03-30
2
1,110 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 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

16 Experts available now in Live!

Get 1:1 Help Now