How to get the Client IP address in asp.net?


Hi

Here is the simple code to do this task

protected void Button1_Click(object sender, EventArgs e)
{
string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

SqlCommand cmd = new SqlCommand(“insert into tblIpAddress(IPAddress)values(@IPAddress)”, con);
cmd.Parameters.AddWithValue(“@IPAddress”, clientIPAddress);
con.Open();
cmd.ExecuteNonQuery();
con.Close();

}

Advertisements

5 Responses to “How to get the Client IP address in asp.net?”

  1. sakthi Says:

    Hi,

    Here its work localhost but it host in server the ip address show blank

  2. Chandra Dev Says:

    Hi
    Please check it properly.I have used this code in one live project.It was working fine.

  3. trupti Says:

    This wil give server ip address and not visitor /client ip address

    • Chandra Dev Says:

      Thanks for updating me. I will check it.

  4. cara cantik Says:

    I really like reading an article that can make people think.
    Also, thank you for permitting me to comment!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: