Computer Programming 3
SIMPLE PAYROLL SYSTEM WITH OBJECT ORIENTED PROGRAMMING
Form1.cs
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 WindowsFormsApplication2
{
public partial class frm_login : Form
{
SqlConnection con = new SqlConnection("Data Source=inser PC here;Initial
Catalog=payroll_db;Integrated Security=True");
public frm_login()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.AcceptButton = this.button1;
}
private void button1_Click_1(object sender, EventArgs e)
{
string query = "Select * From tbl_userpass Where Username = '" +
tb_user.Text.Trim()+ "'and Password ='" + tb_pass.Text.Trim() +" '";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
DataTable dtbl = new DataTable();
sda.Fill(dtbl);
mainform a = new mainform();
if (dtbl.Rows.Count == 1)
{
this.Hide();
a.Show();
}
else if (tb_user.Text == "" || tb_pass.Text == "")
{
MessageBox.Show("Please fill up all the fields");
}
else
{
MessageBox.Show("Invalid Username / Password");
}
}
private void button2_Click_1(object sender, EventArgs e)
{
Application.Exit();
}
private void label1_Click(object sender, EventArgs e)
{
private void tb_pass_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
AcceptButton.PerformClick();
}
}
}
}
Form1.cs [DESIGN]
mainform.cs
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 WindowsFormsApplication2
{
public partial class mainform : Form
{
SqlConnection con = new SqlConnection("Data Source=insert PC here;Initial
Catalog=payroll_db;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
public mainform()
{
InitializeComponent();
}
void ClearAllText(Control con)
{
foreach (Control c in con.Controls)
{
if (c is TextBox)
((TextBox)c).Clear();
else
ClearAllText(c);
}
}
private void button2_Click(object sender, EventArgs e)
{
string del = dataGridView1.CurrentRow.Cells[0].Value.ToString();
con.Open();
cmd = new SqlCommand("DELETE FROM tbl_addemp WHERE ID = '" + del + "'", con);
cmd.ExecuteNonQuery();
MessageBox.Show("Data Deleted");
string query = "SELECT * FROM tbl_addemp";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds, "Employee Info");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Employee Info";
dataGridView1.DataSource = dataGridView1.DataSource;
con.Close();
}
private void btn_save_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("INSERT INTO tbl_addemp (ID, Firstname, Lastname,
Address, Gender, Status, TelNum, PhoneNum, SSSNum, Rate) VALUES ('" +tb_id.Text + "','" +
tb_fn.Text + "','" + tb_ln.Text + "','" + tb_add.Text + "','" + cb_gen.Text + "','" +
cb_stat.Text + "','" + tb_tnum.Text + "','" + tb_pnum.Text + "','" + tb_sss.Text + "','"
+ cb_rate.Text + "')",con);
cmd.ExecuteNonQuery();
MessageBox.Show("Saved");
con.Close();
string query = "SELECT * FROM tbl_addemp";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds, "Employee Info");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Employee Info";
dataGridView1.DataSource = dataGridView1.DataSource;
con.Close();
ClearAllText(this);
cb_gen.SelectedIndex = -1;
cb_rate.SelectedIndex = -1;
cb_stat.SelectedIndex = -1;
}
private void mainform_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'payroll_dbDataSet.tbl_addemp'
table. You can move, or remove it, as needed.
this.tbl_addempTableAdapter.Fill(this.payroll_dbDataSet.tbl_addemp);
}
}
}
mainform.cs [DESIGN]