07-05-2011, 02:54 PM
Add Teacher.cs
Code:
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.OleDb;
namespace SchoolManagementSystem
{
public partial class AddTeacher : Form
{
OleDbConnection con = new OleDbConnection(ConnectionStringForSMS.MyConnectionString());
string teacherid;
string teachername;
public AddTeacher()
{
InitializeComponent();
}
private void AddTeacher_Load(object sender, EventArgs e)
{
OleDbDataAdapter da23 = new OleDbDataAdapter("select schooltitle,Address from schooltitle", con);
DataSet ds23 = new DataSet();
da23.Fill(ds23);
string schooltitle = ds23.Tables[0].Rows[0].ItemArray[0].ToString();
string address = ds23.Tables[0].Rows[0].ItemArray[1].ToString();
this.Text = schooltitle + "-Add New Teachers";
label21.Text = "";
label21.Text = schooltitle;
txtboxteacherid.Focus();
}
private void btnsave_Click(object sender, EventArgs e)
{
try
{
if (tabControl1.SelectedTab.Text == "Teacher")
{
if (txtboxteacherid.Text.Trim() != "")
{
if (txtboxfirstname.Text.Trim() != "" && txtboxfirstname.Text.Length >= 3)
{
if (txtboxsurname.Text.Trim() != "")
{
if (txtboxphno.Text.Trim() != "")
{
if (txtboxphmo.Text.Trim() != "")
{
if (txtboxaddress.Text.Trim() != "")
{
if (cmbmastatus.SelectedIndex != -1)
{
if (cmbmastatus.SelectedItem.ToString() != "--Select--")
{
if (cmbgender.SelectedIndex != -1)
{
if (cmbgender.SelectedItem.ToString() != "--Select--")
{
if (txtboxqualification.Text.Trim() != "")
{
if (txtboxname.Text.Trim() != "")
{
if (txtboxpno.Text.Trim() != "")
{
if (txtboxphmo.Text.Length < 10 || txtboxphmo.Text.Length >= 15)
{
MessageBox.Show("Invalid Mobile No and minimum 10 characters allowed", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtboxphmo.Focus();
}
else
{
if (txtboxphno.Text.Length < 10 || txtboxphno.Text.Length >= 15)
{
MessageBox.Show("Invalid Phone No and minimum 10 characters allowed", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtboxphno.Focus();
}
else
{
if (txtboxpno.Text.Length < 10 || txtboxpno.Text.Length >= 15)
{
MessageBox.Show("Invalid Phone No in Emergency and minimum 10 characters allowed", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtboxpno.Focus();
}
else
{
string pattern = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|" +
@"0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*([-][a-z|0-9]+)*\.([a-z]" +
@"[a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";
System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(txtboxemailid.Text.Trim(), pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
if (!match.Success && txtboxemailid.Text != "")
{
MessageBox.Show("You must specify valid E-mail", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.txtboxemailid.Focus();
}
else
{
string tid = txtboxteacherid.Text;
string firstname = txtboxfirstname.Text;
string surname = txtboxsurname.Text;
teachername = firstname + " " + surname;
string phonenumber = txtboxphno.Text;
string mobile = txtboxphmo.Text;
string emailid = txtboxemailid.Text;
string marialstatus = cmbmastatus.SelectedItem.ToString();
string gender = cmbgender.SelectedItem.ToString();
string qualification = txtboxqualification.Text;
string dateofjoin = dateTimePicker1.Value.Date.ToShortDateString();
string name = txtboxname.Text;
string phoneno = txtboxpno.Text;
string address = txtboxaddress.Text;
OleDbDataAdapter da1 = new OleDbDataAdapter("Select SchoolTitle,Address from Schooltitle", con);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
string Schooltitle = ds1.Tables[0].Rows[0]["Schooltitle"].ToString();
string SchoolAddress = ds1.Tables[0].Rows[0]["Address"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand("insert into AddTeacher(TeacherID,FirstName,SurName,PhoneNumber,PhoneMobile,EmailID,MarialStatus,Gender,Qualification,DateofJoin,Name,PhoneNo,Address,SchoolTitle,SchoolAddress)values('" + tid + "','" + firstname + "','" + surname + "','" + phonenumber + "','" + mobile + "','" + emailid + "','" + marialstatus + "','" + gender + "','" + qualification + "','" + dateofjoin + "','" + name + "','" + phoneno + "','" + address + "','" + Schooltitle + "','" + SchoolAddress + "')", con);
cmd.ExecuteNonQuery();
MessageBox.Show("Successfully Saved and Please Fill the Salary details for the Teacher");
lblteacherid.Text = txtboxteacherid.Text;
txtboxteacherid.Text = "";
tabControl1.SelectedIndex = 1;
btnsave.Enabled = false;
txtboxteachername.Text = teachername;
txtboxfirstname.Text = "";
txtboxsurname.Text = "";
txtboxphno.Text = "";
txtboxphmo.Text = "";
txtboxpno.Text = "";
cmbgender.SelectedIndex = 0;
cmbmastatus.SelectedIndex = 0;
txtboxaddress.Text = "";
txtboxemailid.Text = "";
txtboxname.Text = "";
txtboxqualification.Text = "";
dateTimePicker1.Text = "Wednesday, July 01, 2009";
}
}
}
}
}
else
{
MessageBox.Show("Please Enter Phone Number in Emergency", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtboxpno.Focus();
}
}
else
{
MessageBox.Show("Please Enter Name in Emergency", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtboxname.Focus();
}
}
else
{
MessageBox.Show("Please Enter Qualification", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtboxqualification.Focus();
}
}
else
{
MessageBox.Show("Please Select Gender", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
cmbgender.Focus();
}
}
else
{
MessageBox.Show("Please Select Gender", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
cmbgender.Focus();
}
}
else
{
MessageBox.Show("Please Select Marital Status", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
cmbmastatus.Focus();
}
}
else
{
MessageBox.Show("Please Select Marital Status", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
cmbmastatus.Focus();
}
}
else
{
MessageBox.Show("Please Enter Address", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtboxaddress.Focus();
}
}
else
{
MessageBox.Show("Please Enter Mobile Number", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtboxphmo.Focus();
}
}
else
{
MessageBox.Show("Please Enter Landline Number", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtboxphno.Focus();
}
}
else
{
MessageBox.Show("Please Enter Teacher SurName", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtboxsurname.Focus();
}
}
else
{
MessageBox.Show("Please Enter Teacher FirstName and minimum 3 characters allowed", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtboxfirstname.Focus();
}
}
else
{
MessageBox.Show("Please Enter TeacherID", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtboxteacherid.Focus();
}
}
else
{
if (txtboxteachername.Text.Trim() != "")
{
if (txtboxjobtitle.Text.Trim() != "" && txtboxjobtitle.Text.Length >= 3)
{
if (txtboxsal.Text.Trim() != "" && txtboxsal.Text.Length >= 3)
{
string te = lblteacherid.Text;
string jobtitle = txtboxjobtitle.Text;
string salarydate = dateTimePicker2.Value.Date.ToShortDateString();
string bonus = txtboxbonus.Text;
int b = Convert.ToInt32(bonus);
string salaryamount = txtboxsalamount.Text;
int s = Convert.ToInt32(salaryamount);
int k = 0;
if (k == b)
{
b = 0;
con.Open();
OleDbCommand cmd = new OleDbCommand("insert into Salaryinfo(TeacherID,JobTitle,SalaryDate,Bonus,NetSalaryAmount)values('" + te + "','" + jobtitle + "','" + salarydate + "'," + b + "," + s + ")", con);
cmd.ExecuteNonQuery();
MessageBox.Show("Successfully Saved", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
tabControl1.SelectedIndex = 0;
txtboxteacherid.Focus();
txtboxteachername.Text = "";
txtboxjobtitle.Text = "";
txtboxsalamount.Text = "";
txtboxbonus.Text = "0";
txtboxsal.Text = "";
dateTimePicker2.Text = "Wednesday, July 01, 2009";
}
else
{
con.Open();
OleDbCommand cmd = new OleDbCommand("insert into Salaryinfo(TeacherID,JobTitle,SalaryDate,Bonus,NetSalaryAmount)values('" + te + "','" + jobtitle + "','" + salarydate + "'," + b + "," + s + ")", con);
cmd.ExecuteNonQuery();
MessageBox.Show("Successfully Saved", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
tabControl1.SelectedIndex = 0;
txtboxteacherid.Focus();
txtboxteachername.Text = "";
txtboxjobtitle.Text = "";
txtboxsalamount.Text = "";
txtboxbonus.Text = "0";
txtboxsal.Text = "";
dateTimePicker2.Text = "Wednesday, July 01, 2009";
}
}
else
{
MessageBox.Show("Please Enter Salary and minimum 3 numbers allowed", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtboxsal.Focus();
}
}
else
{
MessageBox.Show("Please Enter Job Title and minimum 3 characters allowed", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtboxjobtitle.Focus();
}
}
else
{
MessageBox.Show("Please Fill the All Fields in Teacher", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtboxteachername.Focus();
}
}
}
catch (Exception ex)
{
MessageBox.Show("TeacherID already exist", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtboxteacherid.Focus();
}
finally
{
con.Close();
}
}
private void txtboxsal_TextChanged(object sender, EventArgs e)
{
}
private void btncancel_Click(object sender, EventArgs e)
{
if (tabControl1.SelectedTab.Text == "Teacher")
{
txtboxteacherid.Text = "";
txtboxfirstname.Text = "";
txtboxsurname.Text = "";
txtboxphno.Text = "";
txtboxphmo.Text = "";
txtboxpno.Text = "";
cmbgender.SelectedIndex = 0;
cmbmastatus.SelectedIndex = 0;
txtboxaddress.Text = "";
txtboxemailid.Text = "";
txtboxname.Text = "";
txtboxqualification.Text = "";
dateTimePicker1.Text = "Wednesday, July 01, 2009";
txtboxteacherid.Focus();
}
else
{
txtboxjobtitle.Text = "";
txtboxsalamount.Text = "";
txtboxbonus.Text = "0";
txtboxsal.Text = "";
dateTimePicker2.Text = "Wednesday, July 01, 2009";
txtboxjobtitle.Focus();
}
}
private bool nonNumberEntered = false;
private void txtboxfirstname_KeyDown(object sender, KeyEventArgs e)
{
nonNumberEntered = false;
if (e.KeyCode < Keys.A || e.KeyCode > Keys.Z)
{
if (e.KeyCode != Keys.Space)
{
if (e.KeyCode != Keys.Back)
{
nonNumberEntered = true;
}
}
}
}
private void txtboxfirstname_KeyPress(object sender, KeyPressEventArgs e)
{
if (nonNumberEntered == true)
{
e.Handled = true;
}
}
private void txtboxsurname_KeyDown(object sender, KeyEventArgs e)
{
nonNumberEntered = false;
if (e.KeyCode < Keys.A || e.KeyCode > Keys.Z)
{
if (e.KeyCode != Keys.Space)
{
if (e.KeyCode != Keys.Back)
{
nonNumberEntered = true;
}
}
}
}
private void txtboxsurname_KeyPress(object sender, KeyPressEventArgs e)
{
if (nonNumberEntered == true)
{
e.Handled = true;
}
}
private void txtboxphno_KeyDown(object sender, KeyEventArgs e)
{
nonNumberEntered = false;
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
if (e.KeyCode != Keys.Back)
{
nonNumberEntered = true;
}
}
}
}
private void txtboxphno_KeyPress(object sender, KeyPressEventArgs e)
{
if (nonNumberEntered == true)
{
e.Handled = true;
}
}
private void txtboxphmo_KeyDown(object sender, KeyEventArgs e)
{
nonNumberEntered = false;
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
if (e.KeyCode != Keys.Back)
{
nonNumberEntered = true;
}
}
}
}
private void txtboxphmo_KeyPress(object sender, KeyPressEventArgs e)
{
if (nonNumberEntered == true)
{
e.Handled = true;
}
}
private void txtboxjobtitle_KeyDown(object sender, KeyEventArgs e)
{
nonNumberEntered = false;
if (e.KeyCode < Keys.A || e.KeyCode > Keys.Z)
{
if (e.KeyCode != Keys.Space)
{
if (e.KeyCode != Keys.Back)
{
nonNumberEntered = true;
}
}
}
}
private void txtboxjobtitle_KeyPress(object sender, KeyPressEventArgs e)
{
if (nonNumberEntered == true)
{
e.Handled = true;
}
}
private void txtboxbonus_KeyDown(object sender, KeyEventArgs e)
{
nonNumberEntered = false;
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
if (e.KeyCode != Keys.Back)
{
nonNumberEntered = true;
}
}
}
}
private void txtboxbonus_KeyPress(object sender, KeyPressEventArgs e)
{
if (nonNumberEntered == true)
{
e.Handled = true;
}
}
private void txtboxsal_KeyDown(object sender, KeyEventArgs e)
{
nonNumberEntered = false;
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
if (e.KeyCode != Keys.Back)
{
nonNumberEntered = true;
}
}
}
}
private void txtboxsal_KeyPress(object sender, KeyPressEventArgs e)
{
if (nonNumberEntered == true)
{
e.Handled = true;
}
}
private void txtboxemailid_TextChanged(object sender, EventArgs e)
{
}
private void txtboxsal_TabIndexChanged(object sender, EventArgs e)
{
}
private void btnnetsal_Click(object sender, EventArgs e)
{
if (txtboxsal.Text.Trim() != "")
{
if (txtboxbonus.Text.Trim() != "")
{
if (txtboxsal.Text.Trim() != "")
{
long b = Convert.ToInt64(txtboxbonus.Text);
long s = Convert.ToInt64(txtboxsal.Text);
long sal = b + s;
txtboxsalamount.Text = sal.ToString();
}
else
{
int b = 0;
int s = Convert.ToInt32(txtboxsal.Text);
int sal = b + s;
txtboxsalamount.Text = sal.ToString();
}
}
else
{
int b = 0;
int s = Convert.ToInt32(txtboxsal.Text);
int sal = b + s;
txtboxsalamount.Text = sal.ToString();
}
btnsave.Enabled = true;
}
else
{
MessageBox.Show("Please Enter Salary", "School Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtboxsal.Focus();
if (txtboxsalamount.Text.Trim() != "")
{
btnsave.Enabled = true;
}
else
{
btnsave.Enabled = false;
}
}
}
private void txtboxname_KeyDown(object sender, KeyEventArgs e)
{
nonNumberEntered = false;
if (e.KeyCode < Keys.A || e.KeyCode > Keys.Z)
{
if (e.KeyCode != Keys.Space)
{
if (e.KeyCode != Keys.Back)
{
nonNumberEntered = true;
}
}
}
}
private void txtboxname_KeyPress(object sender, KeyPressEventArgs e)
{
if (nonNumberEntered == true)
{
e.Handled = true;
}
}
private void txtboxpno_KeyDown(object sender, KeyEventArgs e)
{
nonNumberEntered = false;
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
if (e.KeyCode != Keys.Back)
{
nonNumberEntered = true;
}
}
}
}
private void txtboxpno_KeyPress(object sender, KeyPressEventArgs e)
{
if (nonNumberEntered == true)
{
e.Handled = true;
}
}
private void tabControl1_TabIndexChanged(object sender, EventArgs e)
{
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab.Text == "Teacher")
{
txtboxteacherid.Focus();
btnsave.Enabled = true;
}
else
{
txtboxjobtitle.Focus();
if (txtboxsalamount.Text.Trim() == "")
{
btnsave.Enabled = false;
}
else
{
btnsave.Enabled = true;
}
}
}
private void txtboxfirstname_Leave(object sender, EventArgs e)
{
txtboxfirstname.Text = ConnectionStringForSMS.Capitalize(txtboxfirstname.Text);
}
private void txtboxsurname_Leave(object sender, EventArgs e)
{
txtboxsurname.Text = ConnectionStringForSMS.Capitalize(txtboxsurname.Text);
}
private void txtboxaddress_Leave(object sender, EventArgs e)
{
txtboxaddress.Text = ConnectionStringForSMS.Capitalize(txtboxaddress.Text);
}
private void txtboxemailid_Leave(object sender, EventArgs e)
{
txtboxemailid.Text = ConnectionStringForSMS.Capitalize(txtboxemailid.Text);
}
private void txtboxqualification_Leave(object sender, EventArgs e)
{
txtboxqualification.Text = ConnectionStringForSMS.Capitalize(txtboxqualification.Text);
}
private void txtboxname_Leave(object sender, EventArgs e)
{
txtboxname.Text = ConnectionStringForSMS.Capitalize(txtboxname.Text);
}
private void txtboxjobtitle_Leave(object sender, EventArgs e)
{
txtboxjobtitle.Text = ConnectionStringForSMS.Capitalize(txtboxjobtitle.Text);
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
this.Close();
EntryForm e1 = new EntryForm();
e1.Show();
}
private void pictureBox2_Click(object sender, EventArgs e)
{
this.Close();
SMSMainForm a = new SMSMainForm();
a.Show();
}
}
}