Let us see how to login with Different Designations in asp.net using C#.
Step 1: Create a Table
create table employee( id int identity(101,1) primary key,
name varchar(20), username varchar(20), password varchar(10),
type char(20), createdon date)
Step 2: Insert Into Table
insert into employee values('Ashok','AshokSharma' , 'aks@123','SoftwareEngineer' ,getdate())
insert into employee values('Sejal','Sejal' , 'sejal@123','Manager' ,getdate())
insert into employee values('Vinod','VinodSharma' , 'vinod@123','Salesperson' ,getdate())
(Note: here i have taken thee post 'SoftwareEngineer', 'Manager' , 'Salesperson' )
you can see data in employee table :
Step 3: Open a new website
take the following pages to clear your logic:
- Login.aspx
- Manager.aspx
- Engineer.aspx
Step 4: LoginPage.aspx
(you can copy paste in your loign.aspx page)
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<br />
<br />
<br />
<br />
<br />
<br />
<center>
<table bgcolor="wheat">
<tr>
<td colspan="2" align="center">
LOGIN FORM
</td>
</tr>
<tr>
<td>
<asp:label id="Label1" runat="server" text="USERID"></asp:label>
</td>
<td>
<asp:textbox id="TextBox1" runat="server"></asp:textbox>
<br />
</td>
</tr>
<tr>
<td>
<asp:label id="Label2" runat="server" text="PASSWORD"></asp:label>
</td>
<td>
<asp:textbox id="TextBox2" runat="server"></asp:textbox>
</td>
</tr>
<tr align="center">
<td colspan="2">
<asp:button id="Button1" runat="server" text="Button" onclick="Button1_Click" />
</td>
</tr>
</table>
</center>
</form>
</body>
</html>
Step 5: Write code in Login.aspx.cs page
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from employee where username=@username and password=@password", con);
cmd.Parameters.AddWithValue("@username", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox2.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
string type = (dt.Rows[0]["type"]).ToString().Trim() ;
// store your type row cell value in variable to check condition
if (type =="Manager")
{
Session["USERID"] = TextBox1.Text;
Response.Redirect("Manager.aspx");
}
else if (type =="Salesperson")
{
Session["USERID"] = TextBox1.Text;
Response.Redirect("Salesperson.aspx");
}
else if (type =="SoftwareEngineer")
{
Session["USERID"] = TextBox1.Text;
Response.Redirect("Engineer.aspx");
}
else
{
Response.Write("Not Exists");
}
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(),
"validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
}
Note: (You have taken so to identify your page write some display in both the pages)
2. Manager.aspx (Hello this is manager page)
3. Engineer.aspx (Hello this is engineer page)
Step 6: Take the web.config file also
<connectionStrings>
<add name="constr" connectionString="Data Source=ashok;
Initial Catalog=ashokdb;
Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Step 7: Run your code successfully