Click here to Skip to main content
15,886,778 members

set roles in login details using c# windows application

stellus asked:

Open original thread
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...
Tags: C#, Windows, Application

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900