Click here to Skip to main content
15,914,375 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello. I am trying to find out how to code one user over the other. What I mean is I am CEO user and I have a IALO user. When the CEO logs in, I should be directed to Welcome CEO page. When the IALO logs in, there should be a Welcome IALO page. How would I do this in ASP.net? I have two tables. One table has the CEO users and the other has IALO users. Here is the code I have:
C#
using System;
using System.Data;
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;

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 chkUser = "select count(*) from TableSecurity where EmailAddress = 'TextBox1.Text' and Password = 'TextBox2.Text'";
            SqlCommand chkUsercmd = new SqlCommand(chkUser, con);
            int i = Convert.ToInt16(chkUsercmd.ExecuteScalar().ToString());
        if(i<1)
        {


        string insCmd = "Insert into TableSecurity (EmailAddress, Password, Level) values (@EmailAddress, @Password, @Level)";
        SqlCommand insertUser = new SqlCommand(insCmd, con);
        insertUser.Parameters.AddWithValue("@EmailAddress", TextBox1.Text);
        insertUser.Parameters.AddWithValue("@Password", TextBox2.Text);
        insertUser.Parameters.AddWithValue("@Level", TextBox1.Text);

    }
    else
{
    Session["New"] = TextBox1.Text;
    Response.Redirect("Secure.aspx");
}

        


        con.Close();
        con.Open();
       string cmdStr = "select count(*) from TableCEO where EmailAddress='" + TextBox1.Text + "'";
       cmdStr = "select count(*) from TableIALO where EmailAddress='" + TextBox1.Text + "'";
            SqlCommand Checkuser = new SqlCommand(cmdStr, con);
            int temp = Convert.ToInt32(Checkuser.ExecuteScalar().ToString());
            if (temp == 1)
            {
                string cmdStr2 = "Select Password from TableSecurity where Password='" + TextBox2.Text + "'";
                SqlCommand pass = new SqlCommand(cmdStr2, con);
                string password = pass.ExecuteScalar().ToString();
                con.Close();

                if (password == TextBox2.Text)
                {
                    Session["New"] = TextBox1.Text;
                    Response.Redirect("Secure.aspx");
                }
                else
                {
                    Label1.Visible = true;
                    Label1.Text = "Invalid Password!!!";
                }
            }
            else
            {
                Label1.Visible = true;
                Label1.Text = "Invalid UserName!!!";
Posted
Updated 29-Apr-13 9:00am
v2
Comments
ZurdoDev 29-Apr-13 15:01pm    
if (something) Response.Redirect("ceo.aspx") else Response.Redirect("someotherpage.aspx") This is very easy. Where are you stuck?
Computer Wiz99 29-Apr-13 15:03pm    
I guess I am stuck on where to put the code. I wrote the code but had to make updates to it.
ZurdoDev 29-Apr-13 15:04pm    
Where ever you are redirecting now, just add it there.
Computer Wiz99 29-Apr-13 15:05pm    
Can you give me a clear spot?
ZurdoDev 29-Apr-13 15:12pm    
Again, it depends on you and what you are doing. You already have Response.Redirect("Secure.aspx"); so you could do it there. Of course, I have no idea what Secure.aspx is so maybe you would need to do it after.

1 solution

C#
SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString;
conn.Open();
string uname = TextBox1.Text;
string pswd = Textbox2.Text;
SqlCommand cmd = new SqlCommand("Select uname, pswd from emp_details where uname =@uname and pswd =@pswd", conn);
cmd.Parameters.Add(new SqlParameter("@uname",uname));
cmd.Parameters.Add(new SqlParameter("@pswd",pswd));
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())/*if user exists*/
{                
   string str = dr.GetString(0).ToString();
   dr.Close();
   /*get role of the username provided*/
   SqlCommand cmd2 = new SqlCommand("select role from users where uname=@str",conn);
   cmd.Parameters.Add(new SqlParameter("@str",str));
   SqlDataReader dr2 = cmd2.ExecuteReader();
   if (dr2.Read())
   {
     string role1= dr2.GetString(0).ToString();
     dr2.Close();
     /*redirect user based on the value of the role*/
     if (role1== "user")
         Response.Redirect("user.aspx");
     if (role1== "CEO")
         Response.Redirect("ceo.aspx");
   }
}
else/*user doesnot exist in your database*/
{
  /*Alert her and redirect her to signup page*/
  Response.Write("<script type='text/javascript'>");
  Response.Write("alert('Username does not exists.');");
  Response.Write("document.location.href='SignUp.aspx';");
  Response.Write("</script>");
}
 
Share this answer
 
v3
Comments
Thanks7872 30-Apr-13 7:39am    
you can use this in your function.what else functionality you need further?
Computer Wiz99 30-Apr-13 7:44am    
The whole thing is that I have Three Tables. One is a CEO Table that has CEO records already in it. The other Table is the IALO Table that has IALO records already in it. The Last Table is the Security Table. It will store the username, password and Level. The other tables are there to be read by the program to see if the user has a record and allow the user to make up a username and password that will be stored in the Security Table.
Thanks7872 30-Apr-13 7:50am    
i am lil bit confuse with what you have done.well as per as i understand this,you first want to login to the program and see who the user is?based on that you want to redirect the user.i didnt get The other tables are there to be read by the program to see if the user has a record and allow the user to make up a username and password that will be stored in the Security Table. Does it mean something like sign up?
Computer Wiz99 30-Apr-13 7:55am    
Yes, that is correct. Within the CEO and IALO Tables they have the same thing, Email Address. That is the username. The program is reading to see if the record exist and if it does the user can make up his/her password that will be store in the security table with the email address (username) and the level. That user can login with what was made by them. If someone else comes and tries to login it will not happen because the user exist in the security table.
Thanks7872 30-Apr-13 8:00am    
Have a look at the above answer.I have updated the code for signup.

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