Crown Clit

Crown Clit

  • NA
  • 14
  • 8.4k

global class for different methods to be called from .aspx

Nov 30 2014 8:47 PM

Hello
I'm the beginner in this field. I am trying to write some code to record web page visited by the user. At this moment every page has the same code to do this task and it works fine. Now, I have decided to modify it a little bit and I need to go to every .cs file and do the same change. This is giving me the Sh!*$.

It has to be a better way. So I have tried to create a separate class called "LogIPAddress.cs" and my idea was to call it from every page passing a page name parameter into that class and let it record visitor IP against visited page. However it is not as simple as copy paste :-) and I am having troubles.


here is a code for a single page. this works OK
// ------------------------------------------------------------------------------------------------------------

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


namespace XXX001
{
    public partial class tutorial : System.Web.UI.Page
    {
        //  Line below gets database connection string from web.config file
        private string sqlCfgCon = WebConfigurationManager.ConnectionStrings["Db"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //This will get a visitor ip addres
                string visitorIP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (visitorIP == "" || visitorIP == null)
                    visitorIP = Request.ServerVariables["REMOTE_ADDR"];
                //-----------------------
                //Replace IP if ::1
                if (visitorIP == "::1")
                {
                    visitorIP = "999.999.999.999";
                }
                //-----------------------
                //This will create sql connection, sql command from a stored procedure and assign input parameters
                SqlConnection sqlCon1A = new SqlConnection(sqlCfgCon);
                SqlCommand sqlCmd1A = new SqlCommand("usp_AddWebSiteLog", sqlCon1A);
                sqlCmd1A.CommandType = CommandType.StoredProcedure;

                sqlCmd1A.Parameters.Add(new SqlParameter("@VisitorHostName", Request.ServerVariables["REMOTE_HOST"]));
                sqlCmd1A.Parameters.Add(new SqlParameter("@VisitorIP", visitorIP));
                sqlCmd1A.Parameters.Add(new SqlParameter("@ReferrerURL", Request.ServerVariables["REMOTE_USER"]));
                sqlCmd1A.Parameters.Add(new SqlParameter("@PageName", HttpContext.Current.Request.Url.AbsolutePath.ToString()));
                try
                {
                    ////This will open connection and execute storedprocedure
                    sqlCon1A.Open();
                    SqlDataReader sqlDr = sqlCmd1A.ExecuteReader();
                }
                catch (Exception err)
                {
                    throw (err);
                }
                finally
                {
                    sqlCon1A.Close();
                }
            }
        }
    }
}

 //------------------------------------------------------------------------------------------------------------

 

and here are my "LogIPAddress.cs" attempts

 

 //------------------------------------------------------------------------------------------------------------

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

namespace XXX001
{
    public class LogIPAddress : System.Web.UI.Page
    {
        public string sqlCfgCon = WebConfigurationManager.ConnectionStrings["Db"].ConnectionString;
       
        public  ????? (string PageName) // NO IDEA WHERE TO START TO MAKE THIS WORK
        {

        }
    }
}
//------------------------------------------------------------------------------------------------------------

Could somebody please help me out?

CC


Answers (5)