Search code examples
asp.net.netloginview

Redirect Admin to different page and User to different Page


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>");
        }

Hi I am new to .NET Above code is for LOGIN page. I want to redirect Admin and user to different pages respectively(dashboard.aspx and user-dashboard.aspx). User_Type=1 means Admin and if it is 2 then it means USER.


Solution

  • U can use this simple way

      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
           }