如何根据db列中的值重定向到页面 [英] How do I redirect to page based on value in db column
问题描述
我有一个页面,其中两个不同的部门将登录名为1.Waste和2.Environment。如果用户部门是浪费,我希望页面重定向到特定页面,如果用户部门是环境,我希望页面重定向到另一个页面。在我的注册表中,我有列名,姓,电子邮件,密码和部门
如果部门=浪费
重定向到第1页其他
如果部门=环境
重定向到第2页
i得到错误类型'系统的例外.IndexOutOfRangeException'发生在System.Data.dll中但未在用户代码中处理
不确定如何将其实现为以下代码:
我尝试了什么:
代码
我尝试了什么:
i have a page where two different departments will login called 1.Waste and 2.Environment. i want the page to redirect to a specific page if the users department is Waste and also a different page if the users department is Environment. in my register table i have the columns name, surname , email , password and department
if department = Waste
Redirect to page 1 else
if department = Environment
Redirect to page 2
i get an error "An exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll but was not handled in user code"
Not sure how to implement this into the following code:
What I have tried:
Code
What I have tried:
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand("select * from [dbo].[PB_Register] where pb_Email= @Login_Email and pb_Password=@Login_Password", con);
cmd.Parameters.AddWithValue("@Login_Email",Login_Email.Text);
cmd.Parameters.AddWithValue("@Login_Password", Login_Password.Text);
Session["Username"] = Login_Email.Text;
con.Open();
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows) {
rd.Read();
if (rd["Departmet"].ToString() == "EPIP")
Response.Redirect("UPLOAD.aspx");
else
if (rd["Departmet"].ToString() == "Waste")
Response.Redirect("EMAIL_FILE.aspx");
} else {
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Username or Password incorrect');", true);
}
}
推荐答案
邋way的方式是在<$内抓住这个c $ c> WHILE 在此
The sloppy way would be to grab this within a WHILE
loop in some variation of this
if (rd.HasRows) {
while (rs.Read()) {
string RedirectURL = rd["ColumnName"];
}
}
Response.Redirect(RedirectURL);
我可能会重做这个更像是这样的东西;你只需要一行中的一个值,所以 ExecuteScalar
会更有效率。如果他们都不属于哪一组会怎么样?我为此提出了一个例外。
您可能需要修复一些拼写错误,因为我没有IDE检查我的输入
I would probably redo this to be something more like this; you only need one value from one row so ExecuteScalar
would be much more efficient. And what happens if they are in neither group? I threw in an Exception for that.
You may need to fix some typos as I do not have an IDE checking my typing
SqlConnection con = new SqlConnection(strConnString);
// Just grab the column you need
SqlCommand cmd = new SqlCommand("SELECT Departmet FROM [dbo].[PB_Register] WHERE pb_Email= @Login_Email AND pb_Password=@Login_Password", con);
cmd.Parameters.AddWithValue("@Login_Email",Login_Email.Text);
cmd.Parameters.AddWithValue("@Login_Password", Login_Password.Text);
Session["Username"] = Login_Email.Text;
con.Open();
// SqlDataReader rd = cmd.ExecuteReader();
var DepartmentName = cmd.ExecuteScalar();
if (DepartmentName != null) {
switch(DepartmentName.ToSting()) {
case "EPIP" : Response.Redirect("UPLOAD.aspx"); break
case "Waste" : Response.Redirect("EMAIL_FILE.aspx"); break
default: throw new ArgumentOutOfRangeException("Department", "The department does not have an assigned path"); break
}
} else {
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Username or Password incorrect');", true);
}
}
这篇关于如何根据db列中的值重定向到页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!