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!
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
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;
}
}
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.
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.
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
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
Hello Thanks for the reply,
Here is my solution, Is it just a CS file I need to create?
Hello all,
Following the tutorial mentioned above I'm getting the following error;
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;
I then added the following to my home.cshtml in views
3) Created new store procedure to view the count
4) Created a new file in storeproc called retrieveviewcountfromstore_procedure.cs with the following;
5) Added this to bottom of my home page
Any help would be appreciated
Matt
In your first code block you have 2 namespaces. Remove the namespace that says
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.
Line;
To
But still no joy.
Morning all,
Is anyone able to point me in the right direction in getting this working?
Thanks.
Try
Hello Paul,
Thanks for the reply.
I'm still getting the following;
The name 'ExternalTest' does not exist in the current context
Thanks
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
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.
is working on a reply...