Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. How do I access a csv file without hardcoding it in my code?

How do I access a csv file without hardcoding it in my code?

Scheduled Pinned Locked Moved ASP.NET
questiondatabasedesignjsonworkspace
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Norris Chappell
    wrote on last edited by
    #1

    I want to use: using (StreamReader reader = new StreamReader(NRFileUpload.FileContent)) instead of using (StreamReader reader = File.OpenText(@"c:\Users\pzd74f\Downloads\FinalLabor2015.csv")) However when I try to insert this code: protected void ImportButton_Click(object sender, EventArgs e) { if (this.FileUpload.HasFile) { var extension = Path.GetExtension(FileUpload.FileName); if (extension == ".csv") { using StreamReader reader = new StreamReader(FileUpload.FileContent)) } } } It just imports the file and the rest of my code is not executed. Here is my code:

    using System;
    using System.Configuration;
    using System.Data;
    using System.IO;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Globalization;

    namespace StaffingWebParts.VisualWebPart1
    {
    public partial class VisualWebPart1UserControl : UserControl
    {

        protected void Page\_Load(object sender, EventArgs e)
        {
    
            if (!Page.IsPostBack)
            {
          //      this.QueryStaff();
                gvNewResource.DataSource = QueryStaff();
                gvNewResource.DataBind();
    
    
            }
        }
    
        private static char\[\] Colon = new char\[\] { ',' };
        private DataTable QueryStaff()
    
        {
            const int nameColumnIndex = 1;
            const int hoursColumnIndex = 9;
    
    
    
            using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings\["SQLStaffingConn"\].ConnectionString))
            using (var cmd = new SqlCommand("", conn))
            using (var dataAdapter = new SqlDataAdapter(cmd))
            using (var cmdBuilder = new SqlCommandBuilder(dataAdapter))
            {
                // create temporary table in database
               conn.Open();
               cmd.CommandText = "CREATE TABLE #TempTable(Name nvarchar(100) NOT NULL, Hours decimal(6, 2) NOT NULL);";
               cmd.ExecuteNonQuery();
    
                // create a DataTable and let the DataAdapter create appropriate columns for it
                DataTable dataTable = new DataTable();
                cmd.CommandText = "SELECT \* FROM #TempTable;";
                dataAdapter.Fill(dataTable);
    
                // read the CSV-records into the DataTable
                dataTable.BeginLoadData();
    
    
             //  using (StreamReader reader = new StreamReader(NRFileUpload.FileContent
    
    A 1 Reply Last reply
    0
    • N Norris Chappell

      I want to use: using (StreamReader reader = new StreamReader(NRFileUpload.FileContent)) instead of using (StreamReader reader = File.OpenText(@"c:\Users\pzd74f\Downloads\FinalLabor2015.csv")) However when I try to insert this code: protected void ImportButton_Click(object sender, EventArgs e) { if (this.FileUpload.HasFile) { var extension = Path.GetExtension(FileUpload.FileName); if (extension == ".csv") { using StreamReader reader = new StreamReader(FileUpload.FileContent)) } } } It just imports the file and the rest of my code is not executed. Here is my code:

      using System;
      using System.Configuration;
      using System.Data;
      using System.IO;
      using System.Data.Common;
      using System.Data.SqlClient;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;
      using System.Globalization;

      namespace StaffingWebParts.VisualWebPart1
      {
      public partial class VisualWebPart1UserControl : UserControl
      {

          protected void Page\_Load(object sender, EventArgs e)
          {
      
              if (!Page.IsPostBack)
              {
            //      this.QueryStaff();
                  gvNewResource.DataSource = QueryStaff();
                  gvNewResource.DataBind();
      
      
              }
          }
      
          private static char\[\] Colon = new char\[\] { ',' };
          private DataTable QueryStaff()
      
          {
              const int nameColumnIndex = 1;
              const int hoursColumnIndex = 9;
      
      
      
              using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings\["SQLStaffingConn"\].ConnectionString))
              using (var cmd = new SqlCommand("", conn))
              using (var dataAdapter = new SqlDataAdapter(cmd))
              using (var cmdBuilder = new SqlCommandBuilder(dataAdapter))
              {
                  // create temporary table in database
                 conn.Open();
                 cmd.CommandText = "CREATE TABLE #TempTable(Name nvarchar(100) NOT NULL, Hours decimal(6, 2) NOT NULL);";
                 cmd.ExecuteNonQuery();
      
                  // create a DataTable and let the DataAdapter create appropriate columns for it
                  DataTable dataTable = new DataTable();
                  cmd.CommandText = "SELECT \* FROM #TempTable;";
                  dataAdapter.Fill(dataTable);
      
                  // read the CSV-records into the DataTable
                  dataTable.BeginLoadData();
      
      
               //  using (StreamReader reader = new StreamReader(NRFileUpload.FileContent
      
      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      You can allow the user to select a file using the FileUpload control. This will allow you to choose a file on the client machine. Try these tutorials - File Upload with ASP.NET[^] FileUpload Control[^]

      Mobile Apps - Sound Meter | Color Analyzer | SMBC | Football Doodles

      N 1 Reply Last reply
      0
      • A Abhinav S

        You can allow the user to select a file using the FileUpload control. This will allow you to choose a file on the client machine. Try these tutorials - File Upload with ASP.NET[^] FileUpload Control[^]

        Mobile Apps - Sound Meter | Color Analyzer | SMBC | Football Doodles

        N Offline
        N Offline
        Norris Chappell
        wrote on last edited by
        #3

        Hi, Thanks for the information. I was able to get the correct code but where do I put this in my code?

        protected void UploadButton_Click(object sender, EventArgs e)
        {
        if (NRFileUploadControl.HasFile)
        {
        try
        {
        string filename = Path.GetFileName(NRFileUploadControl.FileName);
        NRFileUploadControl.SaveAs(Server.MapPath("~/") + filename);
        StatusLabel.Text = "Upload status: File uploaded!";
        }
        catch (Exception ex)
        {
        StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
        }
        }

        It just runs this part and don't execute the rest of my code. Thanks, Norris

        N 1 Reply Last reply
        0
        • N Norris Chappell

          Hi, Thanks for the information. I was able to get the correct code but where do I put this in my code?

          protected void UploadButton_Click(object sender, EventArgs e)
          {
          if (NRFileUploadControl.HasFile)
          {
          try
          {
          string filename = Path.GetFileName(NRFileUploadControl.FileName);
          NRFileUploadControl.SaveAs(Server.MapPath("~/") + filename);
          StatusLabel.Text = "Upload status: File uploaded!";
          }
          catch (Exception ex)
          {
          StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
          }
          }
          }

          It just runs this part and don't execute the rest of my code. Thanks, Norris

          N Offline
          N Offline
          Norris Chappell
          wrote on last edited by
          #4

          Abhinav, I want to thank you for provided those websites. If you have the time could you look at my code and let me know where I need to insert the new code?

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups