Breaking

Search Here

01 May 2015

Get System IP Link

aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserRegistration.aspx.cs" Inherits="UserRegistration" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td colspan="2">Login Page</td>
        </tr>
        <tr>
            <td>UserName</td>
            <td>
                <input type="text" id="txtuser" runat="server" />
            </td>
         </tr>
        <tr>
             <td>Pwd</td>
            <td>
                <input type="text" id="txtpwd" runat="server" />
            </td>
         </tr>
        <tr>
             <td colspan="2">
                 <asp:Button ID="btnsubmit" runat="server" OnClick="btnsubmit_Click"  Text="Insert Data"/>
             </td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>


.cs file

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;
using System.Configuration;
using System.Net;


public partial class UserRegistration : System.Web.UI.Page
{
    string StrCon = "";
    SqlConnection Scon;
    SqlCommand Scmd;
    SqlDataAdapter sda;
    DataSet ds;
    DataTable dt;
    string ipaddress = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {

        

        //ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        //if (ipaddress == "" || ipaddress == null)
        //    ipaddress = Request.ServerVariables["REMOTE_ADDR"];
        //Response.Write("IP Address : " + ipaddress); 
        ////IPAdd = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        ////if (string.IsNullOrEmpty(IPAdd))
        //  //  IPAdd = Request.ServerVariables["REMOTE_ADDR"];
        //    //lblIP.Text = IPAdd;
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {

        //string strHostName = "";
        //strHostName = System.Net.Dns.GetHostName();
        //IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
        //IPAddress[] addr = ipEntry.AddressList;

        string hostName = Dns.GetHostName(); //Retrive the Name of HOST
        string myIP = Dns.GetHostByName(hostName).AddressList[0].ToString();
        
        //Console.WriteLine(hostName);
        // Get the IP
        //return addr[addr.Length - 1].ToString();
        //Response.Write(addr[2].ToString());

        StrCon =  ConfigurationManager.ConnectionStrings["Scon"].ConnectionString;
        Scon = new SqlConnection(StrCon);
        Scmd = new SqlCommand("insert into getipSysMob(Username,Passwords,SystemIpAddress,CreatedBy,CreatedDate) values('" + txtuser + "','" + txtpwd + "','" + myIP + "'," + 1 + ",GetDate())", Scon);
        Scon.Open();
        int i = Scmd.ExecuteNonQuery();
        Scon.Close();
        if (i != 0) 
        {
            Response.Write("Inserted Successfuly");
        }
        else
        {
            Response.Write("Not Inserted Successfuly");
        }
        
        //scmd.Parameters.AddWithValue("@Alphabet", ViewState["CurrentAlphabet"]);
        //sda = new SqlDataAdapter(scmd);
        //dt = new DataTable();
       // sda.Fill(dt);
    }
}

No comments:

Post a Comment

Hello all, if you have any doubt feel free comment

Comments