New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting an empty datatable when I parse a CSV? #1097

Open
1RedOne opened this Issue Aug 14, 2018 · 2 comments

Comments

Projects
None yet
3 participants
@1RedOne

1RedOne commented Aug 14, 2018

First off, thanks for this awesome tool, it's really fantastic.

So, I'm new and totally suck at C#. I'm working on an MVC and had a working solution with LumenSoft, but they're not approved in our Software Catalog, so I'm porting over my function to CSVHelper.

When a user uploads a .CSV file like one of these
CSV1

ProgramName, ApplicationName, MachineName
7Zip,,FOXPC123
7Zip,,FOXPC234
7Zip,,FOXP345
7Zip,,FOXADMIN1
,Microsoft Office 2017, FOXPC123
,Microsoft Office 2017, FOXPC234
,Microsoft Office 2017, FOXPC124

or like this one, CSV2

Name,PagedMemorySize,ProcessName
amddvr,183881728,amddvr
amdow,2203648,amdow
ApplicationFrameHost,50364416,ApplicationFrameHost
AsSysCtrlService,1695744,AsSysCtrlService

This code in my controller (doing an MVC) answers the call.

//controller code
public ActionResult Upload(HttpPostedFileBase upload, FormCollection collection)
        {
            if (ModelState.IsValid)
            {

                if (upload != null && upload.ContentLength > 0)
                {

                    if (upload.FileName.EndsWith(".csv"))
                    {
                        Stream stream = upload.InputStream;
                        DataTable csvTable = new DataTable();
                        StreamReader sr = new StreamReader(stream);
                        var csv = new CsvReader(sr);
                        while (csv.Read())
                        {
                            var row = csvTable.NewRow();
                            foreach (DataColumn column in csvTable.Columns)
                            {
                                row[column.ColumnName] = csv.GetField(column.DataType, column.ColumnName);
                            }
                            csvTable.Rows.Add(row);
                        }
 
                        
                        ViewBag.Forms = collection;
                        return View(csvTable);
                        
                    }
                    else
                    {
                        ModelState.AddModelError("File", "This file format is not supported");
                        return View();
                    }
                }
                else
                {
                    ModelState.AddModelError("File", "Please Upload Your file");
                }
            }
            return View();
        }

And it's rednerded like this

if (Model != null)
    {
        <h2>Processed Results</h2>        
        <br /><hr />
        <div class="container">
            <div class="row">                
                    <table>
                        <thead>
                            <tr>
                                @foreach (DataColumn col in Model.Columns)
                                {
                                    <th>@col.ColumnName</th>
                                }
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (DataRow row in Model.Rows)
                            {
                                <tr>
                                    @foreach (DataColumn col in Model.Columns)
                                    {
                                        <td>@row[col.ColumnName]</td>
                                    }
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            </div>        
}

We don't receive any errors...but the resultant table is empty.

<table>
                        <thead>
                            <tr>
                            </tr>
                        </thead>
                        <tbody>
                                <tr>
                                </tr>
                                <tr>
                                </tr>
                                <tr>
                                </tr>
                                <tr>
                                </tr>
                                <tr>
                                </tr>
                                <tr>
                                </tr>
                                <tr>
                                </tr>
                                <tr>
                                </tr>
                                <tr>
                                </tr>
                                <tr>
                                </tr>
                                <tr>
                                </tr>
                                <tr>
                                </tr>
                                <tr>
                                </tr>
                                <tr>
                                </tr>
                                <tr>
                                </tr>
                                <tr>
                                </tr>
                                <tr>
                                </tr>
                        </tbody>
                    </table>

It feels like I've got to be close to a solution, as the number of empty rows matches the actual number of rows in my CSV.

Any thoughts on what I may have missed here? Also, bravo to you again for having such great docs, this is probably just me missing the fundamentals.

@gpduck

This comment has been minimized.

Show comment
Hide comment
@gpduck

gpduck Aug 15, 2018

It looks like the columns are never defined in the DataTable.

Poking around the CsvHelper code, it looks like maybe this would work:

...
var csv = new CsvReader(sr);

// Read the first line
csv.Read();
// Save it as the header record
csv.ReadHeader();

// Grab the header array from the context
foreach(string header in csv.Context.HeaderRecord)
{
    csvTable.Columns.Add(header);
}

while(csv.Read())
...

gpduck commented Aug 15, 2018

It looks like the columns are never defined in the DataTable.

Poking around the CsvHelper code, it looks like maybe this would work:

...
var csv = new CsvReader(sr);

// Read the first line
csv.Read();
// Save it as the header record
csv.ReadHeader();

// Grab the header array from the context
foreach(string header in csv.Context.HeaderRecord)
{
    csvTable.Columns.Add(header);
}

while(csv.Read())
...
@JoshClose

This comment has been minimized.

Show comment
Hide comment
@JoshClose

JoshClose Aug 15, 2018

Owner

Yes, that should work. You won't know the type from there though.

Why are you using a DataTable?

Owner

JoshClose commented Aug 15, 2018

Yes, that should work. You won't know the type from there though.

Why are you using a DataTable?

@JoshClose JoshClose added the question label Aug 15, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment