M# Tutorials

Learn to build .NET applications with M#. Our step-by-step tutorials will get you up-to-speed rapidly.
If you already know ASP.NET and C#, you can master M# in a week or two.

Reading CSV File

Importing data is a vital aspect of many softwares. We often need to import data in web applications as well e.g. importing products, sales, orders etc. M# offers a static class called “CSVReader” which exposes public methods for CSV data import. In this lesson we will see how we can work with the import functionality in M#.

We import data to store it in a permanent form, usually in a database. The first and very important rule in data import is to have a proper data format, as without a format you cannot import useful and reliable data.

CSV Reader

M# provides this class under “MSharp.Framework.Services” namespaces. This class provides three overloads to read the CSV file and returns System.Data.DataTable type object. You can use “CastTo” method to cast the DataTable to a list of strongly typed entities.

Important: In order to use CastTo method on returned DataTable, the title of the columns in CSV file must match the “Name” of the properties of the entity. Otherwise, if the name doesn’t match, the properties will not be populated.

Above three overloads supports three different forms of CSV data as a first parameter.

1) A “Document” (For more information on documents, please read lesson “File” in chapter 2)

2) A “String” contains CSV data

3) A “System.IO.FileInfo” Type targeting a CSV file

The second parameter is used to instruct the reader that the CSV file contain headings of each column in the first row, so that the reader doesn’t try to treat it as data.

The third parameter instructs the reader about the number of columns a CSV file has. This parameter is optional has “0” as the default value and M# reader counts the CSV file columns internally if not specified.

In our lesson we will be using our database as the data import target. We will create a new entity “Product” in our “HelloWorld” project and will use this entity to import data.

We will create another entity “ProductImport” which will be used to keep history of imported files. This entity will have only two columns;

1) The File to be imported

2) The date and time of import

(For more information on entity management, please read lesson “Entity, Page, Module” in chapter 1.)

Product Import Entity

ProductImport entity has just two properties to keep track of import history.

Product Entity

Our “Product” entity is based on a very simple design. It contains reference of the file it is imported from in order to maintain relational data and has only four properties related to product detail, as shown below:

We will use our partial business logic class of ProductImport entity to read and import data. (For more information on partial classes, please read lesson “Partial Classes and Business Logic” in chapter 2).

Reading Data

We will read the data from the “ProductImport” file supplied by the user, as shown below:


The above code calls CastTo method, to convert System.Data.DataRowCollection to IEnumerbale of Product. This only works when the CSV File has columns Titles the same as the entity properties. You can also read the return DataTable for each row and can build objects as shown below: