Login Prog, in C#

Admin and User login

Page 1 of 1

8 Replies - 8002 Views - Last Post: 12 March 2010 - 06:36 AM Rate Topic: -----

#1 jokertin92  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 26-March 09

Login Prog, in C#

Posted 10 March 2010 - 03:06 AM

Hey frens!
Can you please suggest me the way to create a login page for an user and admin..

My req. is like.... If i login as admin, i should get some additional buttons. If i am an user i should not get those button.

Tell me the procedures pls. Do i need to create different forms for user and admin or...
Is This A Good Question/Topic? -1
  • +

Replies To: Login Prog, in C#

#2 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Login Prog, in C#

Posted 10 March 2010 - 03:11 AM

First of all, you need to show some effort first. This forum has policy of not doing your homework.

Do i need to create different forms for user and admin?
No. You can use .Show() and .Hide methods to hide or show those button/controls...
Was This Post Helpful? 0
  • +
  • -

#3 b.ihde  Icon User is offline

  • D.I.C for life
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

Re: Login Prog, in C#

Posted 10 March 2010 - 04:15 AM

Just to get an idea how to start..

create a login form and create a class/method to check if the user is a admin or user...
like, if username == admin && password == pwassword change a bool like isAdmin to true, else false...

Then in the form u can show the controls by checking if the bool isAdmin is true or not.

I would set the admincontrols to visibe.false per default, so the controls are only visible if isAdmin is true..

Hope you ll find your way, if you have any problems, let us know ;)


greetz

Ben
Was This Post Helpful? 0
  • +
  • -

#4 jokertin92  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 26-March 09

Re: Login Prog, in C#

Posted 10 March 2010 - 11:14 PM

View PostFlashM, on 10 March 2010 - 02:11 AM, said:

First of all, you need to show some effort first. This forum has policy of not doing your homework.

Do i need to create different forms for user and admin?
No. You can use .Show() and .Hide methods to hide or show those button/controls...

Hi fren,
I've already started but I couldn't move further.

View Postb.ihde, on 10 March 2010 - 03:15 AM, said:

Just to get an idea how to start..

create a login form and create a class/method to check if the user is a admin or user...
like, if username == admin && password == pwassword change a bool like isAdmin to true, else false...

Then in the form u can show the controls by checking if the bool isAdmin is true or not.

I would set the admincontrols to visibe.false per default, so the controls are only visible if isAdmin is true..

Hope you ll find your way, if you have any problems, let us know ;)


greetz

Ben

Thanks for your reply Boss..

I've created a simple login page. How to move further guide me..

private void btnLogin_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data source=.\\SQLEXPRESS;Initial Catalog=Video Rental;Integrated Security=True");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "SELECT * FROM Access WHERE Username='" + txtusername.Text + "'AND Password='" + txtpassword.Text + "'";
            SqlDataReader reader = null;
            con.Open();
            reader = cmd.ExecuteReader();
            if (reader.HasRows)
                {
                    reader.Read();
                    MessageBox.Show("Successfully Logged in");
                    con.Close();
                    this.Close();
                    frm2.ShowDialog();

                }
                else
                {
                    MessageBox.Show("Username or Password is Wrong");
                    txtusername.Clear();
                    txtpassword.Clear();
                    txtusername.Focus();
                }
                
        }

This post has been edited by JackOfAllTrades: 11 March 2010 - 06:39 AM
Reason for edit:: Added code tags.

Was This Post Helpful? 0
  • +
  • -

#5 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Login Prog, in C#

Posted 11 March 2010 - 01:29 AM

Here some sample code. I think it is self explanatory, but if you need any info regarding this solution, feel free to ask.

using System;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace LoginApplication
{
    public partial class Form1 : Form
    {
        //Setting the connection string to readonly, user is not able to alter it once the application starts
        private readonly string conn_str = "Data source=.\\SQLEXPRESS;Initial Catalog=Video Rental;Integrated Security=True";

        public Form1()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //I didn't use * in select statement just to show you what table fields I used
            string query = "SELECT Id, Username, Password, PriviledgeLvl FROM Access WHERE Username = @Username";

            //If you use keyword 'using', those objects inside using statement will
            //be automatically disposed
            using (SqlConnection conn = new SqlConnection(conn_str))
            {
                using (SqlCommand cmd = new SqlCommand(query, conn))
                {
                    //add a parameter to sql query
                    cmd.Parameters.AddWithValue("Username", txtUsername.Text.Trim());

                    conn.Open();
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            //read the first row that reader returned and save password from DB into variable
                            reader.Read();
                            string password = reader["Password"].ToString();
                            string priviledgeLevel = reader["PriviledgeLvl"].ToString();

                            if (reader.Read())
                            {
                                //If reader is able to read the second row, that means we have retrieved
                                //two records, with exactly the same username and this should not happen!!!
                                return;
                            }
                            else
                            {
                                if (txtPassword.Text.Trim() == password)
                                {
                                    //If password entered in txtPassword matches the one 
                                    //retrieved from DB then login is successful.

                                    //Create new form2 and pass it the 'priviledgeLevel' parameter
                                    Form2 frm2 = new Form2(priviledgeLevel);
                                    frm2.ShowDialog();
                                }
                                else
                                {
                                    //Wrong password!
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}




Form2 code:
using System.Windows.Forms;

namespace LoginApplication
{
    public partial class Form2 : Form
    {
        //Notice that I have added the input parameter in the constructor for Form2
        public Form2(string priviledgeLevel)
        {
            InitializeComponent();
            InitControls(priviledgeLevel);
        }

        private void InitControls(string priviledgeLevel)
        {
            if (priviledgeLevel == "ADMIN")
            {
                //buttons visible to admin only
                btnFirst.Visible = true;
                btnSecond.Visible = true;
                btnThird.Visible = true;
            }
            else
            {
                //buttons not visible to common user
                btnFirst.Visible = false;
                btnSecond.Visible = false;
                btnThird.Visible = false;
            }

            //These two buttons are visible to both admin and common user
            btnFourth.Visible = true;
            btnFifth.Visible = true;
        }
    }
}




Hope this helps...

This post has been edited by FlashM: 11 March 2010 - 01:55 AM

Was This Post Helpful? 0
  • +
  • -

#6 Sn0wBum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 178
  • Joined: 04-February 10

Re: Login Prog, in C#

Posted 11 March 2010 - 07:02 AM

FlashM's way is the correct way to accomplish your question. There is another way (just a different methodology) of testing the SQL return using the username and password query.
//Instead of this
if (reader.HasRows) 
{ 
//logic for correct credentials
return; 
} 
//you could also check like this
if(reader.Read()==true)
{
//logic for correct credentials
return;
}



the read method will return as 1 (true) if the DataReader returns a DataRow with the given credentials.
Also, throwing a LIMIT 1 at the end of the SQL statement would be beneficial if there is a freak occurrence of same username and password :)
Was This Post Helpful? 0
  • +
  • -

#7 jokertin92  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 26-March 09

Re: Login Prog, in C#

Posted 11 March 2010 - 10:05 PM

View PostFlashM, on 11 March 2010 - 12:29 AM, said:

Here some sample code. I think it is self explanatory, but if you need any info regarding this solution, feel free to ask.

using System;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace LoginApplication
{
    public partial class Form1 : Form
    {
        //Setting the connection string to readonly, user is not able to alter it once the application starts
        private readonly string conn_str = "Data source=.\\SQLEXPRESS;Initial Catalog=Video Rental;Integrated Security=True";

        public Form1()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //I didn't use * in select statement just to show you what table fields I used
            string query = "SELECT Id, Username, Password, PriviledgeLvl FROM Access WHERE Username = @Username";

            //If you use keyword 'using', those objects inside using statement will
            //be automatically disposed
            using (SqlConnection conn = new SqlConnection(conn_str))
            {
                using (SqlCommand cmd = new SqlCommand(query, conn))
                {
                    //add a parameter to sql query
                    cmd.Parameters.AddWithValue("Username", txtUsername.Text.Trim());

                    conn.Open();
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            //read the first row that reader returned and save password from DB into variable
                            reader.Read();
                            string password = reader["Password"].ToString();
                            string priviledgeLevel = reader["PriviledgeLvl"].ToString();

                            if (reader.Read())
                            {
                                //If reader is able to read the second row, that means we have retrieved
                                //two records, with exactly the same username and this should not happen!!!
                                return;
                            }
                            else
                            {
                                if (txtPassword.Text.Trim() == password)
                                {
                                    //If password entered in txtPassword matches the one 
                                    //retrieved from DB then login is successful.

                                    //Create new form2 and pass it the 'priviledgeLevel' parameter
                                    Form2 frm2 = new Form2(priviledgeLevel);
                                    frm2.ShowDialog();
                                }
                                else
                                {
                                    //Wrong password!
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}




Form2 code:
using System.Windows.Forms;

namespace LoginApplication
{
    public partial class Form2 : Form
    {
        //Notice that I have added the input parameter in the constructor for Form2
        public Form2(string priviledgeLevel)
        {
            InitializeComponent();
            InitControls(priviledgeLevel);
        }

        private void InitControls(string priviledgeLevel)
        {
            if (priviledgeLevel == "ADMIN")
            {
                //buttons visible to admin only
                btnFirst.Visible = true;
                btnSecond.Visible = true;
                btnThird.Visible = true;
            }
            else
            {
                //buttons not visible to common user
                btnFirst.Visible = false;
                btnSecond.Visible = false;
                btnThird.Visible = false;
            }

            //These two buttons are visible to both admin and common user
            btnFourth.Visible = true;
            btnFifth.Visible = true;
        }
    }
}




Hope this helps...

Thank you fren.. It gives some light.. thank you..

View PostSn0wBum, on 11 March 2010 - 06:02 AM, said:

FlashM's way is the correct way to accomplish your question. There is another way (just a different methodology) of testing the SQL return using the username and password query.
//Instead of this
if (reader.HasRows) 
{ 
//logic for correct credentials
return; 
} 
//you could also check like this
if(reader.Read()==true)
{
//logic for correct credentials
return;
}



the read method will return as 1 (true) if the DataReader returns a DataRow with the given credentials.
Also, throwing a LIMIT 1 at the end of the SQL statement would be beneficial if there is a freak occurrence of same username and password :)

Ya.. I could understand than you dude..
Was This Post Helpful? 0
  • +
  • -

#8 jokertin92  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 26-March 09

Re: Login Prog, in C#

Posted 12 March 2010 - 06:34 AM

Hey I am sorry Spell mistake that is (Ya.. I could understand "thank" you dude..)
Was This Post Helpful? 0
  • +
  • -

#9 Sn0wBum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 178
  • Joined: 04-February 10

Re: Login Prog, in C#

Posted 12 March 2010 - 06:36 AM

All good.. and no problem :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1