Hi I am currently new in C#, I have finished exercising and studying how to code for console application. Then I plan to do a simple application. I started with a user log in. Where in the form will validate the username and password inputted by the user in an access database and if successful it will show MAIN screen.

Below is my simple code, please help me. I am really stuck with this one once I new how to manipulate the records it will be easier for me. I don't know what to code in my cmdLogin. I cannot access there the datareader. Please help me I really want to learn.

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.OleDb;

namespace Perfume_Biz
{
    public partial class Login : Form
    {

        public Login()
        {
            InitializeComponent();
            string strconn = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=C:\\Perfume.mdb";
            string strcmd = "select User_Name,Password from Users";
            OleDbConnection OleConn = new OleDbConnection(strconn);
            OleDbCommand OleCmd = new OleDbCommand(strcmd, OleConn);
            OleConn.Open();
            OleDbDataReader OleRead = OleCmd.ExecuteReader();
        }

        private void cmdcancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void cmdclear_Click(object sender, EventArgs e)
        {
            txtpassword.Text = "";
            txtusername.Text = "";
        }

        private void Login_Load(object sender, EventArgs e)
        {

        }

        private void cmdlogin_Click(object sender, EventArgs e)
        {

        }



    }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

If you want to use variables in more than one method, you should make them member variables instead of local variables.

Try to read the table row by row and compare user name and password for each read.Please look at the code sample below...

private void button1_Click(object sender, EventArgs e)
        {
            
            OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data" +  "Source=myDB.mdb");
            OleDbCommand com = new OleDbCommand("SELECT User_Name,Password FROM Table1",con);
            OleDbDataReader reader;
            bool permit = false;

            try
            {
                con.Open();
                reader = com.ExecuteReader();

                while (reader.Read())
                {
                    if (InputName == (reader["User_Name"].ToString()) && InputPass == reader["Password"].ToString())
                    {
                        permit = true;
                        break;
                    }    
                }
                con.Close();
            }

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

            if (permit == true)
                MessageBox.Show("Access granted");
            else
                MessageBox.Show("Acces denied");

You can also use a query which retrieves the user with the password in a try catch block.If no user found , catch the exception and tell it user.Here is a sample code below:

OleDbDataReader read;
                
                try
                {
                    cmd = new OleDbCommand("SELECT User_Name FROM Table1 WHERE Password =" + Convert.ToInt32(textBox1.Text),con);

                    if(con.State == ConnectionState.Closed)
                        con.Open();

                    read = cmd.ExecuteReader();
                    read.Read();//You should read only once because you can //retrieve only one record(i.e, User_Name is unique)

                    textBox2.Text = read["User_Name"].ToString();
                   

                    read.Close();
                    con.Close();
                   

                }

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

Hi,

thanks to both of you...now I can move one studying further C#. Thanks a lot.

:)

This article has been dead for over six months. Start a new discussion instead.