Blog Views


Sunday, October 23, 2011

CSV double quotes

I just had a project that required us to download a file from a vendor and parse the CSV file and place it into an SQL database. Unfortunately, this is an issue in my case as the vendor is senting up the CSV file to have each row encased in double quotes. This is because their description has commas in it. To do this i had two options.


Option 1 was to code all of it by scratch and i found a very nice tutorial on how to do this here Parse CSV file with Double quotes

Option 2 is to use TextFieldParser in the Microsoft.VisualBasic namespace ( Yes you can implement this even in C#). Here is an example using TextFieldParser:

public void Parser()
{
string pathFile = @"C:\TestFolder\TestCatalog.txt";
string line = "";
TextFieldParser fieldParser = null;

try
{
fieldParser = new TextFieldParser(pathFile);
fieldParser.SetDelimiters(",");
string[] array = fieldParser.ReadFields();

while ((line = fieldParser.ReadLine()) != null)
{
MessageBox.Show("This line: " + line);
}

}

catch (Exception ex)
{
MessageBox.Show("Caught Exception: " + ex);
}
finally
{
if (fieldParser != null)
fieldParser.Close();
}

return stringStuff;
}


The above uses the comma as the delimiter because even though they are using the quotes, you still have the carriage return and the end of a line has no comma dictating end of row. For Example: "Gregg","Test","Software"
As you can see above, Software ends the row with no comma after the end double quote.

MVC – The dreaded "Multiple types were found that match the controller named"

So I have spent time on a project involving MVC and have this dreaded issue that many users seem to have with the routing once you change any of the defaults (for instance areas and namespaces). The error is "Multiple types were found that match the controller named"

After a lot of banging my head against the wall, crying and almost throwing my laptop out the window, i came across a web site by John Plummer which mentioned that VS likes to keep your origional executable
from when you first created the project and instead of renaming the exexecutable, it just creates another one. This is one of the usual major issues that this error can created.

Another issue that can cause this is the changing of namespaces and adding areas. If you add the new string to the end of your area registration page like below, it will route your area's to the correct controllers:

new { action = "Index", id = UrlParameter.Optional }, new string[] { "CISE.UserInterface.Controllers"}

Hope this helps others!!