Pergunta

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.

Foi útil?

Solução

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
       }

Outras dicas

So whats the problem in it? why don't you put User_Type in your database as a column in the table you are storing the user information and then retrieve its value and use it in switch condition to redirect the user to correct page.

For example

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

that is because @janki's is not using WHERE clause in the sql statement. use something like select User_Type from TableName where username=@user instead of select User_Type from TableName

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top