Role Based login Form using SQL Database

In this see Role Based login From using sql database login Table data.

Create in Sql Database,
  1. User Login Table
    1. Create table User_MST  
    2. (  
    3.    UserID int identity (1,1) constraint PK_U_ID primary key,  
    4.    UserName nvarchar(30),  
    5.    UPassword nvarchar(30),  
    6.    URoleID int constraint FK_UR_ID foreign key references UserRole(RoleID)  
    7. )  
  2. UserRole Table
    1. Create table User_Role  
    2. (  
    3.    RoleID int identity (1,1) constraint PK_U_ID primary key,  
    4.    RoleName nvarchar(30)  
    5. )  

Follow step,

Solution Explorer -> Add -> New Item



Select Master Page and give Name -> User Login.Master



Now design for User Login Master,

Login.Master



Login.Master source

  1. <div>  
  2.     <fieldset style="width:auto" >  
  3.     <legend>User Login</legend>  
  4.     <asp:Table runat="server" >  
  5.         <asp:TableRow>  
  6.             <asp:TableCell>User Name</asp:TableCell><asp:TableCell><asp:TextBox runat="server" ID="txtUserName"></asp:TextBox></asp:TableCell>  
  7.         </asp:TableRow>  
  8.         <asp:TableRow>  
  9.             <asp:TableCell>User Password</asp:TableCell><asp:TableCell><asp:TextBox runat="server" ID="txtPassword" TextMode="Password"></asp:TextBox></asp:TableCell>  
  10.         </asp:TableRow>  
  11.         <asp:TableRow>  
  12.             <asp:TableCell></asp:TableCell><asp:TableCell><asp:Button runat="server" ID="btnLogin" Text="LOGIN" onclick="btnLogin_Click" />  </asp:TableCell>  
  13.         </asp:TableRow>  
  14.     </asp:Table>  
  15.       
  16.     </fieldset>  
  17. </div>  
Login.Master.cs Code,
  1. public partial class Login : System.Web.UI.MasterPage  
  2. {  
  3.     string ConnStr = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;  
  4.     string strqry,User,Password;  
  5.     int RowCount;  
  6.     protected void Page_Load(object sender, EventArgs e)  
  7.     {  
  8.   
  9.     }  
  10.   
  11.     protected void btnLogin_Click(object sender, EventArgs e)  
  12.     {  
  13.         UserLogin();  
  14.     }  
  15.   
  16.     protected void UserLogin()  
  17.     {  
  18.     using(SqlConnection con =new SqlConnection(ConnStr))  
  19.       {  
  20.           strqry = "Select * from User_MST";  
  21.           using(SqlCommand cmd = new SqlCommand(strqry))  
  22.           {  
  23.               using (SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, con))  
  24.               {  
  25.                   DataTable dt = new DataTable();  
  26.                   da.Fill(dt);  
  27.                   RowCount = dt.Rows.Count;  
  28.                   for (int i = 0; i < RowCount; i++)  
  29.                   {  
  30.   
  31.                       User = dt.Rows[i]["UserName"].ToString();  
  32.                       Password = dt.Rows[i]["UPassword"].ToString();  
  33.   
  34.                       if (User == txtUserName.Text && Password == txtPassword.Text)  
  35.                       {  
  36.   
  37.                           Session["UserName"] = User;  
  38.                           if (dt.Rows[i]["URoleID"].ToString() == "1")  
  39.                               Response.Redirect("~/Demo/AdminDemo.aspx");  
  40.                           else if (dt.Rows[i]["URoleID"].ToString() == "2")  
  41.                               Response.Redirect("~/Demo/DemoUser.aspx");  
  42.   
  43.                       }  
  44.                       else  
  45.                       {  
  46.                           lblmsg.Text = "Invalid User Name or Password!";  
  47.                       }  
  48.                   }  
  49.               }  
  50.           }  
  51.         
  52.       }  
  53.       
  54.     }  
  55. }  

UserLogin.aspx

Now, add UserLogin.aspx using master page



Now looking Here your Login Page



And Also Add two New from

  1. AdminDemo.aspx
  2. DemoUser.aspx

Now, you fill in textbox Username & Password as Role Admin or Employee name
And click login button and Redirect to Admin Page OR Employee Page

Login for Admin

UserName : Rakesh
Password : rakesh
and Click button LOGIN and you see Admin From



Employee Login

For Employee Login fill in login textbox,
Username : chavda
Password: chavda

and Click button LOGIN and you see Employee From