A
AlexGjnr
Guest
Hi i am new to C# and visual studio i am trying to read a .csv file into my Web Application MVC project and can not find the correct tutorials i have tried multiple different ways and keep getting the same errors on the same pieces of code listed below is the code I have.
[System.Web.Mvc.HttpPost]
public ActionResult Index(HttpPostedFileBase postedFile)
{
List<TeamModel> team = new List<TeamModel>();
string filePath = string.Empty;
if (postedFile != null)
{
string path = Server.MapPath("~/Uploads/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
filePath = path + Path.GetFileName(postedFile.FileName);
string extension = Path.GetExtension(postedFile.FileName);
postedFile.SaveAs(filePath);
//Read the contents of CSV file.
string csvData = System.IO.File.ReadAllText(filePath);
//Execute a loop over the rows.
foreach (string row in csvData.Split('\n'))
{
if (!string.IsNullOrEmpty(row))
{
team.Add(new TeamModel
{
ID = Convert.ToInt32(row.Split(',')[0]),
player_name = row.Split(',')[1],
quality = row.Split(',')[2],
overall = row.Split(',')[2],
club = row.Split(',')[2],
league = row.Split(',')[2],
nationality = row.Split(',')[2],
position = row.Split(',')[2],
age = row.Split(',')[2],
});
}
}
}
return View(team);
}
these are the errors I am getting can someone please help me all the tutorials I found was for VS 2010, 2015 none for 2017 not sure if that makes a difference. I am just trying to read a csv file from inside the project folder and put it in a List<> i do not need to upload the file
This is where I got the code:
Upload, Read and Display CSV file (Text File) data in ASP.Net MVC
Continue reading...
[System.Web.Mvc.HttpPost]
public ActionResult Index(HttpPostedFileBase postedFile)
{
List<TeamModel> team = new List<TeamModel>();
string filePath = string.Empty;
if (postedFile != null)
{
string path = Server.MapPath("~/Uploads/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
filePath = path + Path.GetFileName(postedFile.FileName);
string extension = Path.GetExtension(postedFile.FileName);
postedFile.SaveAs(filePath);
//Read the contents of CSV file.
string csvData = System.IO.File.ReadAllText(filePath);
//Execute a loop over the rows.
foreach (string row in csvData.Split('\n'))
{
if (!string.IsNullOrEmpty(row))
{
team.Add(new TeamModel
{
ID = Convert.ToInt32(row.Split(',')[0]),
player_name = row.Split(',')[1],
quality = row.Split(',')[2],
overall = row.Split(',')[2],
club = row.Split(',')[2],
league = row.Split(',')[2],
nationality = row.Split(',')[2],
position = row.Split(',')[2],
age = row.Split(',')[2],
});
}
}
}
return View(team);
}
- Error CS0246 The type or namespace name 'HttpPostedFileBase' could not be found (are you missing a using directive or an assembly reference?)
- Error CS0103 The name 'Server' does not exist in the current context
these are the errors I am getting can someone please help me all the tutorials I found was for VS 2010, 2015 none for 2017 not sure if that makes a difference. I am just trying to read a csv file from inside the project folder and put it in a List<> i do not need to upload the file
This is where I got the code:
Upload, Read and Display CSV file (Text File) data in ASP.Net MVC
Continue reading...