Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows Application
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:
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 13-Mar-13 1:34am
Edited 13-Mar-13 1:40am
v2
Comments
Salman622 at 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...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

because ur checking it 3 times..
use goto statement... exampal
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"
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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();
                    }
                }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 Andreas Gieriet 100
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,260


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 14 Sep 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100