In this post How to Display Last Login by User in Asp.Net
Important Namespaces Used ------------------------- using System.Data; using System.Data.SqlClient; ------------------------- Login Button Click Event ------------------------- String mycon = "Data Source=sk-pc;Initial Catalog=LoginDatabase;Integrated Security=True"; SqlConnection scon = new SqlConnection(mycon); String myquery = "select * from LoginDetail where username='" + TextBox1.Text + "'"; SqlCommand cmd = new SqlCommand(); cmd.CommandText = myquery; cmd.Connection = scon; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; DataSet ds = new DataSet(); da.Fill(ds); String uname; String pass; String lastlogin; if (ds.Tables[0].Rows.Count > 0) { uname = ds.Tables[0].Rows[0]["username"].ToString(); pass = ds.Tables[0].Rows[0]["password"].ToString(); lastlogin = ds.Tables[0].Rows[0]["lastlogin"].ToString(); scon.Close(); if (uname == TextBox1.Text && pass == TextBox2.Text) { if (lastlogin == "0") { lastlogin = "You Have Logged in Website First Time"; Session["firsttime"] = 1; } setlastlogin(lastlogin); Session["username"] = uname; Session["lastlogin"] = lastlogin; Response.Redirect("MainPage.aspx"); } else { Label3.Text = "Invalid Username or Password - Relogin with Correct Username Password- No. of Attemps Remaining : " ; } } else { Label3.Text = "Invalid Username or Password - Relogin with Correct Username Password "; } ---------------------------------- User Defined Method --------------------- private void setlastlogin(String lastlogin1) { String mycon = "Data Source=sk-pc; Initial Catalog=LoginDatabase; Integrated Security=True"; String updatedata = "Update LoginDetail set lastlogin='" + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString() + "' where username='"+TextBox1.Text+"'"; SqlConnection con = new SqlConnection(mycon); con.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = updatedata; cmd.Connection = con; cmd.ExecuteNonQuery(); } ------------------- Main Page ( Page Load Coding ) -------------------------------- if (Session["username"] == null) { Response.Redirect("Default.aspx"); } else { Label2.Text = "Welcome " + Session["username"].ToString(); if (Session["lastlogin"].ToString() == "You Have Logged in Website First Time") { Label3.Text = "You Have Logged in Website For the First Time"; } else { Label3.Text = "You Have Last Logged In Website :" + Session["lastlogin"].ToString(); } } ------------------------------ Logout Button Click Coding --------------------------- Session.Abandon(); Response.Redirect("Signout.aspx"); ------------------------