Вы находитесь на странице: 1из 6

//Add Student Code

using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Text; System.Windows.Forms; System.Data.SqlClient;

namespace EISys { public partial class AddStudent : Form { public AddStudent() { InitializeComponent(); } private void btnCancel_Click(object sender, EventArgs e) { this.Close(); }

private void btnSave_Click(object sender, EventArgs e) { try { //GET THE DATA IN THE FORM string StudNo, LName, FName, MI, Gender="DUMMY", Program; StudNo = txtStudentNumber.Text.ToString(); LName = @txtLName.Text; FName = @txtFName.Text; MI = txtMI.Text.ToString(); if (rdoMale.Checked) Gender = "Male"; if (rdoFemale.Checked) Gender = "Female"; Program = cboProgram.Text.ToString(); //ACCESS THE DATA SOURCE string connString = @"Data Source=Hazell;Initial Catalog=Enrollment;Integrated Security=True"; SqlConnection connStudent = new SqlConnection(connString); connStudent.Open(); string sqlInsertStatement = @"Insert into Student Values ('" + StudNo + "','" + LName + "', '" + FName + "', '" + MI + "', '" + Gender + "', '" + Program + "' )"; SqlCommand cmdTxt = new SqlCommand(sqlInsertStatement, connStudent); cmdTxt.ExecuteNonQuery(); MessageBox.Show("Saved!"); //CLOSE THE DATA SOURCE connStudent.Close(); connStudent.Dispose(); this.Close();

} catch (Exception ex) { MessageBox.Show(ex.Message); } } private void txtStudentNumber_KeyDown(object sender, KeyEventArgs e) { if (e.KeyValue == 13) txtLName.Focus(); } } }

//Edit/Delete Student Code


using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Text; System.Windows.Forms; System.Data.SqlClient;

namespace EISys { public partial class EditDeleteStudent : Form { public EditDeleteStudent() { InitializeComponent(); } private void txtStudentNumber_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) {

string connString = @"Data Source=Hazell;Initial Catalog=Enrollment;Integrated Security=True"; SqlConnection connStudent = new SqlConnection(connString); connStudent.Open(); string sqlSelectStatement = @"select * from Student where StudentNumber= '" + @txtStudentNumber.Text + "'"; SqlCommand cmdTxt = new SqlCommand(sqlSelectStatement, connStudent); SqlDataReader dtrStudent = cmdTxt.ExecuteReader(); dtrStudent.Read(); try { if ((dtrStudent["Lastname"].ToString().Trim().Length != 0)) { //ENABLE OTHER CONTROLS txtLName.Enabled = true; txtFName.Enabled = true; txtMI.Enabled = true; rdoFemale.Enabled = true; rdoMale.Enabled = true; cboProgram.Enabled = true; cboYearLevel.Enabled = true; btnSave.Enabled = true; btnDelete.Enabled = true;

//FILL UP THOSE OBJECT BY GETTING THE DATA FROM TABLE Student txtLName.Text = dtrStudent["Lastname"].ToString(); txtFName.Text = dtrStudent["Firstname"].ToString(); txtMI.Text = dtrStudent["MI"].ToString(); if (dtrStudent["Gender"].ToString().Trim() == "Male") rdoMale.Checked = true; if (dtrStudent["Gender"].ToString().Trim() == "Female") rdoFemale.Checked = true; cboProgram.Text = dtrStudent["Program"].ToString(); } catch (Exception ex) { MessageBox.Show("No Record Found", "Warning! ", MessageBoxButtons.OK, MessageBoxIcon.Warning); //MessageBox.Show(ex.Message); } connStudent.Close(); connStudent.Dispose();

} }

private void btnCancel_Click(object sender, EventArgs e) { this.Close();

} private void btnSave_Click(object sender, EventArgs e) { //GET THE DATA IN THE FORM string StudNo, LName, FName, MI, Gender=" " , Program = " ", YearLevel, Nationality; StudNo = txtStudentNumber.Text.ToString(); LName = txtLName.Text.ToString(); FName = txtFName.Text.ToString(); MI = txtMI.Text.ToString(); if (rdoMale.Checked) Gender = "Male"; if (rdoFemale.Checked) Gender = "Female"; Program = cboProgram.Text.ToString(); //CONNECT string connString = @"Data Source=Hazell;Initial Catalog=Enrollment;Integrated Security=True"; SqlConnection connStudent = new SqlConnection(connString); connStudent.Open(); string sqlUPDATEstring = @"UPDATE Student SET LastName = '" + LName + "', FirstName = '" + FName + "', MI = '" + MI + "', Gender = '" + Gender + "'" + "WHERE (StudentNumber = '" + @txtStudentNumber.Text + "')"; SqlCommand UPDATEcmd = new SqlCommand(sqlUPDATEstring); UPDATEcmd.Connection = connStudent; UPDATEcmd.ExecuteNonQuery(); connStudent.Close(); connStudent.Dispose(); MessageBox.Show("Done"); this.Close(); } private void btnDelete_Click(object sender, EventArgs e) { DialogResult dlgResult = new DialogResult(); dlgResult = MessageBox.Show("Are you sure?", "Warning!",MessageBoxButtons.YesNo,MessageBoxIcon.Warning); if (dlgResult == DialogResult.Yes) { string connString = @"Data Source=Hazell;Initial Catalog=Enrollment;Integrated Security=True"; SqlConnection connStudent = new SqlConnection(connString);

connStudent.Open(); string sqlDELETEstring = @"DELETE FROM Student WHERE (StudentNumber = '" + @txtStudentNumber.Text + "')"; SqlCommand cmdDELETE = new SqlCommand(sqlDELETEstring); cmdDELETE.Connection = connStudent; cmdDELETE.ExecuteNonQuery(); connStudent.Close(); connStudent.Dispose(); MessageBox.Show("Done!"); this.Close(); } } } }

Вам также может понравиться