【问题标题】:Redirect Admin to different page and User to different Page将管理员重定向到不同的页面,将用户重定向到不同的页面
【发布时间】:2013-07-30 06:23:41
【问题描述】:
SqlConnection con= new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from login where Email =@username and Password=@password and Activated_User=1 and User_Type=1", con);
        cmd.Parameters.AddWithValue("@username", Login1.UserName);
        cmd.Parameters.AddWithValue("@password", Login1.Password);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable ds = new DataTable();
        da.Fill(ds);

        if (ds.Rows.Count > 0)
        {            
                Session["UID"] = Login1.UserName;
                Response.Redirect("dashboard.aspx");

        }
        else
        {
            ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
        }

您好,我是 .NET 新手 以上代码用于登录页面。 我想将管理员和用户分别重定向到不同的页面(dashboard.aspx 和 user-dashboard.aspx)。 User_Type=1 表示 Admin,如果为 2 则表示 USER。

【问题讨论】:

    标签: asp.net .net loginview


    【解决方案1】:

    你可以用这个简单的方法

      SqlConnection con = new SqlConnection("Connection string");
            con.Open();
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand("select User_Type from TableName", con);
            SqlDataAdapter da = new SqlDataAdapter();
    
            cmd.CommandType = CommandType.Text;
            da.SelectCommand = cmd;
            da.Fill(ds);
    
            if(ds.Tables[0].Rows.Count > 0)
            {
                int usertype = Convert.ToInt32(ds.Tables[0].Rows[0]["User_Type"]);
    
                if(usertype==1)
                {
                   Response.Redirect("dashboard.aspx");
                }
              else if(usertype==2)
               {
                   Response.Redirect("user_dashboard.aspx");
               }
           }
           else
           {
             //record is not in ur table
           }
    

    【讨论】:

    • @Janki 这是 givig 错误“找不到表 0”。在线 int usertype = Convert.ToInt32(ds.Tables[0].Rows[0]["User_Type"]);
    • @Janki 该错误已解决。但是现在每次登录时它都会将我重定向到dashboard.aspx 它应该将管理员重定向到dashboard.aspx,将用户重定向到user-dashboard.aspx
    • 我已经尝试过了,但变量用户类型的值只有 =1 而不是 2。
    • 通过此查询“Select * from TableName Where User_Type=2”检查您的数据库,如果有记录,则应在此处显示。如果没有,则在您的数据库中输入 User_Type 为 2 的记录。
    • 我已经在数据库中运行了该查询,它返回了我想要的值。但在上面的代码中,它没有重定向到 User-dashboard.aspx 页面。它仅重定向到 dashboard.aspx 页面。
    【解决方案2】:

    那么它有什么问题呢?为什么不将 User_Type 作为存储用户信息的表中的列放入数据库中,然后检索其值并在切换条件下使用它来将用户重定向到正确的页面。

    例如

    try{
                    con.Open();
                    cmd = new SqlCommand("select User_Type from TableName where username=@user", con);
                    cmd.Parameters.Add("@user", SqlDbType.Int).Value = "PersonName";
                    dr = cmd.ExecuteReader();
                    if (dr.HasRows == false)
                    {
                        throw new Exception();   
                    }
                    if (dr.Read())
                    {
                        int Value = Convert.ToInt32(dr[0].ToString());
                    }
                   switch(Value)
                  {
                   case 1:
                   Response.Redirect("dashboard.aspx");
                   break;
                   case 2:
                   Response.Redirect("user_dashboard.aspx");   
                   break;
                    }
                  }
                catch
                {
                    Result.Text = "THE GIVEN ID IS UNAVAILABLE";
                }
                finally
                {
                    con.Close();
                }
    

    【讨论】:

    • 感谢您的快速回复。我如何在上面的代码中使用 Switch? User_Type 是 db 中的列。
    • 它在 "int Value = Convert.ToInt32(row["User_Type"]);" 行给出错误 "Object cannot be cast from DBNull to other types"
    • 对不起,兄弟,我现在没有安装 Visual Studio,但无论如何尝试这个我更新的答案......我希望它应该工作。
    【解决方案3】:

    那是因为@janki's 没有在 sql 语句中使用 WHERE 子句。 使用 select User_Type from TableName where username=@user 之类的东西而不是 select User_Type from TableName

    【讨论】:

      猜你喜欢
      • 2011-12-25
      • 2019-04-05
      • 1970-01-01
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-29
      • 1970-01-01
      相关资源
      最近更新 更多