HitCounter

assaultkitty
assaultkitty used Ask the Experts™
on
I have the program up on the server.  Now, I need to get the database to set and get the method to the server.  Please preview this code and give me some suggestions.  No mean experts allowed!  This is homework.  I have come a long way baby!  But, I have a longer way to go!
Default.aspx.cs
Default.aspx
HitCounterDatabase.mdf
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015

Commented:
Please don't include my user name in the question. That's an EE no-no.

Looking at your files, I see a disconnect between the SimpleProperty class and the _Default class. The goal of the assignment is to "get" or "set" the number of hits FROM code in the _Default class by using the SimpleProperty class. Understand that these two classes would normally be in separate files. That way different pages in the same web application can use the SimpleProperty class. Putting them together in the same file should work for your purposes however.

Starting with the SimpleProperty class, you will want to put your code to retrieve the hits number from the database inside a function that returns an integer result. This function should be within the SimpleProperty class, not in a separate class like UsesSimpleProperty. You will want it to be the _Default class that "uses" the SimpleProperty class to retrieve or set the hits. Follow?

Then, in the _Default class, to "get" the hits number from the SimpleProperty class you will call the public property MyTotalNumberofHits using something similar to what was done in the UsesSimpleProperty class with these two lines:

            SimpleProperty example = new SimpleProperty();
            // Gets the property.
            int anumber = example.MyTotalNumberofHits;

These lines can go in the Page_Load section of the _Default class. Once the anumber variable is filled in with a value, you can send it to a control on your web page for display.

Author

Commented:
Changes made not results posted!
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Top Expert 2015

Commented:
It could be made to work the way you have it with a few simple changes, but it would not be a proper solution based on the instructions in the assignment. The assignment calls for two classes to be created, you have three. So, the other thing I suggested in the last post still needs to be done, i.e. move all the code you have inside the UsesSimpleProperty class to a function that is inside the SimpleProperty class. The function should return an integer. You can call the function getMyNumber or something similar, so:

private int getMyNumber()
{
      //SQL code to get the number from the database goes here
      return returnValue  //return the integer
}

Get rid of the UsesSimpleProperty class so you are in compliance with the assignment.

Author

Commented:
Ok, I made the following changes:
created another class.
HitCounter.cs

Author

Commented:
This is the default.cs now.
Default.aspx.cs

Author

Commented:
Ok. if we are on track where do we go because we are not connecting.  As, suggested Mr. Patel hardcoding is not the proper way to do software because of future changes.
Top Expert 2015

Commented:
I don't understand how we got so far off track. Who suggested hardcoding anything?

Author

Commented:
No suggestion just about another discussion.
Top Expert 2015

Commented:
So, do you want to have the hits counter in a separate file or in the same file as the _Default class. You have a miss-mosh right now.

Author

Commented:
You told me to separate the classes.  I did not know how to do it any other way.  I like it the other way.  I still works like it is now.  You said, "Understand that these two classes would normally be in separate files."
Top Expert 2015

Commented:
But I also said it would work the way you had it so we were working on getting it finished.

Okay, lets go with separate files, Default.aspx.cs to make the call and HitCounter.cs for retrieving the number from the database.

HitCounter.cs needs to have the property declaration that you currently have in the class SimpleProperty on Default.aspx.cs:

public int MyTotalNumberofHits
        {
            get { return totalNumberofHits; }
            // Assigns to the data member number.
            set { totalNumberofHits = value; }
        }

HitsCounter.cs does NOT need this. I have no idea where this came from:

public void setTotalNumberofHits(int numbers)
        {
            totalNumberofHits = numbers;
        }

Once you copy the property declaration over to HitsCounter.cs, you should delete theSimpleProperty class in it's entirety from Default.aspx.cs.

Lets get that far and post again.

Author

Commented:
I was still trying to use the set method though!
Top Expert 2015

Commented:
The set method will be in the HitsCounter.cs file after you copy over the property declaration. You will be able to use it. Right now, let's finish the "get" functionality. I don't want to feed you too much at one time.

Author

Commented:
Ok.  this is what I got so far.
HitCounter.cs
Default.aspx.cs
Top Expert 2015

Commented:
Two things.

1.) You deleted the SimpleProperty class as I expected, but what I did not anticipate is the removal of the code inside the Page_Load of the _Default class. Why? That needs to go back in.

2.) One last change to the HitsCounter.cs to complete the circuit. You have your getTotalNumberofHits() function retrieving the number for you and that's great. Now you need to complete the circuit by calling the function from your "get" property so other classes (like _Default) can use the property to get the value returned from getTotalNumberofHits().

Your "get" looks like this:  get { return totalNumberofHits; }

It needs to look like this:  get { return getTotalNumberofHits(); }

Author

Commented:
I can put it back.  I got an error because the simpleproperty class was removed.  I will put it back be advised that there will be two error!

Author

Commented:
Here are the changes.
Default.aspx.cs
HitCounter.cs
Top Expert 2015

Commented:
The error is because of this line:
   
            SimpleProperty example = new SimpleProperty();

You are retrieving the value from the new class you created called HitCounter so you need to change SimpleProperty to HitCounter.

Also in the Default.aspx.cs file, you are retrieving the value into the variable called anumber. You will need to assign this value to a control's Text property on the page so you can see it.

Author

Commented:
Good news! I got the zero on the web page but it is not advancing.  I tried to refresh the page and starting the webpage but it is still at zero.

Author

Commented:
I may need an update statement to query the database. Yes?
Default.aspx.cs
Default.aspx
HitCounter.cs
HitCounterDatabase.mdf
results.docx
Top Expert 2015

Commented:
Unfortunately, I don not have a test platform that can read a database created in SQLExpress 2008. I have 2005 version, so the MDF file is useless to me.

Can you describe it? Is there just one column of type integer? What did you name the column?

Yes, you will need a similar function like you did for getTotalNumberofHits() to "set" the number in the database.

Author

Commented:
I can show you better than I can tell you.  Also, What will the update statement look like?
databaseC-.docx
Top Expert 2015

Commented:
That's as simple as it gets.

Lots of examples of update statements online, but it should be easy enough. Something like this should work:

"UPDATE totalNumberofHits SET totalNumberofHits=(totalNumberofHits+1)"

Your Select statement needs adjustment too but you had best wait until you have a value in the column.

Author

Commented:
Not working.  Can you help me I have to turn in project at 12.
Top Expert 2015

Commented:
For a little while more. What code do you have to trigger an update?

Author

Commented:
OK.  All I know is that I am not getting the label to increment.  I just need help with that portion.

Author

Commented:
When I did the update in the database I got an error.Can you pass me off to another expert since you do not have time?
Top Expert 2015

Commented:
The experts that participate in EE are just programmers that volunteer to help out. We have no contact with each other, no affiliations.  Our only connection is the forum itself, just like your connection to it. I'm sitting in my living room dozing off.

You are going to need something in Page_Load that runs only when the page is run for the first time, not on reloads. Did you write the function for the Update in HitCounter yet? Call it in page load of Default.aspx.cs.

if (!IsPostBack)
{
    //run the update function in HitCounter
}

Author

Commented:
I do not know how to postback a sql statement.  Sorry!

Author

Commented:
When I execute the query it says the cell is read-only
Top Expert 2015

Commented:
Post your update function.

Author

Commented:
It is the same one you gave me.  I did not change it.
Top Expert 2015

Commented:
I gave you an Update statement. You were supposed to include it in your function to update the database. Your update function would be very similar to your function getTotalNumberofHits().
Top Expert 2015

Commented:
Are you done?

Author

Commented:
return totalNumberofHits();
Top Expert 2015

Commented:
You have to understand the mechanisms at work in these functions and classes. Programming is an unforgiving discipline. One semi-colon out of place, one missing quote and it fails. I need to see the new code in order to help. At least copy and paste your getTotalNumberofHits() with the Update statement in the right place.

Author

Commented:
I have been doing this for so long I am getting a little confused.  Sorry.
Top Expert 2015

Commented:
It's literally 60 seconds worth of work to cut and paste your getTotalNumberofHits() function to create a new function. Do you have it yet?

Author

Commented:
OK I will put the get method in the postback.
Top Expert 2015

Commented:
I wish we had more time. We are not on the same page right now.

Author

Commented:
public int getTotalNumberofHits()
        {
            SqlConnection sqlConnection1 = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename='C:\\Course Technology\\CIS Programming\\ASP .NET Programming\\Data Files\\Chapter.10\\Chapter\\HitCounter\\HitCounter\\App_Data\\HitCounterDatabase.mdf'; Integrated Security=true; User Instance=true");
            SqlCommand cmd = new SqlCommand();
            int totalNumberofHits;

            cmd.CommandText = "SELECT COUNT(*)FROM Counter";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = sqlConnection1;

            sqlConnection1.Open();

            totalNumberofHits = (int)cmd.ExecuteScalar();

            sqlConnection1.Close();

            return totalNumberofHits; //return the integer
        }

I hope you are talking about this method?
Top Expert 2015

Commented:
I am. We need a second just like it in the HtiCounter.cs file right underneath this one. Call it setTotalNumberofHits(). Paste everything in exactly the same and swap out the Select statement for the Update statement I gave you earlier.

Author

Commented:
public int setTotalNumberofHits()
        {
            SqlConnection sqlConnection1 = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename='C:\\Course Technology\\CIS Programming\\ASP .NET Programming\\Data Files\\Chapter.10\\Chapter\\HitCounter\\HitCounter\\App_Data\\HitCounterDatabase.mdf'; Integrated Security=true; User Instance=true");
            SqlCommand cmd = new SqlCommand();
            int totalNumberofHits;

            cmd.CommandText = "UPDATE totalNumberofHits SET totalNumberofHits=(totalNumberofHits+1)";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = sqlConnection1;

            sqlConnection1.Open();

            totalNumberofHits = (int)cmd.ExecuteScalar();

            sqlConnection1.Close();

            return totalNumberofHits; //return the integer
        }

Done! Now what?

Author

Commented:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace HitCounter
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {  
            HitCounter hitCounter = new HitCounter();
            // Sets the property.
            hitCounter.MyTotalNumberofHits = 5;
           
            // Gets the property.
            lblTotalNumberofHits.Text = hitCounter.MyTotalNumberofHits.ToString();

            if (!IsPostBack)
            {        
                public int getTotalNumberofHits()
                {
                    SqlConnection sqlConnection1 = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename='C:\\Course Technology\\CIS Programming\\ASP .NET Programming\\Data Files\\Chapter.10\\Chapter\\HitCounter\\HitCounter\\App_Data\\HitCounterDatabase.mdf'; Integrated Security=true; User Instance=true");
                    SqlCommand cmd = new SqlCommand();
                    int totalNumberofHits;

                    cmd.CommandText = "SELECT COUNT(*)FROM Counter";
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = sqlConnection1;

                    sqlConnection1.Open();

                    totalNumberofHits = (int)cmd.ExecuteScalar();

                    sqlConnection1.Close();

                    return totalNumberofHits; //return the integer
       
            }   }
}    
}
  got this error.  I added }  but it gives me an error.  Can you help?
Top Expert 2015
Commented:
I'm sorry, I have work in the morning. Just when I think we are close to finishing, you submit your Default.aspx.cs with the getTotalNumberofHits() function on the page. That belongs in the HitCounter.cs page where it has always been. Why did you move it? Your not grasping the concepts yet. The Default page needs nothing but a Page_Load event with the code we discussed. Everything else goes on the HitCounter.cs page.

I had the following suggestions ready to post, but you appear to be so confused that I don't know if any of it will make sense at this point:

In the getTotalNumberofHits() function change your Select statement to:

"SELECT * FROM HitCounter";

In the property declaration in HitCounter.cs, change this line:

public int MyTotalNumberofHits
        {
            get { return getTotalNumberofHits(); }
            // Assigns to the data member number.
            set { totalNumberofHits = setTotalNumberofHits(); }
        }

In your Default.aspx.cs you have this:

protected void Page_Load(object sender, EventArgs e)
        {  
            HitCounter hitCounter = new HitCounter();
            // Sets the property.
            hitCounter.MyTotalNumberofHits = 5;
           
            // Gets the property.
            lblTotalNumberOfUsers.Text = hitCounter.MyTotalNumberofHits.ToString();
        }

Change it to this:

protected void Page_Load(object sender, EventArgs e)
        {  
            HitCounter hitCounter = new HitCounter();
            // Sets the property.
            if (!IsPostBack)
            {
                   hitCounter.MyTotalNumberofHits = hitCounter.MyTotalNumberofHits + 1;
            }
           
            // Gets the property.
            lblTotalNumberOfUsers.Text = hitCounter.MyTotalNumberofHits.ToString();
        }

There are a dozen reasons whay this could fail, but at least you can get credit for having the thing set up properly.

Good luck.

Author

Commented:
I am lost now that I have added the post back method I have an error.  I have pasted it.  Can I get some help with this final issue.  Also, the zero is gone now.!


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace HitCounter
{
   public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {  
         HitCounter hitCounter = new HitCounter();
            // Sets the property.
            hitCounter.MyTotalNumberofHits = 5;
           
            // Gets the property.
            lblTotalNumberofHits.Text = hitCounter.MyTotalNumberofHits.ToString();

            if (!IsPostBack)
            {        
                public int getTotalNumberofHits()
                {
                    SqlConnection sqlConnection1 = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename='C:\\Course Technology\\CIS Programming\\ASP .NET Programming\\Data Files\\Chapter.10\\Chapter\\HitCounter\\HitCounter\\App_Data\\HitCounterDatabase.mdf'; Integrated Security=true; User Instance=true");
                    SqlCommand cmd = new SqlCommand();
                    int totalNumberofHits;

                    cmd.CommandText = "SELECT COUNT(*)FROM Counter";
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = sqlConnection1;

                    sqlConnection1.Open();

                    totalNumberofHits = (int)cmd.ExecuteScalar();

                    sqlConnection1.Close();

                    return totalNumberofHits; //return the integer
       
            }   }
             
} }
}  


Error      1      } expected      C:\Course Technology\CIS Programming\ASP .NET Programming\Data Files\Chapter.10\Chapter\HitCounter\HitCounter\Default.aspx.cs      24      14      HitCounter


Error      2      Type or namespace definition, or end-of-file expected      C:\Course Technology\CIS Programming\ASP .NET Programming\Data Files\Chapter.10\Chapter\HitCounter\HitCounter\Default.aspx.cs      45      3      HitCounter

Error      3      Type or namespace definition, or end-of-file expected      C:\Course Technology\CIS Programming\ASP .NET Programming\Data Files\Chapter.10\Chapter\HitCounter\HitCounter\Default.aspx.cs      46      1      HitCounter

Author

Commented:
I am sorry.  I was so disappointed because I have never "not completed" a project.  With all that said I made 100% on the project.  Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial