How to Import a CSV File in ASP.NET

Written by jon guenther
  • Share
  • Tweet
  • Share
  • Email

You can use the StreamReader class in ASP.NET to import a comma-separated value, or CSV, file and read those values into one of several server-side text controls. One such popular server-side control is the ListBox. For example, you pass the values to ListBox control using methods like Read() to obtain file contents. Because these contents are treated as text, you can use methods of the String class, such as Replace(), to change the way the contents are displayed in the ListBox server-side control.

Skill level:
Moderately Challenging

Things you need

  • Microsoft Visual Studio 2010
  • Microsoft Windows Notepad

Show MoreHide


    Create a New CSV File

  1. 1

    Open Windows Notepad to create a new file.

  2. 2

    Type the following:

    1,John Smith,blue

    2,Fred Jones,purple

    3,Rita Chavez,yellow

    4,Belinda Simms,pink

  3. 3

    Save the file to the root of your C drive as "favcolors.csv" in the "File name" text box.

    Create a New Web Project

  1. 1

    Open Microsoft Visual Studio 2010 and from the "File" menu choose "New Project."

  2. 2

    Select Visual C# from the "Installed Templates" column and choose "ASP.NET Empty Web Application."

  3. 3

    Enter a name of your choice in the "Name" text box.

  4. 4

    Specify a location of your choice in the "Location" text box.

  5. 5

    Click "OK."

    Create the ASP.NET Page

  1. 1

    Right-click the project name in "Solution Explorer," then click "Add" and "New Item."

  2. 2

    Select "Web Form" from Visual C# Installed Templates and click "OK"

  3. 3

    Click the "Design" button to switch to the designer view.

  4. 4

    Double-click the "ListBox" control under the Standard tab of the Toolbox to add it to the page.

  5. 5

    Double-click the "Button" control under the Standard tab of the Toolbox to add it to the page.

  6. 6

    Right-click the button and choose "Properties," and change the Text Property to "Upload."

    Write the Code

  1. 1

    Double-click the button added to the designer page to display the code-behind page.

  2. 2

    Type the following at the top of the file with other "using" statements:

    using System.IO;

  3. 3

    Type the following code between the opening "{" and closing "}" brackets of the Button1_Click event:

    string fileName = @"C:\favcolors.csv";

         using (StreamReader strdr = File.OpenText(filename))
            String content;
            while ((content = strdr.ReadLine()) != null)
  4. 4

    Click the "Save All" icon to save your changes.

    Test the Program

  1. 1

    Press "F5" on the keyboard to run the program in Debugging mode.

  2. 2

    Click the "Upload" button.

  3. 3

    Check that the names listed in the CSV file display as separate lines in the ListBox control.

Tips and warnings

  • You can experiment using the String.Replace() method to remove or replace the commas in the imported file because the entire file contents are being read as a string.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.