Hi friends, today I will say the solution for “Multiple types
of users in your application”.
Let us take an example so that you could understand it easily.
Example Software company website. They will be different types of users like
Manager, HR and Admin etc....
Then most of will be in ambiguity that “whether I must create 3
login pages for 3 users??” No, here there is a solution. In login table in the
database we will add a column “Role” so it will differentiate them in the
back-end.
Now comes to front-end. Here we will check the “Role” when the
user presses login button. Depending upon the “Role” we will redirect to that
particular page. I mean if manager then “Manager.aspx” , if HR then “HR.aspx”
and if Admin “Admin.aspx”.
Example code:
I have created a blood bank website in which they were three
types of users:-
1) User
2) Admin
3) Student
Login.aspx.cs:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data;
using
System.Data.SqlClient;
using
System.Web.Security;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
public
partial class _Default : System.Web.UI.Page
{
// Server
address
string str
= "Data
Source=SANTOSHKLPKL-HP\\SQLEXPRESS;Initial Catalog=blood_bank;Integrated
Security=True";
// Connection
SqlConnection
con = new SqlConnection();
protected void Page_Load(object
sender, EventArgs e)
{
//Making
session values “Null”
Session["Username"]
= null;
Session["Password"]
= null;
}
protected void login_Click1(object
sender, EventArgs e)
{
//Storing
textboxes values into sessions.
Session["Username"]
= tblogin.Text.Trim();
Session["Password"]
= tbpwd.Text.Trim();
try
{
string
mrole;
con.ConnectionString = str;
con.Open();
SqlDataReader
dr;
// Created a procedure “sp_login”
SqlCommand
cmd1 = new SqlCommand("sp_getlogin", con);
cmd1.CommandType = CommandType.StoredProcedure;
//
Assigning textboxes values to paramaters “@user” and “@pwd”
SqlParameter
x1 = new SqlParameter("@user", tblogin.Text.Trim());
SqlParameter
x2 = new SqlParameter("@pwd", tbpwd.Text.Trim());
// Adding
parameters
cmd1.Parameters.Add(x1);
cmd1.Parameters.Add(x2);
con.Close();
SqlCommand
cmd2 = new SqlCommand("select Role from Allusers where username ='"
+ tblogin.Text + "'", con);
SqlDataReader
dr1;
con.Open();
dr1 = cmd2.ExecuteReader();
dr1.Read();
//Assigning “Role” value to mrole string.
mrole = dr1["Role"].ToString();
con.Close();
con.Open();
dr = cmd1.ExecuteReader();
if
(!dr.HasRows)
{
Response.Write("<script>alert('Invalid
Username or
Password!');</script>");
}
else
{
con.Close();
if
(mrole == "ADMIN")
{
Response.Redirect("./Admin.aspx");
}
else
if (mrole == "USER")
{
Response.Redirect("./Display.aspx");
}
else
if (mrole == "STUDENT")
{
Response.Redirect("./Studentsearch.aspx");
}
}
}
catch (Exception)
{
Response.Write("<script> alert('Invalid Login details
!')</script>");
}
con.Close();
}
}
You can rate my class on right side top of my blog....
-Your's santoshklpkl
No comments:
Post a Comment