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. General Programming
  3. .NET (Core and Framework)
  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 .NET (Core and Framework)
questiondatabasedesignjsonworkspace
1 Posts 1 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
    
    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