Link to home
Start Free TrialLog in
Avatar of assaultkitty
assaultkitty

asked on

HitCounter

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
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

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.
Avatar of assaultkitty
assaultkitty

ASKER

Changes made not results posted!
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.
Ok, I made the following changes:
created another class.
HitCounter.cs
This is the default.cs now.
Default.aspx.cs
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.
I don't understand how we got so far off track. Who suggested hardcoding anything?
No suggestion just about another discussion.
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.
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."
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.
I was still trying to use the set method though!
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.
Ok.  this is what I got so far.
HitCounter.cs
Default.aspx.cs
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(); }
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!
Here are the changes.
Default.aspx.cs
HitCounter.cs
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.
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.
I may need an update statement to query the database. Yes?
Default.aspx.cs
Default.aspx
HitCounter.cs
HitCounterDatabase.mdf
results.docx
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.
I can show you better than I can tell you.  Also, What will the update statement look like?
databaseC-.docx
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.
Not working.  Can you help me I have to turn in project at 12.
For a little while more. What code do you have to trigger an update?
OK.  All I know is that I am not getting the label to increment.  I just need help with that portion.
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?
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
}
I do not know how to postback a sql statement.  Sorry!
When I execute the query it says the cell is read-only
Post your update function.
It is the same one you gave me.  I did not change it.
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().
Are you done?
return totalNumberofHits();
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.
I have been doing this for so long I am getting a little confused.  Sorry.
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?
OK I will put the get method in the postback.
I wish we had more time. We are not on the same page right now.
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?
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.
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?
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?
ASKER CERTIFIED SOLUTION
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.