Display Details of User After Successfull Login

In this article, we will learn how to display the corresponding details of a user after a successful login. In this article, we use JavaScript for validation. First, we will create a login page where the user will provide their respective credentials, username and password. After a successful login, the control moves to the details page where all the information of that user is displayed.

Table Creation

Login1.jpg

Now let's move to the code.

Login.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="Login_related_details.Login" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml" >  
  4. <head runat="server">  
  5.     <title>Untitled Page</title>  
  6.     <script type="text/javascript" language="javascript">  
  7.     function Validate()  
  8.     {  
  9.     var UName=document.getElementById('TextBox_user_name');  
  10.     var Password=document.getElementById('TextBox_password');  
  11.     if((UName.value=='') || (Password.value==''))  
  12.     {  
  13.      alert('UserName or Password should not be blank');  
  14.      return false;  
  15.     }  
  16.     else  
  17.     {  
  18.       return true;  
  19.     }  
  20.     }  
  21.     </script>  
  22. </head>  
  23. <body>  
  24.     <form id="form1" runat="server">  
  25.     <div>  
  26.     <asp:Label ID="lb1" runat="server" Font-Bold="True" ForeColor="#FF3300"></asp:Label><br />  
  27.      <asp:Label ID="Label1" runat="server" Text="UserName" Font-Bold="True" Width="100px" BackColor="#FFFF66" ForeColor="#FF3300"></asp:Label>  
  28.         <asp:TextBox ID="TextBox_user_name" runat="server" ForeColor="#993300" Width="100px"></asp:TextBox><br />  
  29.         <asp:Label ID="Label2" runat="server" Text="Password" Font-Bold="True" Width="100px" BackColor="#FFFF66" ForeColor="#FF3300"></asp:Label>  
  30.         <asp:TextBox ID="TextBox_password" runat="server" ForeColor="#CC6600" TextMode="Password" Width="100px"></asp:TextBox><br />  
  31.         <asp:Button ID="btn_login" runat="server" Text="Login" Font-Bold="True" BackColor="#CCFF99"   OnClientClick="Validate()" onclick="btn_login_Click"/><br />  
  32.     </div>  
  33.     </form>  
  34. </body>  
  35. </html> 

Login.aspx.cs

  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13. using System.Data.SqlClient;  
  14. namespace Login_related_details  
  15. {  
  16.     public partial class Login : System.Web.UI.Page  
  17.     {  
  18.         string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;  
  19.         SqlCommand com;  
  20.         SqlDataAdapter sqlda;  
  21.         string str;  
  22.         DataTable dt;  
  23.         int RowCount;  
  24.         protected void btn_login_Click(object sender, EventArgs e)  
  25.         {  
  26.             string UserName = TextBox_user_name.Text.Trim();  
  27.             string Password = TextBox_password.Text.Trim();  
  28.             SqlConnection con = new SqlConnection(strConnString);  
  29.             con.Open();  
  30.             str = "Select * from Login";  
  31.             com = new SqlCommand(str);  
  32.             sqlda = new SqlDataAdapter(com.CommandText, con);  
  33.             dt = new DataTable();  
  34.             sqlda.Fill(dt);  
  35.             RowCount = dt.Rows.Count;  
  36.             for (int i = 0; i < RowCount; i++)  
  37.             {  
  38.                 UserName = dt.Rows[i]["UserName"].ToString();  
  39.                 Password = dt.Rows[i]["Password"].ToString();  
  40.                 if (UserName == TextBox_user_name.Text && Password == TextBox_password.Text)  
  41.                 {  
  42.                     Session["UserName"] = UserName;  
  43.                     Response.Redirect("Details.aspx");  
  44.                 }  
  45.                 else  
  46.                 {  
  47.                     lb1.Text = "Invalid User Name or Password! Please try again!";  
  48.                 }  
  49.             }   
  50.         }  
  51.     }  
  52. }

Details.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Details.aspx.cs" Inherits="Login_related_details.Details" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml" >  
  4. <head runat="server">  
  5.     <title>Untitled Page</title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.     <asp:Label ID="lb1" runat="server" Text="Label"></asp:Label>  
  11.      <h1><font color="olive">User Details</font></h1>  
  12.     <table border="1" style="border-collapse: collapse"  cellspacing="1">  
  13.     <tr>  
  14.       <td width="77" height="16" align="left" ><b><font size="2" color="red">Name:</font></b></td>  
  15.       <td width="77" height="16" align="left" ><b><font size="2"> <asp:Label  
  16.               ID="lbl_UserName" runat="server" Font-Bold="True"></asp:Label><br /></font></b></td>  
  17.     </tr>  
  18.     <tr>  
  19.       <td width="77" height="16" align="left" ><b><font size="2" color="red">Address:</font></b></td>  
  20.       <td width="77" height="16" align="left" ><b><font size="2"> <asp:Label  
  21.               ID="lbl_address" runat="server" Font-Bold="True"></asp:Label><br /></font></b></td>  
  22.     </tr>  
  23.     <tr>  
  24.       <td width="77" height="16" align="left" ><b><font size="2" color="red">Salary:</font></b></td>  
  25.       <td width="77" height="16" align="left" ><b><font size="2"> <asp:Label  
  26.               ID="lbl_sal" runat="server" Font-Bold="True"></asp:Label><br /></font></b></td>  
  27.     </tr>  
  28.     <tr>  
  29.       <td width="77" height="16" align="left" ><b><font size="2" color="red">Phone:</font></b></td>  
  30.       <td width="77" height="16" align="left" ><b><font size="2"> <asp:Label  
  31.               ID="lbl_phone" runat="server" Font-Bold="True"></asp:Label><br /></font></b></td>  
  32.     </tr>  
  33.     </table>  
  34.     </div>  
  35.     </form>  
  36. </body>  
  37. </html>

Details.aspx.cs

  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13. using System.Data.SqlClient;  
  14. namespace Login_related_details  
  15. {  
  16.     public partial class Details : System.Web.UI.Page  
  17.     {  
  18.         string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;  
  19.         string str;  
  20.         SqlCommand com;  
  21.         protected void Page_Load(object sender, EventArgs e)  
  22.         {  
  23.             lb1.Text = "<b><font color=Brown>" + "WELLCOME:: " + "</font>" + "<b><font color=red>" + Session["UserName"] + "</font>";  
  24.             SqlConnection con = new SqlConnection(strConnString);  
  25.             con.Open();  
  26.             str = "select * from Login where UserName='" + Session["UserName"] + "'";  
  27.             com = new SqlCommand(str, con);  
  28.             SqlDataAdapter da = new SqlDataAdapter(com);  
  29.             DataSet ds = new DataSet();  
  30.             da.Fill(ds);  
  31.             lbl_UserName.Text = ds.Tables[0].Rows[0]["UserName"].ToString();  
  32.             lbl_address.Text = ds.Tables[0].Rows[0]["address"].ToString();  
  33.             lbl_sal.Text = ds.Tables[0].Rows[0]["sal"].ToString();  
  34.             lbl_phone.Text = ds.Tables[0].Rows[0]["phone"].ToString();  
  35.         }  
  36.     }  
  37. }

Output

Login2.jpg

After providing the correct credentials:

Login3.jpg 


Similar Articles