Copied to clipboard

Flag this post as spam?

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


  • Daniel Larsen 116 posts 381 karma points
    Jan 10, 2016 @ 12:59
    Daniel Larsen
    0

    Razor database connection to Umbraco DB

    Hi,

    I am trying to make a simple display of some data from the Umbraco database, using only Razor, but it fails...

    What am I doing wrong? I made a Partial View with the following code.

    /Daniel

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{ 
        var DB = Database.Open("umbracoDbDSN"); 
        var DataTypes = DB.Query("SELECT * FROM cmsDataType");
    }
    
    @{
        foreach (var item in DataTypes)
        {  
            @item
        }
    }
    
  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Jan 10, 2016 @ 15:31
    Nik
    1

    When you say it fails, what do you mean?

  • Daniel Larsen 116 posts 381 karma points
    Jan 10, 2016 @ 15:37
    Daniel Larsen
    0

    Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS0103: Navnet 'Database' findes ikke i den aktuelle sammenhæng
    
    Source Error:
    
    
    Line 4:  
    Line 5:  @{ 
    Line 6:      var DB = Database.Open("umbracoDbDSN");    
    Line 7:     var DataTypes = DB.Query("SELECT * FROM cmsDataType");
    Line 8:     
    
  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Jan 11, 2016 @ 09:09
    Nik
    1

    I'm afraid I don't speak what looks like German so I'm having to rely on Google Translate, which says that message means "Does not exist in current context".

    Based on that I suspect you might be missing a reference/using statement in order to user the Database static class.

  • Graeme W 113 posts 289 karma points
    Jan 11, 2016 @ 12:38
    Graeme W
    1

    I have some views where I use peta poco and according to my comments I needed to include this declaratioin

    @using Umbraco.Core.Persistence;
    

    This is on version 6

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 11, 2016 @ 12:52
    Dennis Aaen
    1

    Hi Daniel,

    What if you do something like this would this work for you.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{ 
        var db = Umbraco.Core.ApplicationContext.Current.DatabaseContext.Database;
        var dataTypes = db.Query("SELECT * FROM cmsDataType");
    }
    
    @{
        foreach (var item in dataTypes)
        {  
            @item
        }
    }
    

    Hope this helps,

    /Dennis

  • Charles Afford 1163 posts 1709 karma points
    Jan 11, 2016 @ 12:58
    Charles Afford
    1

    Out of interest why are you accessing the database directly?

  • Daniel Larsen 116 posts 381 karma points
    Jan 11, 2016 @ 15:36
    Daniel Larsen
    0

    Nik, it is Danish and you got it right.

    Graeme W - it now complains about the Open statement.

    Dennis - It did not work.

    Compiler Error Message: CS1061: 'Umbraco.Web.UmbracoHelper' does not contain a definition for indeholder ikke en definition til 'Core', og der blev ikke fundet en udvidelsesmetode 'Core', der accepterer et første argument af typen 'Umbraco.Web.UmbracoHelper' (mangler du en 'using'-direktiv eller en assemblyreference?)
    

    Charles - I would like to make a comment function, and the first step is to access the DB. Do you have an other surgestion, that does not involve C#?

    Thank you all for your answers :-)

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 11, 2016 @ 17:39
    Dennis Aaen
    100

    Hi Daniel,

    I think that you need these namespaces

    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    

    That being said, could it be an option to have a custom table in the Umbraco database, then try see Warrens blog post here http://creativewebspecialist.co.uk/2013/07/16/umbraco-petapoco-to-store-blog-comments/

    Or here is a video that shows how to get data from a custom table.

    https://www.aptitude.co.uk/videos/using-custom-tables-data-in-umbraco/

    Hope this helps,

    /Dennis

  • Daniel Larsen 116 posts 381 karma points
    Jan 11, 2016 @ 18:54
    Daniel Larsen
    0

    Thank you! I think this will get me what i want :-)

    /Daniel

Please Sign in or register to post replies

Write your reply to:

Draft