Copied to clipboard

Flag this post as spam?

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


  • Matt 358 posts 841 karma points
    Sep 27, 2018 @ 15:26
    Matt
    0

    Umbraco hit counter

    Hello all,

    I'm following this tutorial about how to create a hit counter.

    https://codeshare.co.uk/blog/how-to-create-a-page-view-hit-counter-in-umbraco/

    But I'm stuck!

    The part where it says the following "Then I would create a static method within your project somewhere that would call the stored procedure"

    I'm not sure where I should place this?

    Thanks in advance, Matt

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Sep 27, 2018 @ 16:06
    Jan Skovgaard
    0

    Hi Matt

    Are you using Visual studio along with following the tutorial? If so you should be able to create a new class anywhere in your project containing the code code Paul mentions...I think that is what he means :)

    If you're still in doubt could you please share a screendump of you Visual Studio solution - That will make it easier to figure out what context you're in :)

    Does this make sense? Looking forward to hearing from you!

    /Jan

  • Matt 358 posts 841 karma points
    Sep 28, 2018 @ 08:05
    Matt
    0

    Hello Thanks for the reply,

    Here is my solution, Is it just a CS file I need to create?

    enter image description here

  • Matt 358 posts 841 karma points
    Sep 28, 2018 @ 09:48
    Matt
    0

    Hello all,

    Following the tutorial mentioned above I'm getting the following error;

    Compiler Error Message: CS0103: The name 'CodeShare' does not exist in the current context

    I'm pretty sure I've not done it right but the tutorial doesnt give you much detail for a noob here is what I've done.

    1) Created my database

    2) Created the store procedure

    3) created a static method within my template I made a folder called storeproc and created retrieveviewcount.cs and added the following code;

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace ExternalTest.storeproc
    {
        using System.Data.SqlClient;
        using System.Web.Configuration;
    
        namespace CodeShare.Example
        {
            public static class HitCounter
            {
                public static void RecordView(int nodeId)
                {
                    using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString))
                    {
                        using (SqlCommand cmd = new SqlCommand("usp_record_view", conn))
                        {
                            cmd.CommandType = System.Data.CommandType.StoredProcedure;
                            cmd.Parameters.Add("node_id", System.Data.SqlDbType.Int).Value = nodeId;
                            conn.Open();
                            cmd.ExecuteNonQuery();
                        }
                    }
                }
            }
        }
    }
    

    I then added the following to my home.cshtml in views

    @{ 
        int nodeId = Umbraco.AssignedContentItem.Id;
        string cookieName = String.Format("PageView_{0}", nodeId);
        if (Session[cookieName] == null)
        {
            CodeShare.Example.RecordView(nodeId);
            Session[cookieName] = 1;
        }
    }
    

    3) Created new store procedure to view the count

    4) Created a new file in storeproc called retrieveviewcountfromstore_procedure.cs with the following;

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace ExternalTest.storeproc
    {
        public static int GetViewCount(int nodeId)
        {
            int viewCount = 0;
            using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("usp_get_view_count", conn))
                {
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.Add("node_id", System.Data.SqlDbType.Int).Value = nodeId;
                    conn.Open();
                    viewCount = (int)cmd.ExecuteScalar();
                }
            }
            return viewCount;
        }
    }
    

    5) Added this to bottom of my home page

    @CodeShare.Example.GetViewCount(Umbraco.AssignedContentItem.Id)
    

    Any help would be appreciated

    Matt

  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    Sep 28, 2018 @ 12:06
    Paul Seal
    0

    In your first code block you have 2 namespaces. Remove the namespace that says

        namespace CodeShare.Example
        {
          ...
        }
    
  • Matt 358 posts 841 karma points
    Sep 28, 2018 @ 12:18
    Matt
    0

    Hello Paul,

    Thanks for the quick reply,

    Still no joy I just get

    "CS0234: The type or namespace name 'RecordView' does not exist in the namespace 'ExternalTest.storeproc' (are you missing an assembly reference?)"

    I guess I need to update the following as well.

    @{ 
        int nodeId = Umbraco.AssignedContentItem.Id;
        string cookieName = String.Format("PageView_{0}", nodeId);
        if (Session[cookieName] == null)
        {
            CodeShare.Example.RecordView(nodeId);
            Session[cookieName] = 1;
        }
    }
    

    Line;

    CodeShare.Example.RecordView(nodeId);
    

    To

       ExternalTest.storeproc.RecordView(nodeId);
    

    But still no joy.

  • Matt 358 posts 841 karma points
    Oct 02, 2018 @ 09:20
    Matt
    0

    Morning all,

    Is anyone able to point me in the right direction in getting this working?

    Thanks.

  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    Oct 02, 2018 @ 12:38
    Paul Seal
    0

    Try

    ExternalTest.storeproc.HitCounter.RecordView(nodeId);
    
  • Matt 358 posts 841 karma points
    Oct 02, 2018 @ 13:11
    Matt
    0

    Hello Paul,

    Thanks for the reply.

    I'm still getting the following;

    The name 'ExternalTest' does not exist in the current context

    Thanks

  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    Oct 02, 2018 @ 13:37
    Paul Seal
    0

    Hi Matt

    I don't mean any offence by this, but I think you need to learn about classes and namespaces in C# as this will be very difficult and frustrating for you until you learn more.

    Once you have grasped the basics then this sort of thing will be easy for you.

    There are loads of free courses on YouTube and cheap ones on Udemy.

    I honestly don't mean to be rude, but I've been there and it is no fun programming that way.

    Kind regards

    Paul

  • Matt 358 posts 841 karma points
    Oct 02, 2018 @ 14:29
    Matt
    0

    Hello Paul,

    I have everything working now :)

    No offence taken, I'm using Udemy ( and following your youtube) but like everything just takes time to learn and is more months/years rather then quick couple of days.

    This is something a client wanted pretty urgently.

    Thanks.

Please Sign in or register to post replies

Write your reply to:

Draft