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

Saineshwar BageriPosted Dec 2, 2014, 3:11 AM
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 xxxx001
{
public partial class resources : LogIpAddress
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
}
}
Crown ClitPosted Dec 2, 2014, 5:13 AM
Crown ClitPosted Dec 2, 2014, 4:20 AM
Thank you kindly for your time Saineshwar
I am getting following error now while building the solution "Error 1 Inconsistent accessibility: base class 'xxxx001.LogIpAddress' is less accessible than class 'xxxx001.resources' C:\Data\xxxx\resources.aspx.cs 13 26 xxxx001'
any suggestions?
CC
Crown ClitPosted Dec 2, 2014, 3:07 AM
Hi Saineshwar
Thank you for your reply. I have created a class yu suggested but I am still a little confused how I call it from a page on pane load event. Here is e sample of my class and page code.
//----------------------------------------------
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 xxxx001
{
class LogIpAddress : System.Web.UI.Page
{
private string sqlCfgCon = WebConfigurationManager.ConnectionStrings["Db"].ConnectionString;
public LogIpAddress ()
{
//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();
}
}
}
}
//----------------------------------------------
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 xxxx001
{
public partial class resources : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LogIpAddress?????
}
}
}
}
//----------------------------------------------
Saineshwar BageriPosted Dec 1, 2014, 2:07 AM
Class Mainclass :System.Web.UI.Page // this class will inherit System.Web.UI.Page
{
Write Your Common logic Here.
}
And all you pages will be Inherit from Mainclass
public class LogIPAddress : Mainclass
{
}