Click here to Skip to main content
15,886,755 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi all,

i have few assistance in my code below.

i have developed by c# with windows application

my flow is :,

i have set three roles while login details

they are,

1.user

2.admin

3.others

these are all contains my three roles, i actually done this process but what happened my code means,

if i enter correct user name and password means its working fine but,

if i suppossed to type wrong user name and password means i got the given below error message thrice at a time ,



else { MessageBox.Show("Please check your username/Password or else check your Role", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); }




so please check my full code below and find out where i would made mistake in my code.

please drop a solution here..

code is:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace tarriff_design_page
{
    public partial class sample : Form
    {
        SqlConnection con = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        SqlDataReader dr;

        string s = @"Data Source=.\SQLEXPRESS;AttachDbFilename=Database1.mdf;Integrated Security=True;User Instance=True";
      
        public sample()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {



            if (checkBox1.Checked)
            {
                try
                {
                   
                    con = new SqlConnection(s);
                    con.Open();
                    cmd = new SqlCommand("select username,password from login", con);
                    cmd.ExecuteNonQuery();
                    dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        string strname = textBox1.Text;
                        string strpwd = textBox2.Text;

                        if (dr["username"].ToString() == strname && dr["password"].ToString() == strpwd)
                        {
                            Home hpage = new Home();
                            hpage.Text = ("Welcome to ") + textBox1.Text + ("  ") + System.DateTime.Now.ToString();
                            hpage.Show();

                            this.Hide();

                        }

                        else
                        {
                            MessageBox.Show("Please check your username/Password or else check your Role", this.Text,
                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                        }


                        dr.Close();
                        con.Close();
                    }

                }

                catch (Exception ex1)
                {

                }

            }
           

                if (checkBox3.Checked)
                {
                    try
                    {

                        con = new SqlConnection(s);
                        con.Open();
                        cmd = new SqlCommand("select username,password from role3", con);
                        cmd.ExecuteNonQuery();
                        dr = cmd.ExecuteReader();
                        while (dr.Read())
                        {
                            string strname = textBox1.Text;
                            string strpwd = textBox2.Text;

                            if (dr["username"].ToString() == strname && dr["password"].ToString() == strpwd)
                            {

                                searchgv vd = new searchgv();
                                vd.Text = ("Welcome to ") + textBox1.Text + ("  ") + System.DateTime.Now.ToString();

                                //vd.btnprv.Hide();
                                vd.Show();

                                this.Hide();
                            }

                            else
                            {
                                MessageBox.Show("Please check your username/Password or else check your Role", this.Text,
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                            }

                            dr.Close();
                            con.Close();
                        }

                    }

                    catch (Exception ex1)
                    {

                    }

                }

                if (checkBox2.Checked)
                {
                    try
                    {
                        
                        con = new SqlConnection(s);
                        con.Open();
                        cmd = new SqlCommand("select username,password from adminlogin", con);
                        cmd.ExecuteNonQuery();
                        dr = cmd.ExecuteReader();
                        while (dr.Read())
                        {
                            string strname = textBox1.Text;
                            string strpwd = textBox2.Text;

                            if (dr["username"].ToString() == strname && dr["password"].ToString() == strpwd)
                            {
                                Admin ad = new Admin();
                                ad.Text = ("Welcome to ") + textBox2.Text + ("  ") + System.DateTime.Now.ToString();
                                ad.Show();
                                this.Hide();
                            }

                            else
                            {
                                MessageBox.Show("Please check your username/Password or else check your Role", this.Text,
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                            }
                        }
                        con.Close();
                        dr.Close();

                    }
                    catch (Exception ex)
                    {
                        //MessageBox.Show(ex.Message.ToString());
                    }

                }
               

            }

      
            
    }
}


thanks in advance...
Posted
Updated 13-Mar-13 0:40am
v2
Comments
Salman622 14-Sep-13 4:51am    
i'll recommand you to use else if ladder
because when your code of first if block get false it returns else part
then second return false it give else part and so on .....

thats why its displaying error msg thrice instead of one...

because ur checking it 3 times..
use goto statement... exampal
VB
if(i==2)
  goto endThis;

if(g==6)
  i=g;
endThis:

after ur message use goto and set endpart before closing of method
also for goto search on google for "goto in c# windows application"
 
Share this answer
 
v3
i think this will help you....

if (cboRole.Text == "NORMAL USER")
                {
                    try
                    {
                        objCon.Open();
                        SqlCommand objCom = new SqlCommand("SELECT * FROM LOGIN WHERE USERNAME='" + this.txtUserName.Text.Trim() + "' and PASSWORD='" + this.txtPassword.Text.Trim() + "' AND USER_ROLE='" + this.cboRole.Text.Trim() + "'", objCon);
                        SqlDataReader dr = objCom.ExecuteReader();
                        if (dr.HasRows)
                        {
                            MessageBox.Show("Login Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Form1 objForm = new Form1();
                            objForm.Show();
                            this.Hide();
                        }
                        else
                        {
                            MessageBox.Show("User Name or password is incorrect", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            txtUserName.Text = "";
                            txtPassword.Text = "";
                            txtUserName.Focus();
                        }
                    }
                    catch (SqlException er)
                    {
                        MessageBox.Show(er.Message);
                    }
                    finally
                    {
                        objCon.Close();
                    }
                }
                else if (cboRole.Text == "SUPER USER")
                {
                    try
                    {
                        objCon.Open();
                        SqlCommand objCom = new SqlCommand("SELECT * FROM LOGIN WHERE USERNAME='" + this.txtUserName.Text.Trim() + "' and PASSWORD='" + this.txtPassword.Text.Trim() + "' AND USER_ROLE='" + this.cboRole.Text.Trim() + "'", objCon);
                        SqlDataReader dr = objCom.ExecuteReader();
                        if (dr.HasRows)
                        {
                            MessageBox.Show("Login Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Form1 objForm = new Form1();
                            objForm.Show();
                            this.Hide();
                        }
                        else
                        {
                            MessageBox.Show("User Name or password is incorrect", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            txtUserName.Text = "";
                            txtPassword.Text = "";
                            txtUserName.Focus();
                        }
                    }
                    catch (SqlException er)
                    {
                        MessageBox.Show(er.Message);
                    }
                    finally
                    {
                        objCon.Close();
                    }
                }
                
                else if (cboRole.Text == "ADMIN")
                {
                    try
                    {
                        objCon.Open();
                        SqlCommand objCom = new SqlCommand("SELECT * FROM LOGIN WHERE USERNAME='" + this.txtUserName.Text.Trim() + "' AND PASSWORD='" + this.txtPassword.Text.Trim() + "' AND USER_ROLE='" + this.cboRole.Text.Trim() + "'", objCon);
                        SqlDataReader dr = objCom.ExecuteReader();
                        if (dr.HasRows)
                        {
                            MessageBox.Show("Login Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.RetUserName = (string)txtUserName.Text;
                            Form1 objForm = new Form1();
                            objForm.Show();
                            this.Hide();
                        }
                        else
                        {
                            MessageBox.Show("User Name or password is incorrect", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            txtUserName.Text = "";
                            txtPassword.Text = "";
                            txtUserName.Focus();
                        }
                    }
                    catch (SqlException er)
                    {
                        MessageBox.Show(er.Message);
                    }
                    finally
                    {
                        objCon.Close();
                    }
                }
 
Share this answer
 
It happens since all the 3 if conditions are satisfied.

You can check the if else condition or switch case construct with the roles .

Also the code blocks appears to be the same except the role passed as the parameter for the sql query only change. So can be thought of moving the redundant code to a method which takes roles as parameter.
 
Share this answer
 

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