Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Chris King 5 posts 85 karma points
    1 week ago
    Chris King
    0

    Query custom table in Umbraco DB

    I was successful in creating the DB using NPoco but am having trouble getting the data from a function in my surface controller. I am trying to return a single email based on the query. I can't find a decent example of how to do this. I am pretty new to Umbraco and primarily a front end developer so I am giving this my best shot.

    Here is my query

        public string GetEmail(string Locations)       {
    
            using (var scope = _scopeProvider.CreateScope())
            {
                var db = scope.Database;
                var record = db.Query<ContactViewModel>("SELECT Location FROM EmailList WHERE [Location] = @Locations");
    
                return null;
            }
        }
    

    I am not sure how to wrap the query in a function. No matter what I do it just seems to fail.

  • Huw Reddick 655 posts 2146 karma points
    1 week ago
    Huw Reddick
    0

    What errors are you getting?

  • Huw Reddick 655 posts 2146 karma points
    1 week ago
    Huw Reddick
    100

    Try changing your query to

    var record = db.Single<ContactViewModel>("SELECT Location FROM EmailList WHERE [Location] = @0", locations);
    
  • Chris King 5 posts 85 karma points
    1 week ago
    Chris King
    0

    I will try that Huw, thank you!

  • Chris King 5 posts 85 karma points
    1 week ago
    Chris King
    0

    Worked like a charm Huw, thank you again!

  • Chris King 5 posts 85 karma points
    7 days ago
    Chris King
    0

    Huw, I do have another Question. I tried adding an attachment to the contact form using "public IFormFile Attachment { get; set; }" in my model. When it gets to the DB Query above it throws the error below. I am pretty sure it blows up on " db.Single".

    InvalidProgramException: Common Language Runtime detected an invalid program.

    Would you know any resolution to this?

Please Sign in or register to post replies

Write your reply to:

Draft