我想将 select 语句的结果值检索到字符串变量中。实际上我做了成功的登录编码,但之后我想根据他们的角色重定向页面从检索到数据库,如果那是 ADMIN 那么它会转到 ADMIN HOME PAGE 如果这是 USER 然后它会转到 USER HOME PAGE。在我的代码下方,看看我错在哪里。
protected void btn_lpc_Login_Click(object sender, EventArgs e)
{
string strCon = ConfigurationManager.ConnectionStrings["sessionname"].ConnectionString;
string strSelect = "SELECT COUNT(*) FROM LOGIN_DETAILS WHERE EMAIL_ID=@EMAIL_ID AND PASSWORD =@PASSWORD";
SqlConnection con = new SqlConnection(strCon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSelect;
SqlParameter username = new SqlParameter("@EMAIL_ID", SqlDbType.NVarChar, 36);
username.Value = tbx_lpc_username.Text.Trim().ToString();
cmd.Parameters.Add(username);
SqlParameter password = new SqlParameter("@PASSWORD", SqlDbType.NVarChar, 50);
password.Value = tbx_lpc_password.Text.Trim().ToString();
cmd.Parameters.Add(password);
con.Open();
int result = (Int32)cmd.ExecuteScalar();
在这里我怀疑的话:
if (result >= 1)
{
string strSelect2 = "SELECT ROLEID FROM LOGIN_DETAILS WHERE EMAIL_ID=@EMAIL_ID";
if (srole == "USER")
{
Session.Add("usnm", tbx_lpc_username.Text);
Response.Redirect("~/ADMIN_PAGES/LPC_ADM_HomePage.aspx");
}
else if (srole == "ADMIN")
{
Session.Add("usnm", tbx_lpc_username.Text);
Response.Redirect("~/ADMIN_PAGES/LPC_ADM_SettingsPage.aspx");
}
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
}
}