从两个不同的表登录后如何重定向到不同的页面? [英] How to redirect to different page after login from two different tables?
本文介绍了从两个不同的表登录后如何重定向到不同的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两张桌子: - 管理员和用户
管理员(管理员,用户名,密码)
用户(UserId,用户名,密码,角色).... [角色=:学生或老师]
我为管理员创建了一个登录表单用户..下面是Admin表中管理员登录的代码...我想知道我的代码应该做些什么更改,以便用户根据他们的角色(学生或老师)登录和重定向到他们各自的页面>
我尝试过:
I having two tables:- Admin and Users
Admin(AdminId, Username, Password)
Users(UserId, Username, Password, Role)....[Role=:Student or Teacher]
I have created One login form for both Admin and Users..below is the code of admin login from Admin table...I want to know what changes should I do in my code for Users to login and redirect to their respective page based on their role(student or teacher)
What I have tried:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class Login : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
bool flag = true;
public Login()
{
con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
cmd = new SqlCommand();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
con.Open();
cmd.CommandText = "select * from [ADMIN]";
cmd.Connection = con;
SqlDataReader rd = cmd.ExecuteReader();
while(rd.Read())
{
if(rd["USERNAME"].ToString()==TextBox1.Text && rd["PASSWORD"].ToString()==TextBox2.Text)
{
Session["ADMIN"] = rd["USERNAME"];
flag = false;
break;
}
}
if (flag == true)
Label1.Text = "Username and password invalid";
else
Response.Redirect("~/Admin_welcome.aspx");
}
catch(Exception ex)
{
Label1.Text = ex.Message;
}
}
}
推荐答案
管理员表正在为我创建问题所以我删除了它....现在检查我做出的解决方案: -
The admin table is creating problem for me so I removed it....now check the solution I made:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class Home : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
bool flag = true;
public Home()
{
con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
cmd = new SqlCommand();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogIn_Click(object sender, EventArgs e)
{
try
{
con.Open();
cmd.CommandText = "select * from [Users]";
cmd.Connection = con;
SqlDataReader rd = cmd.ExecuteReader();
if (txtUserName.Text == "admin" && txtPwd.Text == "admin")
{
Session["Username"] = txtUserName.Text;
Response.Redirect("Admin.aspx");
}
else
{
while (rd.Read())
{
if (rd["UserName"].ToString() == txtUserName.Text && rd["Password"].ToString() == txtPwd.Text)
{
Session["Username"] = rd["UserName"];
flag = false;
break;
}
}
if (flag == true)
lblMsg.Text = "Username and password invalid";
else
{
if(rd["Role"].ToString()=="Student")
Response.Redirect("Student.aspx");
else
Response.Redirect("Teacher.aspx");
}
}
}
catch(Exception ex)
{
lblMsg.Text = ex.Message;
}
}
}
它正在工作!!!!
It is working !!!!
这篇关于从两个不同的表登录后如何重定向到不同的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文