This way
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.StoreVisitorIPDetails();
}
}
private void StoreVisitorIPDetails()
{
string ipAddress;
// Gets Client IP Address
ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipAddress == "" || ipAddress == null)
ipAddress = Request.ServerVariables["REMOTE_ADDR"];
string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand("StoreIPAddressDetails");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@IPAddress", ipAddress);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
con.Dispose();
}
SQL:
CREATE TABLE [dbo].[StoreIPAddress](
[Id] [int] IDENTITY(1,1) NOT NULL,
[IpAddress] [varchar](50) NULL,
[VisitedDate] [datetime] NULL
) ON [PRIMARY]
GO
Stored Procedure:
CREATE PROCEDURE StoreIPAddressDetails
@IPAddress VARCHAR(50)
AS
BEGIN
INSERT INTO StoreIPAddress
(IpAddress
,VisitedDate)
VALUES(@IPAddress
,GETDATE())
END
GO
ScreenShot
