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. </fieldset>
  16. </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. protected void btnLogin_Click(object sender, EventArgs e)
  10. {
  11. UserLogin();
  12. }
  13. protected void UserLogin()
  14. {
  15. using(SqlConnection con =new SqlConnection(ConnStr))
  16. {
  17. strqry = "Select * from User_MST";
  18. using(SqlCommand cmd = new SqlCommand(strqry))
  19. {
  20. using (SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, con))
  21. {
  22. DataTable dt = new DataTable();
  23. da.Fill(dt);
  24. RowCount = dt.Rows.Count;
  25. for (int i = 0; i < RowCount; i++)
  26. {
  27. User = dt.Rows[i]["UserName"].ToString();
  28. Password = dt.Rows[i]["UPassword"].ToString();
  29. if (User == txtUserName.Text && Password == txtPassword.Text)
  30. {
  31. Session["UserName"] = User;
  32. if (dt.Rows[i]["URoleID"].ToString() == "1")
  33. Response.Redirect("~/Demo/AdminDemo.aspx");
  34. else if (dt.Rows[i]["URoleID"].ToString() == "2")
  35. Response.Redirect("~/Demo/DemoUser.aspx");
  36. }
  37. else
  38. {
  39. lblmsg.Text = "Invalid User Name or Password!";
  40. }
  41. }
  42. }
  43. }
  44. }
  45. }
  46. }

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