Barrassment
Barrassment

Reputation: 75

How can I write to a DataTable?

I'm in need of a fresh pair of eyes here. I'm trying to extract details from a user-uploaded file, add them to a DataTable, then write them to my SQL Server table.

It all appears to work nicely but for the actual fields being added to the DataTable (rather crucial!). The code runs to completion but with a blank DataTable, so obviously nothing is being written to my table. I attach my Controller - I'm sure it's something obvious that I'm missing, but I can't determine why.

public class FileUploaderController : Controller
{

    public FileUploaderController()
    {
        db = new DatabaseContext();

    }

    public FileUploaderController(DatabaseContext context)
    {
        db = context;
    }

    private DatabaseContext db { get; }

    public ActionResult UploadFilesPartial()
    {
        return View();
    }


    [HttpPost]
    public ActionResult Upload(HttpPostedFileBase file)
    {

        var dt = CreateDataTable();

        if (file != null)
        {
            var fileName = file.FileName;
            var filePath = Path.GetFullPath(fileName);
            var fileType = Path.GetExtension(fileName);

            DataRow fileDetails = dt.NewRow();
            fileDetails["FileName"] = fileName;
            fileDetails["FilePath"] = filePath;
            fileDetails["FileType"] = fileType;

            UploadToDataBase(db, dt);
        }


        return RedirectToAction("NettingBatch", "Views");

    }


    private DataTable CreateDataTable()
    {

        System.Data.DataTable table = new DataTable("FileUploads");

        DataColumn id;
        id = new DataColumn
        {
            DataType = System.Type.GetType("System.Int32"),
            ColumnName = "Id",
            ReadOnly = true,
            Unique = true
        };
        table.Columns.Add(id);

        DataColumn name;
        name = new DataColumn
        {
            DataType = System.Type.GetType("System.String"),
            ColumnName = "FileName",
            AutoIncrement = false,
            Caption = "FileName",
            ReadOnly = false,
            Unique = false
        };
        table.Columns.Add(name);

        DataColumn path;
        path = new DataColumn
        {
            DataType = System.Type.GetType("System.String"),
            ColumnName = "FilePath",
            AutoIncrement = false,
            Caption = "FilePath",
            ReadOnly = false,
            Unique = false
        };
        table.Columns.Add(path);

        DataColumn type;
        type = new DataColumn
        {
            DataType = System.Type.GetType("System.String"),
            ColumnName = "FileType",
            AutoIncrement = false,
            Caption = "FileType",
            ReadOnly = false,
            Unique = false
        };
        table.Columns.Add(type);

        DataColumn[] PrimaryKeyColumns = new DataColumn[1];
        PrimaryKeyColumns[0] = table.Columns["Id"];
        table.PrimaryKey = PrimaryKeyColumns;

        return table;

    }




    private void UploadToDataBase(DbContext context, DataTable data)
    {
        try
        {
            var columnMappings = new List<Tuple<string, string>>
            {
                new Tuple<string, string>("Id", "Id"),
                new Tuple<string, string>("FileName", "FileName"),
                new Tuple<string, string>("FilePath", "FilePath"),
                new Tuple<string, string>("FileType", "FileType"),

            };

            PopulateHistoryTable.BulkCopyDataTableIntoDatabase(context, data, "[mdp].[FileUploads]",
                columnMappings);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.InnerException);
        }



    }


}

I expect the details of my test file that I upload on my view to be extracted and written back to FileUploads table. Actual results are blank.

Upvotes: 0

Views: 192

Answers (1)

LarsTech
LarsTech

Reputation: 81620

NewRow returns a row that matches your DataTable scheme, but it doesn't actually "add" it to the DataTable. Use the "Add" function of the Rows collection to do that:

DataRow fileDetails = dt.NewRow();
fileDetails["FileName"] = fileName;
fileDetails["FilePath"] = filePath;
fileDetails["FileType"] = fileType;
dt.Rows.Add(fileDetails);

Upvotes: 1

Related Questions