Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Dear C# mvc programmers

please I am new in C# mvc a bit help load data from csv file to loacal database. Thank you :)

CSv file has a value in the first row I have to avoid this value to skip error get loading the data

+-------+------+--------------+
|       | 300  |              |
+-------+------+--------------+
| age   | fname| lastName     |
+-------+------+--------------+
|   1   | Anna |   Smith      |
+-------+------+--------------+
|   2   |  Tom |  Cat         |
+-------+------+--------------+
|   3   |  Sam |   Fear       |
+-------+------+--------------+

class Detail
[Key]
Id {get;set;}
age{get;set;}
Fname{get;set;}
LastName{get;set;}

context 

 public DbSet<Detail> Detail{ get; set; }


What I have tried:

In Home controller

C#
public ActionResult Upload()
        {
           
            return View(db.Details.ToList());
        }



   [HttpPost]
        public ActionResult Upload(HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                StreamReader csvReader = new StreamReader(file.InputStream);
                string inputLine = "";
                while ((inputLine = csvReader.ReadLine()) != null)
                {
                    string[] values = inputLine.Split(new Char[] { ',' });
                    Detail records = new Detail();
                    records.age= values[0];
                    records.fname= values[1];
                    records.lastname= values[2];
//I am getting error here 'index was out of bounds of the array'
                   
                   db.Details.Add(records);
                    db.SaveChanges();
                    ViewBag.Success = "Data saved";
                }
                csvReader.Close();
            }



            return View("Upload");
        }



Upload.cshtml



HTML
@model IEnumerable<AppGarden.Models.Detail>

@{
    ViewBag.Title = "Upload";
}

<h3>Upload File</h3>

@using (Html.BeginForm("Upload",
                    "Home",
                    FormMethod.Post,
                    new { enctype = "multipart/form-data" }))
{
    <div>
        <label for="File">Upload File:</label>
        <input type="file" name="File" id="File"  /><br />

        <input type="submit" value="Upload File" /><br />
    </div>

   

   

}
Posted
Updated 18-Mar-19 12:05pm
v3
Comments
Herman<T>.Instance 7-Mar-19 6:09am    
LINQ -> .Expand()
dataService.GetCars().Expand(.....);

1 solution

If it were me, I'd find a library that already knows how to parse CSV files, anduse that. Simply splitting eachline on a comma will NOT always get the job done. Furthermore, putting the data into the database with an appropriate datatype is the only way to do it right.

I wrote a library that parses Excel and CSV files. You might want to check it out:

CSV/Excel File Parser - A Revisit[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900