Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a page where two different departments will login called 1.Waste and 2.Environment. i want the page to redirect to a specific page if the users department is Waste and also a different page if the users department is Environment. in my register table i have the columns name, surname , email , password and department

if department = Waste
Redirect to page 1 else
if department = Environment
Redirect to page 2

i get an error "An exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll but was not handled in user code"

Not sure how to implement this into the following code:

What I have tried:
Code

What I have tried:

SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand("select * from [dbo].[PB_Register] where pb_Email= @Login_Email and pb_Password=@Login_Password", con);
cmd.Parameters.AddWithValue("@Login_Email",Login_Email.Text);
cmd.Parameters.AddWithValue("@Login_Password", Login_Password.Text);  

Session["Username"] = Login_Email.Text;

con.Open();
SqlDataReader rd = cmd.ExecuteReader();

if (rd.HasRows) {
   
    rd.Read();
       if (rd["Departmet"].ToString() == "EPIP")
                Response.Redirect("UPLOAD.aspx");
            else
                if (rd["Departmet"].ToString() == "Waste")
                Response.Redirect("EMAIL_FILE.aspx");  
            


} else {

    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Username or Password incorrect');", true);

}  
}
Posted
Updated 3-Apr-19 5:20am
Comments
ZurdoDev 3-Apr-19 10:57am    
Step 1 is to understand what your code does. Then, doing what you want to do is very easy.

Department is spelled wrong which is why it can't find it in rd.
Member 14183767 3-Apr-19 11:02am    
its still giving me the same error , this is my login page, i would like for the code to check what department is in PB_Register for each user and redirect to appropriate page
ZurdoDev 3-Apr-19 11:04am    
Again, you need to first learn what the code does.

Calling rd = cmd.ExecuteReader() will execute the sql statement that you have. If you want to access a column in the result then you can do it by using rd["columnName"].ToString().
Member 14183767 3-Apr-19 11:08am    
i understand now what its doing thanks for that! but issue is that even when ive used ur solution i still get an error which is confusing me, plz help i am still learning
ZurdoDev 3-Apr-19 11:09am    
Then you have typed something wrong. What is the name of the column in the PB_Register table that you want to check?

1 solution

The sloppy way would be to grab this within a WHILE loop in some variation of this
C#
if (rd.HasRows) {
  while (rs.Read()) {
    string RedirectURL = rd["ColumnName"];
  }
}
Response.Redirect(RedirectURL);


I would probably redo this to be something more like this; you only need one value from one row so ExecuteScalar would be much more efficient. And what happens if they are in neither group? I threw in an Exception for that.
You may need to fix some typos as I do not have an IDE checking my typing
C#
SqlConnection con = new SqlConnection(strConnString);

// Just grab the column you need
SqlCommand cmd = new SqlCommand("SELECT Departmet FROM [dbo].[PB_Register] WHERE pb_Email= @Login_Email AND pb_Password=@Login_Password", con);

cmd.Parameters.AddWithValue("@Login_Email",Login_Email.Text);
cmd.Parameters.AddWithValue("@Login_Password", Login_Password.Text);  

Session["Username"] = Login_Email.Text;

con.Open();

// SqlDataReader rd = cmd.ExecuteReader();
var DepartmentName = cmd.ExecuteScalar();

if (DepartmentName != null) {
  switch(DepartmentName.ToSting()) {
    case "EPIP" : Response.Redirect("UPLOAD.aspx"); break
    case "Waste" : Response.Redirect("EMAIL_FILE.aspx"); break
    default: throw new ArgumentOutOfRangeException("Department", "The department does not have an assigned path"); break
  } 
} else {
  ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Username or Password incorrect');", true);

}
 
Share this answer
 
Comments
Member 14183767 3-Apr-19 11:38am    
thank you MadMyche it works!!!!
MadMyche 3-Apr-19 11:39am    
You're welcome. Please mark this as the solution and consider rating it as well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900