private void frmAddTutor_Load(object sender, EventArgs e) { tb_ID.Enabled = false; #region FindMax string _Connectionstring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MitchellMusicDB.mdf;Integrated Security=True"; frmAddStudent add = new frmAddStudent(); using (SqlConnection connection = new SqlConnection(_Connectionstring)) { connection.Open(); string sqlQuery = string.Format("SELECT MAX(Tutor_Id) FROM Tbl_Tutor"); SqlCommand insertCommand = new SqlCommand(sqlQuery, connection); SqlDataReader rdr = insertCommand.ExecuteReader(); int x; // if (rdr.Read()) { x = Convert.ToInt32(rdr[0].ToString()); this.tb_ID.Text = Convert.ToString(x + 1); rdr.Close(); } connection.Dispose(); connection.Close(); //this.tb_ID.Text = Convert.ToString(x + 1); } #endregion }
private void frmAddStudent_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'mitchellMusicDataSet.Tbl_Tutor' table. You can move, or remove it, as needed. this.tbl_TutorTableAdapter.Fill(this.mitchellMusicDataSet.Tbl_Tutor); tb_ID.Enabled = false; #region FindMax string _Connectionstring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MitchellMusicDB.mdf;Integrated Security=True"; frmAddStudent add = new frmAddStudent(); using (SqlConnection connection = new SqlConnection(_Connectionstring)) { connection.Open(); string sqlQuery = string.Format("SELECT MAX(Student_Id) FROM tbl_Student "); SqlCommand insertCommand = new SqlCommand(sqlQuery, connection); SqlDataReader rdr = insertCommand.ExecuteReader(); // if (rdr.Read()) { x = Convert.ToInt32(rdr[0].ToString()); rdr.Close(); } connection.Dispose(); connection.Close(); this.tb_ID.Text = Convert.ToString(x + 1); } #endregion }
private void btn_AddDetails_Click(object sender, EventArgs e) { //Form Hidden, New Form opened, Current Resources Disposed of this.Dispose(); this.Hide(); frmAddStudent nextscreen = new frmAddStudent(); nextscreen.ShowDialog(); }
private void btnFind_Click(object sender, EventArgs e) { try { string _Connectionstring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MitchellMusicDB.mdf;Integrated Security=True"; frmAddStudent add = new frmAddStudent(); using (SqlConnection connection = new SqlConnection(_Connectionstring)) { connection.Open(); string sqlQuery = string.Format("SELECT * FROM Tbl_Tutor WHERE Tutor_Id = '{0}'", Convert.ToInt32(tb_ID.Text)); SqlCommand insertCommand = new SqlCommand(sqlQuery, connection); SqlDataReader rdr = insertCommand.ExecuteReader(); // if (rdr.Read()) { this.tb_Name.Text = rdr[1].ToString(); this.tb_Surname.Text = rdr[2].ToString(); this.tb_Address1.Text = rdr[3].ToString(); this.tb_Address2.Text = rdr[4].ToString(); this.tb_Postcode.Text = rdr[5].ToString(); this.tb_Tel.Text = rdr[6].ToString(); this.tb_Instrument.Text = rdr[7].ToString(); this.tb_DOB.Text = rdr[8].ToString(); rdr.Close(); } else { add.showBalloonTip("Unsuccessful", "Could Not find Data!"); rdr.Close(); } connection.Dispose(); connection.Close(); } } catch (Exception ex) { if (count < 1) { add.showBalloonTip("Error", "Something Went Wrong"); } System.IO.StreamWriter writer = new System.IO.StreamWriter("../../ErrorLog.txt", true); writer.WriteLine("- " + DateTime.Now + " " + ex.Message, true); writer.Flush(); writer.Close(); count++; } }
private void btnDelete_Click(object sender, EventArgs e) { try { frmAddStudent student = new frmAddStudent(); int rowsAffected = MitchellDAL.DeleteStudent(Convert.ToInt32(tb_Student_Delete.Text)); if (rowsAffected == 0) { student.showBalloonTip("Delete Successful", "Successfully deleted Student"); } else { student.showBalloonTip("Delete Unsuccessful", "Could not delete Student"); } //this.Dispose(); this.Hide(); StudentDetails mynextscreen = new StudentDetails(); mynextscreen.ShowDialog(); Close(); } catch (Exception ex) { if (count < 1) { add.showBalloonTip("Error", "Something Went Wrong"); } System.IO.StreamWriter writer = new System.IO.StreamWriter("../../ErrorLog.txt", true); writer.WriteLine("- " + DateTime.Now + " " + ex.Message, true); writer.Flush(); writer.Close(); count++; this.Hide(); StudentDetails nextScreen = new StudentDetails(); nextScreen.ShowDialog(); //this.Dispose(); Close(); } }
private void btnSurnameSearch_Click(object sender, EventArgs e) { try { // throw new IndexOutOfRangeException(); string _Connectionstring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MitchellMusicDB.mdf;Integrated Security=True"; frmAddStudent add = new frmAddStudent(); using (SqlConnection connection = new SqlConnection(_Connectionstring)) { connection.Open(); if (tb_Name.Text == "") { string sqlQuery = string.Format("SELECT * FROM tbl_Student WHERE Student_Surname LIKE '{0}'", tb_Surname.Text); SqlCommand insertCommand = new SqlCommand(sqlQuery, connection); SqlDataReader rdr = insertCommand.ExecuteReader(); // if (rdr.Read()) { this.tb_ID.Enabled = false; this.tb_ID.Text = rdr[0].ToString(); this.tb_Name.Text = rdr[1].ToString(); this.tb_Surname.Text = rdr[2].ToString(); this.tb_Address1.Text = rdr[3].ToString(); this.tb_Address2.Text = rdr[4].ToString(); this.tb_Postcode.Text = rdr[5].ToString(); this.tb_Tel.Text = rdr[6].ToString(); this.tb_Instrument.Text = rdr[7].ToString(); this.tb_Tutor.Text = rdr[8].ToString(); this.cbo_Status.Text = rdr[9].ToString(); this.cboClass.Text = rdr[11].ToString(); this.checkBox1.Checked = Convert.ToBoolean(rdr[12].ToString()); this.cboLevel.Text = rdr[13].ToString(); rdr.Close(); } else { add.showBalloonTip("Unsuccessful", "Could Not find Data!"); rdr.Close(); } } else { string sqlQuery = string.Format("SELECT * FROM tbl_Student WHERE Student_Surname LIKE '{0}' and Student_Name LIKE '{1}'", tb_Surname.Text, tb_Name.Text); SqlCommand insertCommand = new SqlCommand(sqlQuery, connection); SqlDataReader rdr = insertCommand.ExecuteReader(); if (rdr.Read()) { this.tb_ID.Enabled = false; this.tb_ID.Text = rdr[0].ToString(); this.tb_Name.Text = rdr[1].ToString(); this.tb_Surname.Text = rdr[2].ToString(); this.tb_Address1.Text = rdr[3].ToString(); this.tb_Address2.Text = rdr[4].ToString(); this.tb_Postcode.Text = rdr[5].ToString(); this.tb_Tel.Text = rdr[6].ToString(); this.tb_Instrument.Text = rdr[7].ToString(); this.tb_Tutor.Text = rdr[8].ToString(); this.cbo_Status.Text = rdr[9].ToString(); this.cboClass.Text = rdr[11].ToString(); this.checkBox1.Checked = Convert.ToBoolean(rdr[12].ToString()); this.cboLevel.Text = rdr[13].ToString(); rdr.Close(); } else { add.showBalloonTip("Unsuccessful", "Could Not find Data!"); rdr.Close(); } } connection.Dispose(); connection.Close(); } } catch (Exception ex) { if (count2 < 1) { add.showBalloonTip("Error", "Something Went Wrong"); } System.IO.StreamWriter writer = new System.IO.StreamWriter("../../ErrorLog.txt", true); writer.WriteLine("- " + DateTime.Now + " " + ex.Message, true); writer.Flush(); writer.Close(); count2++; } }