-1

I made a web application and I want to Login in admincp for adminstrator and modcp for modrator manager. this is code for login page :

protected void btnOk_Click(object sender, EventArgs e)
{
    try
    {
        string user_n = txtUser.Text;
        string pass_n = txtPass.Text;
        Int32 res = 0;


        DAL.DAL log = new DAL.DAL();
        res = log.login_user_form(user_n, pass_n);




        if (res == 1)
        { 

           if (user_n == "admin")
              {
                  Response.Redirect("manage/AdminPage.aspx");
              }
       else 
         {
            Response.Redirect("modcp/defult3.aspx");
        }


        }
        else
        {
            lblRes.Text = "error username or password";
        }
    }
    catch (Exception ex)
    {
        lblRes.Text = ex.ToString();
    }


}

and in DAL layer this code :

public Int32 login_user_form(String username, String password)
{
    int res = 0;
    con = new SqlConnection(strcon);
    SqlCommand cmd = new SqlCommand("EXEC chek_user '" + username + "' , '" + password + "'", con);

    try
    {
        con.Open();
        if (cmd.ExecuteScalar() == null)
        {
            res = 0;
        }

        else
        {
            res = 1;
        }

        con.Close();

    }
    catch (SqlException)
    {
        throw;
    }

    return res;
}

and this is my stored procedures :

@username nvarchar(50) , @password nvarchar(50)
AS
BEGIN

SET NOCOUNT ON;
    SELECT username_l1 , password_l1 
    FROM user_L1
    WHERE username_l1 = @username AND password_l1 = @password
END

this cod in page login.aspx in work without this line:

Response.Redirect("manage/AdminPage.aspx");

if I clear this code and write :

txtPass.Visible = false;
lblRes.Text = "mesesege";

That is work corect but without Response.Redirect, it doesn't?

(I dont know how work whit session for level access)

1 Answers1

0

You should move the Response.Redirect() out of the try/catch since it actually throws an exception.

Even if the code was logically correct, the Response.Redirects would be swallowed up by the catch statement.

Edit:

This is explained in this question.

Community
  • 1
  • 1
Digbyswift
  • 10,310
  • 4
  • 38
  • 66
  • TanX ,I move code out of try/catch this is code : protected void btnOk_Click(object sender, EventArgs e) { string user_n = txtUser.Text; string pass_n = txtPass.Text; Int32 res = 0; DAL.DAL log = new DAL.DAL(); res = log.login_user_form(user_n, pass_n); if (res == 1) { if (user_n == "admin") { Response.Redirect("manage/AdminPage.aspx");}else{ Response.Redirect("modcp/defult3.aspx");}}} But yet dosent work :( – Strawberry Jan 09 '13 at 17:10