Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Windows.Forms;
using System.Data.SqlClient;

public partial class Login : System.Web.UI.Page
{
    
    SqlConnection con;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        ButtLogon.Focus();
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["sfs"].ConnectionString);
        
    }
    public string GetIpAddress()
    {
        string hostname=System.Net.Dns.GetHostName();
        System.Net.IPAddress[] ips = System.Net.Dns.GetHostAddresses(hostname);
        string Org_IpAddress = ips[1].ToString();

//after executing above statement program jumps up to the catch block n raise d exception

        return Org_IpAddress;
    }

    public bool ValidStudent(string course_string)
    { 
        string ip=GetIpAddress();
        bool b=false;
        SqlCommand cmd = new SqlCommand("select ip_address from student_login where course_string='" + course_string + "'",con);
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            if (dr[0].ToString() == ip)
            {
                b = true;
            }
        }
        con.Close();
        return b;
    }

    public SqlDataReader FetchDetails_frmString(string s)
    {
        string query = "select * from string_gen where string='" + s.Trim() + "'";
        SqlCommand cmm = new SqlCommand(query, con);
        con.Open();
        SqlDataReader dr = cmm.ExecuteReader();
        return dr;        
    }
    public int  StoreIpAddress(string ip,string course_string)
    {
        int i = 0;
        SqlCommand cmd = new SqlCommand("insert into student_login values('" + course_string + "','" + ip + "')");
        cmd.Connection = con;
        con.Open();
        i = cmd.ExecuteNonQuery();
        con.Close();
        return i;
    }
   string s;
    protected void ButtLogon_Click(object sender, EventArgs e)
    {
        try
        {
            bool check = ValidStudent(TBRNo.Text);
            if (check == false)
            {
                SqlDataReader dr = FetchDetails_frmString(TBRNo.Text);
                DataTable dt = new DataTable();
                
                dt.Load(dr);
                Session["dr"] = dr;
                Session["FetchDetails_frmString"] = dt;
                con.Close();
                if (dt.Rows.Count > 0)
                {
                    string ip = GetIpAddress();
                    int i = StoreIpAddress(ip, TBRNo.Text);
                    if (i > 0)
                    {
                        ClientScript.RegisterStartupScript(GetType(), "Onload", "alert('Login Successful!')", true);
                        Response.Redirect("~/Student/home.aspx");
                    }
                    else
                    { 
                        
                    }
                   
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "Onload", "alert('Access Denied.....This string does not exist')", true);
                }
            }
           else
            {
                ClientScript.RegisterStartupScript(GetType(), "Onload", "alert('You have Already login this Website')", true);
            }
        }
        catch (Exception o)
        {
            Label8.Text = o.Message;
            ClientScript.RegisterStartupScript(GetType(), "Onload", "alert('Access Denied.....Some Problem occured')", true);
        }
        finally 
        { 
            con.Close();         
        }
                }
    protected void TBRNo_TextChanged(object sender, EventArgs e)
    {

    }
}
Posted
Updated 31-Jan-12 1:13am
v2

string Org_IpAddress = ips[1].ToString();
For this to work there has to be at least two elements in the array.

if(ips.Length > 0) 
{
  string Org_IpAddress = ips[0].ToString();
}
if(ips.Length > 1) 
{
  string Org_IpAddress = ips[1].ToString();
}


In c# the firts valid index is 0 not 1 - it might help to think of index as an offset into the array.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Jan-12 16:17pm    
Good catch, a 5.
--SA
Espen Harlinn 31-Jan-12 16:22pm    
Thank you, Sergey!
use
C#
string Org_IpAddress = ips[0].ToString();
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Jan-12 16:18pm    
Disagree with one who voted 2. This is most likely a reason; I voted 5.
--SA
uspatel 1-Feb-12 0:04am    
Thanks.....
Espen Harlinn 31-Jan-12 16:23pm    
Agree, have a 5 :)
uspatel 1-Feb-12 0:05am    
thanks.....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900