Monday 2 January 2012

create user master form in .net with sql server

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.IO;
using System.Data.SqlClient;


namespace PIS_Billing.Master
{
    public partial class CreateUser : Form
    {
        public CreateUser()
        {
            InitializeComponent();
        }

        DataUtility dut = new DataUtility();
        DataTable dt = new DataTable();
        string Query,ID;

        private void CreateUser_Load(object sender, EventArgs e)
        {          
            Fill_lstDetail();           
        }

        # region Button set 1 to 4
        private void Setbutton1()
        {
            btnSave.Text = "&NEW";
            btnSave.Enabled = true;
            BtnUpdate.Text = "&MODIFY";
            BtnUpdate.Enabled = true;
            BtnDelete.Text = "&REMOVE";
            BtnDelete.Enabled = true;
            BtnClose.Text = "&CLOSE";
            Disable();
            ClearTextbox();
            groupBox1.Enabled = false;

        }
        private void Setbutton2()
        {
            btnSave.Text = "&NEW";
            btnSave.Enabled = false;
            BtnUpdate.Text = "&UPDATE";
            BtnUpdate.Enabled = true;
            BtnDelete.Text = "&REMOVE";
            BtnDelete.Enabled = false;
            BtnClose.Text = "&CANCEL";
            Enable();
            ClearTextbox();
            lstDetail.Focus();
        }
        private void Setbutton3()
        {
            btnSave.Text = "&SAVE";
            btnSave.Enabled = true;
            BtnUpdate.Text = "MODIFY";
            BtnUpdate.Enabled = false;
            BtnDelete.Text = "&REMOVE";
            BtnDelete.Enabled = false;
            BtnClose.Text = "&CANCEL";
            Enable();
            ClearTextbox();
            txtName.Focus();

        }
        private void Setbutton4()
        {
            btnSave.Text = "&SAVE";
            btnSave.Enabled = false;
            BtnUpdate.Text = "MODIFY";
            BtnUpdate.Enabled = false;
            BtnDelete.Text = "&DELETE";
            BtnDelete.Enabled = true;
            BtnClose.Text = "&CANCEL";
            Enable();
            ClearTextbox();
            txtSearch.Focus();
            panel2.Enabled = false;
            groupBox1.Enabled = true;

        }
        private void Enable()
        {
            txtName.Enabled = true;
            txtCPassword.Enabled = true;
            txtPassword.Enabled = true;

        }
        private void Disable()
        {
            txtName.Enabled = false;
            txtCPassword.Enabled = false;
            txtPassword.Enabled = false;

        }
        private void ClearTextbox()
        {
            txtName.Clear();
            txtPassword.Clear();
            txtCPassword.Clear();
        }
        private void Fill_lstDetail()
        {
            try
            {
                if (Static_Varriable.UserName.ToString() == "ADMIN")
                {
                    Query = "Select Distinct Name from CreateUsermst";
                    dut.FillList(Query, lstDetail);
                }
                else
                {
                    Query = " Select Distinct Name from CreateUsermst Where Name = '" + Static_Varriable.UserName.ToString() + "'";
                    dut.FillList(Query, lstDetail);
                }
                Setbutton1();
            }
            catch (Exception)
            {
                //throw;
            }
        }
        private void fillgrid()
        {
           // Query = "Select * from CreateUserMst";
           // dt = dut.GetDataTable(Query);
           // dataGridView1.DataSource = dt;
        }

        #endregion

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

                if (btnSave.Text == "&SAVE")
                {
                    bool Exist;
                    Query = "select count(Name) from CreateUserMst Where Name='" + txtName.Text + "'";
                    Exist = dut.IsExist(Query);
                    if (Exist)
                    {
                        MessageBox.Show("User Login Name Already Exist \n Please choose Another Name", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        if (txtName.Text.Length <= 0)
                        {
                            MessageBox.Show("Please Fill User Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            txtName.Focus();
                        }
                        else if (txtPassword.Text.Length <= 0)
                        {
                            MessageBox.Show("Please Fill User Password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            txtPassword.Focus();
                        }
                        else if (txtCPassword.Text.Length <= 0)
                        {
                            MessageBox.Show("Please Fill User ConfirmPassword", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            txtCPassword.Focus();
                        }
                        else if (txtPassword.Text.Trim() == txtCPassword.Text.Trim())
                        {
                            if (txtPassword.Text == txtCPassword.Text)
                            {

                                Query = "Insert into CreateUserMst(Name,Password,CoPassword,EName) values('" + txtName.Text + "','" + txtPassword.Text + "','" + txtCPassword.Text.Trim() + "','" + "No" + "')";
                                dut.ExecuteSql(Query);                               
                                MessageBox.Show("User Account SuccessFully Created", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                ClearTextbox();
                                Setbutton1();
                                Fill_lstDetail();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Password Is Not Similar Retry", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            txtPassword.Clear();
                            txtCPassword.Clear();
                            txtPassword.Focus();
                        }
                    }
                }
                else
                {
                    Setbutton3();
                    panel2.Enabled = true;
                }

            }
            catch (Exception)
            {               
             //   throw;
            }
        }

        private void BtnUpdate_Click(object sender, EventArgs e)
        {
            try
            {

                if (BtnUpdate.Text == "&UPDATE")
                {
                    if (txtName.Text.Length <= 0)
                    {
                        MessageBox.Show("Please Fill User Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else if (txtPassword.Text.Length <= 0)
                    {
                        MessageBox.Show("Please Fill User Password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else if (txtPassword.Text.Trim() == txtCPassword.Text.Trim())
                    {
                        if (txtPassword.Text == txtCPassword.Text)
                        {
                            Query = "Update createUserMst Set Name='" + txtName.Text + "',Password='" + txtPassword.Text.Trim() + "',CoPassword='" + txtCPassword.Text.Trim() + "' where id='" + ID + "'";
                            dut.ExecuteSql(Query);
                            MessageBox.Show("User Account SuccessFully UPDATE", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Setbutton1();
                            Fill_lstDetail();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Password Is Not Similar Retry", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtPassword.Clear();
                        txtCPassword.Clear();
                        txtPassword.Focus();
                    }
                }
                else
                {
                    Setbutton2();
                    groupBox1.Enabled = true;
                    panel2.Enabled = true;
                }

            }
            catch (Exception)
            {               
               // throw;
            }
        }

        private void BtnDelete_Click(object sender, EventArgs e)
        {
            if (BtnDelete.Text == "&DELETE")
            {
                if (txtName.Text.Length <= 0 && txtPassword.Text.Length <= 0 && txtCPassword.Text.Length <= 0)
                {
                    MessageBox.Show("Please Select Record For Delete");
                }
                else
                {
                    DialogResult DR;
                    DR = MessageBox.Show("Are You Sure Want to Delete this User", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                    if (DR == DialogResult.Yes)
                    {
                        Query = "Delete from CreateUserMst where Id='" + ID + "'";
                        dut.ExecuteSql(Query);
                        MessageBox.Show("Account Deleted", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                Setbutton1();
                Fill_lstDetail();
            }
            else
            {
                Setbutton4();
            }
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            try
            {
                if (BtnClose.Text == "&CLOSE")
                {
                    this.Close();
                }
                else
                {
                    Setbutton1();
                }
            }
            catch (Exception)
            {
               // throw;
            }
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            try
            {
                Query = "Select Name from CreateUserMst where Name like '" + txtSearch.Text + "%'";
                dut.FillList(Query, lstDetail);
            }
            catch (Exception)
            {               
              //  throw;
            }           
        }

        private void lstDetail_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                dut.OpenConnection();
                Query = " select Id,Name,Password,CoPassword from createUserMst Where Name = '" + lstDetail.SelectedItem.ToString() + "' ";
                SqlCommand cmd = new SqlCommand(Query, dut.Con);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    ID = dr["Id"].ToString();
                    txtName.Text = dr["Name"].ToString();
                    txtPassword.Text = dr["Password"].ToString();
                    txtCPassword.Text = dr["CoPassword"].ToString();
                }
                dr.Close();
                dut.CloseConnection();
                dut.DisposeConnection();
            }
            catch (Exception)
            {               
              //  throw;
            }
        }

        #region KeyPress Validation.....

        private void txtName_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if ((e.KeyChar >= 65 && e.KeyChar <= 90) || (e.KeyChar >= 97 && e.KeyChar <= 122) || (e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 32 || e.KeyChar == 8)
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                    MessageBox.Show("Invalied");
                }
            }
            catch (Exception)
            {
               // throw;
            }
        }

        private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if ((e.KeyChar >= 65 && e.KeyChar <= 90) || (e.KeyChar >= 97 && e.KeyChar <= 122) || (e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 32 || e.KeyChar == 8)
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                    MessageBox.Show("Invalied");
                }
            }
            catch (Exception)
            {
             //   throw;
            }
        }

        private void txtCPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if ((e.KeyChar >= 65 && e.KeyChar <= 90) || (e.KeyChar >= 97 && e.KeyChar <= 122) || (e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 32 || e.KeyChar == 8)
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                    MessageBox.Show("Invalied");
                }
            }
            catch (Exception)
            {
                //throw;
            }
        }

        private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if ((e.KeyChar >= 65 && e.KeyChar <= 90) || (e.KeyChar >= 97 && e.KeyChar <= 122) || (e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 32 || e.KeyChar == 8)
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                    MessageBox.Show("Invalied");
                }
            }
            catch (Exception)
            {
             //   throw;
            }
        }
        #endregion

    }
}

No comments:

Post a Comment