Search This Blog

Windows Applications

This is the login  code for a database application.This code is designed with access rights.Not really through SQL but through the interface.For example if you want to have a main menu form after the login form and this form contains certain features which are not supposed to be available to all users then this is the ideal code for you.
In case you will be wondering what are the databse fields required i will just mention it here
Table name:Login_File
Fields:UserName(varchar),Password(varchar) and MemberType(varchar)


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; // depends on your databse type. This is for Microsoft Sql Server
namespace GOGREEN
{
    public partial class Login : Form
    {
        public string MemberType = null;
        public string pword = null;
        public string uname = null;
        public Login()
        {
            InitializeComponent();
        }
       

private void button1_Click(object sender, EventArgs e)
        {
           
            SqlConnection con; //connection object
            SqlCommand com; //command object
            string sql = null;

            if (textBox1.Text == "")
            {
                MessageBox.Show("Enter  the username");
                textBox1.Focus();
                return;
            }
            if (textBox2.Text == "")
            {
                MessageBox.Show(textBox1, "Enter  the password");
                textBox2.Focus();
                return;
            }


             con = new SqlConnection(Properties.Settings.Default.ConnectionString); //this retrieves your connection string
            con.Open(); // opens the connection
            progressBar1.Value = 10; // i have added a progress bar but it is not necessary
            sql = ("select * from Login_File where UserName=@name and Password=@pw"); //sql command

             com = new SqlCommand(sql, con);
            com.Parameters.AddWithValue("@name", textBox1.Text);
            com.Parameters.AddWithValue("@pw", textBox2.Text);
            DataTable dt = new DataTable();
            progressBar1.Value = 30;
            SqlDataAdapter da = new SqlDataAdapter(com);//adapter object

            da.Fill(dt);

            SqlDataReader sqlReader = com.ExecuteReader();//reader object
            progressBar1.Value = 50;
            while (sqlReader.Read()) //checking for the correct username and password
            {
                uname = sqlReader.GetValue(0).ToString();//checks for the username
                pword = sqlReader.GetValue(1).ToString();//checks for the password

                MemberType = sqlReader.GetValue(2).ToString(); //checks for the member type
            }
           if (dt.Rows.Count > 0)
                {
                    progressBar1.Value = 100;
                    Main_Menu mm = new Main_Menu(MemberType); // passes the member type to the main menu form so that it will be displayed appropriately
                    mm.Show();
                   
           }
             else //if the credentials were invalid
                {
                    progressBar1.Value = 0;

                    MessageBox.Show("Incorrect Username or Password");
                   
                }

No comments:

Post a Comment