Tuesday, May 21, 2013

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;

namespace Employee_Management
{
    public partial class Form1 : Form
    {

        SqlConnection con = new SqlConnection("Data Source=DELL;Initial Catalog=Company;User ID=sa;Password=m8002088");
        string[] deptarray = new string[] { "Development", "Support", "HR" };

        SqlCommand cmd;
        SqlDataReader dr;
        int id= 1;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.DataSource = deptarray;
            display();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            con.Open();
            cmd = new SqlCommand("select max(eid) from employee",con);
            int cnt = (int)cmd.ExecuteScalar();
            cnt = cnt + 1;
            textBox1.Text = Convert.ToString(cnt);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            con.Open();
             cmd = new SqlCommand("select max(eid) from employee",con);
             int j = (int)cmd.ExecuteScalar();
             if (Convert.ToInt32(textBox1.Text) > j)
             {

                 cmd = new SqlCommand("insert into employee values(" + textBox1.Text + ",'" + textBox2.Text + "','" + comboBox1.SelectedItem.ToString() + "'," + textBox3.Text + ")", con);
                 int r = cmd.ExecuteNonQuery();
                 if (r > 0)
                     MessageBox.Show("Row Inserted!!!");
                 else
                     MessageBox.Show("Insert Data");
             }
             else
             {
                // cmd = new SqlCommand("insert into employee values(" + textBox1.Text + ",'" + textBox2.Text + "','" + comboBox1.SelectedItem.ToString() + "'," + textBox3.Text + ")", con);
                 cmd = new SqlCommand("update employee set ename='" + textBox2.Text + "',dep='" + comboBox1.SelectedItem.ToString() + "',age=" + textBox3.Text + " where eid=" + Convert.ToUInt32(textBox1.Text) + "", con);
               
                 int r = cmd.ExecuteNonQuery();
                 if (r > 0)
                     MessageBox.Show("Row udated!!!");
                 else
                     MessageBox.Show("Not updated");
             }
            con.Close();
            display();
        }
        public void display()
        {
            con.Open();
            cmd = new SqlCommand("select * from employee", con);
            dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            dataGridView1.DataSource = dt;
            con.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
          
        }

        private void button5_Click(object sender, EventArgs e)
        {
           
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
     
           DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
           textBox1.Text = row.Cells[0].Value.ToString();

        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {

            DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
            textBox1.Text = row.Cells[0].Value.ToString();
        }
      

        private void button2_Click(object sender, EventArgs e)
        {

        }
    }
}