0

I have just made a model and controller which insert new row in a custom table to my umbraco database.

I based it on petapoco tutorial http://creativewebspecialist.co.uk/2013/07/16/umbraco-petapoco-to-store-blog-comments/

Despite the script executing without errors the row is not inserted into the table.

here's what I have:

namespace MyImport.Models
{
[TableName("MyImport_Uploads")]
[PrimaryKey("ID", autoIncrement = true)]
[ExplicitColumns]
public class ImportFile
{
    [Column("ID")]
    [PrimaryKeyColumn(AutoIncrement=true)]
    public int Id { get; set; }

    [Required]
    [Column("CompanyID")]
    public string CompanyId { get; set; }
    //public Guid CompanyId { get; set; }

    [Required]
    [Column("FilenameOriginal")]
    public string FilenameOriginal { get; set; }

    [Required]
    [Column("Filename")]
    public string Filename { get; set; }

    [Required]
    [Column("FileType")]
    public string FileType { get; set; }

    [Column("NumberOfItems")]
    public int NumberOfItems { get; set; }

    [Column("DateCreated")]
    public DateTime DateCreated { get; set; }

    [Column("DeleteExisting")]
    public bool DeleteExisting { get; set; }
}
}

And controller:

namespace MyImport.Controllers
{
    public class ImportController : SurfaceController
    {
        private Umbraco.Core.Persistence.UmbracoDatabase db = MyImport.Settings.UmbracoDbDSN;

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ImportExcel(ImportModel model)
    {

        var fileTypes = new List<string>
        {
            "text/xml",
            "application/xml",
        };
        string fileType = GetFileType(model.FileUpload.ContentType);

        if(model.FileUpload != null && model.FileUpload.ContentLength > 0)
        {
            string uploadDir = "~/imports";
            string origFileName = model.FileUpload.FileName;
            string extension = origFileName.Substring(origFileName.LastIndexOf('.') + 1);
            string pathToCheck = Path.Combine(Server.MapPath(uploadDir), origFileName);

            // Check to see if a file already exists with the
            // same name as the file to upload.
            if (!System.IO.File.Exists(pathToCheck))
            {
                string companyId = MyImport.Member.CompanyIdDummy;

                string tempfileName = companyId.ToLower() + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + "." + extension;
                pathToCheck = Path.Combine(Server.MapPath(uploadDir), tempfileName);
                model.FileUpload.SaveAs(pathToCheck);

                var importFile = new ImportFile
                {
                    CompanyId = companyId,
                    Filename = tempfileName,
                    FilenameOriginal = origFileName,
                    FileType = extension,
                    DateCreated = DateTime.UtcNow
                };

                db.Insert(importFile);
            }

            TempData.Add("Success", true);
        }

        //redirect to current page to clear the form
        return RedirectToCurrentUmbracoPage();
    }
}

Any suggestions? Thanks

Djensen
  • 1,337
  • 1
  • 22
  • 32
nickornotto
  • 1,946
  • 4
  • 36
  • 68

1 Answers1

0

I had the database wrongly set up.

Basically I had it as umbraco.sdf in App_Data folder while I intended to use my full MSSQL database where the table was created.

I recreated the project, reinstalled umbraco and HAD to choose Customize at the bottom of the screen where I was choosing setting up cms access. This is important as I was then directed to the screen where I could set up my database connection. Otherwise umbraco just install into SQL Server Express database which is stored in umbraco.sdf file in the project.

I hope this make sense for anyone looking for the solution to the same problem.

nickornotto
  • 1,946
  • 4
  • 36
  • 68