Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. I am trying to find out how to write the code to finish my levels. What is going on is that on the Login page I have to program to get the username and password from a table in order to have the user to login. That part works. Now I want to login depending on the user levels. If the user is a Level1 then the page redirects to a Welcome page for that user. If the user is a Level2 then the user goes to that page. Here is the code I have:

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

        string cmdStr = "select count(*) from TableSecurity where EmailAddress= '" + TextBoxEA.Text + "'";
        SqlCommand Checkuser = new SqlCommand(cmdStr, con);
        int temp = Convert.ToInt32(Checkuser.ExecuteScalar().ToString());
        if (temp == 1)
        {
            string cmdStr3 = "select Level from TableSecurity where EmailAddress= '" + TextBoxEA.Text + "'";

            SqlCommand level = new SqlCommand(cmdStr3, con);

            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("~/Secure.aspx");
                }

                else if (returnedLevel == 2)
                {
                    Response.Redirect("~/WelcomeIALO.aspx");
                }
            }
        }
        con.Close();
    }
}
Posted
Updated 1-May-13 10:05am
v10
Comments
Richard C Bishop 1-May-13 11:32am    
What is the problem? Also, please start using <pre></pre> tags around your code in order to make it readable.
Computer Wiz99 1-May-13 12:02pm    
Ok I will start using those tags. I the problem is the last part where the if else statement is, based on the level within the Security Table I wanted to redirect that user to his welcome page. And the same with the other user.
Richard C Bishop 1-May-13 12:09pm    
It looks like you are on the right track. A problem I do see is that you are not loading the data from your select statement into a datatable. Add this:

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

Then get the row with the "level" in it and check that before redirecting.
Computer Wiz99 1-May-13 12:13pm    
Ok Thanks. Do I need to put the reader before the if statement or before the string cmdStr2?
Richard C Bishop 1-May-13 12:15pm    
After both but before closing the connection.

Perhaps you first need to study the subject using this tutorial: http://www.asp.net/web-forms/tutorials/security/roles/role-based-authorization-cs[^].

—SA
 
Share this answer
 
Comments
Computer Wiz99 1-May-13 13:55pm    
Ok. That is good info, ---SA, but I am doing it kind of different.
Sergey Alexandrovich Kryukov 1-May-13 13:58pm    
I can understand that. Thought it could be useful anyway, at least to get the ideas...
—SA
Computer Wiz99 1-May-13 14:36pm    
Yeah, you right. Thanks again.
Here is the code you need to add/alter in order for it to work:
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();
 
        string cmdStr = "select count(*) from TableSecurity where EmailAddress= '" + TextBoxEA.Text + "'";
        SqlCommand Checkuser = new SqlCommand(cmdStr, con);
        int temp = Convert.ToInt32(Checkuser.ExecuteScalar().ToString());
        if (temp == 1)
        { 
            con.Close();
            string cmdStr3 = "select Level from TableSecurity where EmailAddress= '" +        TextBoxEA.Text + "'";
            con.Open();
            SqlCommand level = new SqlCommand(cmdStr3, con);
             
            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("~/Secure.aspx");
                }
            
                else if(returnedLevel == 2) 
                {                
                Response.Redirect("~/WelcomeIALO.aspx");
                }
            }
        }
       con.Close();
     }
 
Share this answer
 
v10
Comments
Computer Wiz99 1-May-13 16:29pm    
INST_ID numeric(18, 0) Unchecked
EmailAddress varchar(50) Unchecked
Password varchar(50) Unchecked
Level numeric(18, 0) Checked

1 admin@mail.com 12345678 NULL
2 ahowell@abac.edu 12345678 NULL
3 kwesihopkins@mail.com password NULL
4 mark@mail.com 123pass NULL
10880 milholland@acu.edu iloveme 1
10880 schubert@acu.edu p1a2s3s4 2
NULL NULL NULL NULL
Richard C Bishop 1-May-13 16:33pm    
Ok, so you are only going to get redirected if 1 of the last 2 emails is entered. If you enter anything other than milholland@acu.edu or schubert@acu.edu it will not work.
Computer Wiz99 1-May-13 18:28pm    
Sorry for the long wait. I can't logon as anyone. What I have done was I deleted all of the records in the Security Table. That is the table that has the username, password, and level. I will try again.
Computer Wiz99 1-May-13 18:44pm    
I debuged it and the same thing happened. I just have the milholland@acu.edu and schubert@acu.edu records.
Computer Wiz99 2-May-13 8:03am    
I had an update with my records in the database. I deleted the ones without the levels and tried to login with the ones that does have levels. I got the same resuit.

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