Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Can I have the If statement to check and see for three levels? What I am doing is that I have it working for two different levels for login. I wanted to add a third level login. This is what I have so far.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["PassConnectionString"].ConnectionString);
        con.Open();

        if (true)
        {
            SqlCommand level = new SqlCommand("select AccessLevel, Password from TableSecurity where EmailAddress = @EmailAddress AND Password = @Password", con);
            level.Parameters.Add(new SqlParameter("EmailAddress", TextBoxEA.Text));
            level.Parameters.Add(new SqlParameter("Password", TextBoxPW.Text));

            SqlDataReader reader = level.ExecuteReader();
            DataTable dt1 = new DataTable();
            dt1.Load(reader);

            foreach (DataRow dr1 in dt1.Rows)
            {
                int returnedLevel = Convert.ToInt32(dr1[0].ToString());
                if (returnedLevel == 1)
                {
                    Response.Redirect("CEOPage.aspx");
                }

                else if (returnedLevel == 2)
                {
                    Response.Redirect("IALOPage.aspx");
                }
            }
        }
        con.Close();
    }
    
}


How can I add level 3 to the code?
Posted

Further to CHill60's answer:

C#
string[] levelPages = new string[] { "CEOPage.aspx", "IALOPage.aspx", "SomeOtherPage.aspx", };

//...

Response.Redirect(levelPages[returnedLevel - 1]);


—SA
 
Share this answer
 
v2
Comments
Maciej Los 9-Jul-13 17:54pm    
A 5!
Sergey Alexandrovich Kryukov 9-Jul-13 18:23pm    
Thank you, Maciej.
—SA
Sergey Alexandrovich Kryukov 10-Jul-13 12:19pm    
Believe or not, the code could be even more compact and supportable. Please see also Solution 4. :-)
—SA
CHill60 10-Jul-13 7:21am    
I like that! Instantly extendable by amending only one line of code! D'oh. My +5
Sergey Alexandrovich Kryukov 10-Jul-13 10:26am    
Thank you very much.
By the way, I have a whole CodeProject article devoted to getting rid of long switch statements (well, maybe not 100%, but this is how it is formulated in the sub-title: Title: "Dynamic Method Dispatcher", subtitle: "No more long switch statements!") :-)
—SA
Further to jkirkerx' suggestion ...

C#
switch (returnedlevel)
           {
               case 1:
                   Response.Redirect("CEOPage.aspx");
                   break;
               case 2:
                   Response.Redirect("IALOPage.aspx");
                   break;
               case 3:
                   Response.Redirect("SomeOtherPage.aspx");
                   break;
               default:
                   break;
           }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Jul-13 17:44pm    
Nah... still poorly supportable. How about Solution 3? Please see... :-)
—SA
You can add as many elseif levels as you like, and optionally at the end, you could add a catch-all else block:
C#
if (returnedLevel == 1)
                {
                    Response.Redirect("CEOPage.aspx");
                }
                else if (returnedLevel == 2)
                {
                    Response.Redirect("IALOPage.aspx");
                }
                else if (returnedLevel == 3)
                {
                    Response.Redirect("SomeOtherPage.aspx");
                }
                else
                {
                    Response.Redirect("SomeDefaultPage.aspx");
                }

And as jkirkerx suggested in the comments, a switch/Case statement would work as well.
 
Share this answer
 
Comments
CHill60 9-Jul-13 16:11pm    
Sorry - didn't see your solution ... and we chose the same name for the OtherPage !!
woopsydoozy 9-Jul-13 16:34pm    
what do they say about great minds?
Further to Solution 3:

If you can consider renaming the URLs in some more convenient way, the code could be even more compact and supportable:
C#
const string levelUrlFormat = "level{0}.aspx";

//...

Response.Redirect(string.Format(levelUrlFormat, returnedLevel));

Enjoy,
—SA
 
Share this answer
 
v3

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