private void btnSave_Click(object sender, EventArgs e) { try { int rowsaffected = MitchellDAL.EditTutor(Convert.ToInt32(tb_ID.Text), tb_Name.Text, tb_Surname.Text, tb_Address1.Text, tb_Address2.Text, tb_Postcode.Text, tb_Tel.Text, tb_Instrument.Text); if (rowsaffected == 1) { add.showBalloonTip("Success", "Successfully Updated Tutor Details"); } else { add.showBalloonTip("UnSuccessful", "Could not update Tutor Details"); } // this.Dispose(); this.Hide(); Tutor_Details mynextscreen = new Tutor_Details(); mynextscreen.ShowDialog(); Close(); } catch (Exception ex) { if (count1 < 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(); count1++; } }
private void button1_Click(object sender, EventArgs e) { try { int a = dataGridView1.CurrentCell.ColumnIndex; int b = dataGridView3.CurrentCell.ColumnIndex; int c = dataGridView2.CurrentCell.ColumnIndex; if (a == 0) { //add.showBalloonTip( dataGridView1.CurrentCell.Value.ToString(), "Student ID Selected"); x = int.Parse(dataGridView1.CurrentCell.Value.ToString()); if (b == 0) { //add.showBalloonTip(dataGridView3.CurrentCell.Value.ToString(), "Tutor ID Selected"); y = int.Parse(dataGridView3.CurrentCell.Value.ToString()); if (c == 0) { //add.showBalloonTip(dataGridView2.CurrentCell.Value.ToString(), "Room ID Selected"); z = int.Parse(dataGridView2.CurrentCell.Value.ToString()); BookingDAL.addNewBooking(x, y, z, cboAllocated.Text, cboDay.Text, cboTime.Text, comboBox2.Text, int.Parse(textBox1.Text), int.Parse(comboBox1.Text)); add.showBalloonTip("Success", "Successfully Add Booking"); cboAllocated.SelectedIndex = -1; cboDay.SelectedIndex = -1; cboTime.SelectedIndex = -1; comboBox1.SelectedIndex = -1; comboBox2.SelectedIndex = -1; textBox1.Text = ""; dataGridView1.ClearSelection(); dataGridView2.ClearSelection(); dataGridView3.ClearSelection(); } else { add.showBalloonTip("Room Number ", "Please Select a Room Number"); } } else { add.showBalloonTip("Tutor ID ", "Please Select a Tutor ID"); } } else { add.showBalloonTip("Student ID ", "Please Select a Student ID"); } } catch (Exception ex) { 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(); } }
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 StudentDetails_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'mitchellMusicDBDataSet2.tbl_Student' table. You can move, or remove it, as needed. this.tbl_StudentTableAdapter1.Fill(this.mitchellMusicDBDataSet2.tbl_Student); this.tbl_StudentTableAdapter.Fill(this.mitchellMusicDBDataSet.tbl_Student); string _Connectionstring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MitchellMusicDB.mdf;Integrated Security=True"; try { //This code executes a query with the data being wrote to a datatable which is viewed //in a datagridview with the other datagridview .visible command becomming false using (SqlConnection connection = new SqlConnection(_Connectionstring)) { connection.Open(); string sqlQuery = string.Format("SELECT * FROM tbl_Student "); SqlCommand insertCommand = new SqlCommand(sqlQuery, connection); int rowsAffected = insertCommand.ExecuteNonQuery(); DataTable table = new DataTable(); SqlDataAdapter a = new SqlDataAdapter(sqlQuery, connection); a.Fill(table); connection.Dispose(); connection.Close(); DataStudentView.DataSource = table; } //Checks each record pre payment status and if the answer is 'False' the colour is changed to red for (int i = 0; i < DataStudentView.Rows.Count; i++) { string val = DataStudentView.Rows[i].Cells[12].Value.ToString(); if (val == "False") { DataStudentView.Rows[i].DefaultCellStyle.BackColor = Color.Red; } } } 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 btnName_Click(object sender, EventArgs e) { string _Connectionstring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MitchellMusicDB.mdf;Integrated Security=True"; try { //This code executes a query with the data being wrote to a datatable which is viewed //in a datagridview with the other datagridview .visible command becomming false using (SqlConnection connection = new SqlConnection(_Connectionstring)) { if (tbName.Text != "") { connection.Open(); string sqlQuery = string.Format("SELECT * FROM Tbl_Tutor where Tutor_Surname = '{0}'", tbName.Text); SqlCommand insertCommand = new SqlCommand(sqlQuery, connection); int rowsAffected = insertCommand.ExecuteNonQuery(); DataTable table = new DataTable(); SqlDataAdapter a = new SqlDataAdapter(sqlQuery, connection); a.Fill(table); connection.Dispose(); connection.Close(); DataTutorView.DataSource = table; } else { connection.Open(); string sqlQuery = string.Format("SELECT * FROM Tbl_Tutor"); SqlCommand insertCommand = new SqlCommand(sqlQuery, connection); int rowsAffected = insertCommand.ExecuteNonQuery(); DataTable table = new DataTable(); SqlDataAdapter a = new SqlDataAdapter(sqlQuery, connection); a.Fill(table); connection.Dispose(); connection.Close(); DataTutorView.DataSource = table; } } } //Any Exception that is caught by the Try-Catch will be checked if this has been caught by the //try catch if so will not display a Notification message but will still write to the Error Log //Text File 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 btnSave_Click(object sender, EventArgs e) { try { if (Controls.OfType <TextBox>().Any(t => t.Text == "")) { student.showBalloonTip("Field entries", "All Fields must be used"); } else if (Controls.OfType <ComboBox>().Any(t => t.Text == "")) { student.showBalloonTip("Field entries", "All Fields must be used"); } else { int rowsAffected = MitchellDAL.AddNewTutor(Convert.ToInt32(tb_ID.Text), tb_Name.Text, tb_Surname.Text, tb_Address1.Text, tb_Address2.Text, tb_Postcode.Text, tb_Tel.Text, tb_Instrument.Text, tb_DOB.Text); if (rowsAffected == 1) { student.showBalloonTip("Success", "Successfully Added Tutor"); } else { student.showBalloonTip("Unsuccessful", "Could not add Tutor"); } //this.Dispose(); this.Hide(); Tutor_Details mynextscreen = new Tutor_Details(); mynextscreen.ShowDialog(); Dispose(); } } 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 btnTutor_Click(object sender, EventArgs e) { //This code sets up a connection between the project and the database and creates and executes //the query before filling a datatable with the results of the query string _Connectionstring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MitchellMusicDB.mdf;Integrated Security=True"; try { using (SqlConnection connection = new SqlConnection(_Connectionstring)) { connection.Open(); string sqlQuery = string.Format("SELECT * FROM tbl_Lesson WHERE Tutor_ID = '{0}'", int.Parse(textBox1.Text)); SqlCommand insertCommand = new SqlCommand(sqlQuery, connection); int rowsAffected = insertCommand.ExecuteNonQuery(); DataTable table = new DataTable(); SqlDataAdapter a = new SqlDataAdapter(sqlQuery, connection); a.Fill(table); connection.Dispose(); connection.Close(); dgvViewBookings.DataSource = table; } } //Any Exception that is caught by the Try-Catch will be checked if this has been caught by the //try catch if so will not display a Notification message but will still write to the Error Log //Text File 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 btnSave_Click(object sender, EventArgs e) { try { string _Connectionstring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MitchellMusicDB.mdf;Integrated Security=True"; int studentID, TutorID, RoomID; string[] nameParts = cboStudent.Text.Split(' '); string firstName = nameParts[0]; string lastName = nameParts[1]; string[] nameParts1 = cboTutor.Text.Split(' '); string firstName1 = nameParts1[0]; string lastName1 = nameParts1[1]; bool studentBooking = false, TutorAvailable = true, RoomUsed = false; using (SqlConnection connection = new SqlConnection(_Connectionstring)) { connection.Open(); string sqlQuery = string.Format("SELECT * FROM tbl_Student where Student_Name = '{0}' and Student_Surname = '{1}'", firstName, lastName); string sqlQuery1 = string.Format("SELECT * FROM Tbl_Tutor where Tutor_Name = '{0}' and Tutor_Surname = '{1}'", firstName1, lastName1); // string sqlQuery2 = string.Format("SELECT * FROM tbl_Lesson where Student_ID = '{0}' and Lesson_Allocated = '{1}'", studentID, cboBlock.Text); SqlCommand insertCommand = new SqlCommand(sqlQuery, connection); //SqlCommand insertCommand1 = new SqlCommand(sqlQuery1, connection); SqlDataReader rdr = insertCommand.ExecuteReader(); // if (rdr.Read()) { studentID = int.Parse(rdr[0].ToString()); rdr.Close(); } else { studentID = 0; } rdr.Close(); SqlCommand insertCommand1 = new SqlCommand(sqlQuery1, connection); SqlDataReader rdr1 = insertCommand1.ExecuteReader(); if (rdr1.Read()) { TutorID = int.Parse(rdr1[0].ToString()); rdr1.Close(); } else { TutorID = 0; } #region student Double Booked Check string sqlQuery2 = string.Format("SELECT * FROM tbl_Lesson where Student_ID = '{0}' and Lesson_Allocated = '{1}'", studentID, cboBlock.Text); SqlCommand insertCommand2 = new SqlCommand(sqlQuery2, connection); SqlDataReader rdr2 = insertCommand2.ExecuteReader(); if (rdr2.HasRows) { studentBooking = false; } else { studentBooking = true; } rdr2.Close(); #endregion #region Room Check string sqlQuery3 = string.Format("SELECT * FROM tbl_Lesson where Room_ID = '{0}' and Lesson_Allocated = '{1}' and Lesson_Day = '{2}' and Lesson_Time = '{3}'", int.Parse(cboRooms.Text), cboBlock.Text, cboDay.Text, cboTime.Text); SqlCommand insertCommand3 = new SqlCommand(sqlQuery3, connection); SqlDataReader rdr3 = insertCommand3.ExecuteReader(); if (rdr3.HasRows) { RoomUsed = false; } else { RoomUsed = true; } rdr3.Close(); #endregion #region Tutor Already used string sqlQuery4 = string.Format("SELECT * FROM tbl_Lesson where Tutor_ID = '{0}' and Lesson_Allocated = '{1}' and Lesson_Day = '{2}' and Lesson_Time = '{3}'", TutorID, cboBlock.Text, cboDay.Text, cboTime.Text); SqlCommand insertCommand4 = new SqlCommand(sqlQuery4, connection); SqlDataReader rdr4 = insertCommand3.ExecuteReader(); if (rdr4.HasRows) { TutorAvailable = false; } else { TutorAvailable = true; } rdr4.Close(); #endregion connection.Dispose(); connection.Close(); //MessageBox.Show("Student ID: " + studentID.ToString() + "Tutor ID: " + TutorID.ToString()); } RoomID = int.Parse(cboRooms.Text); try { DAL.BookingDAL.UpdateBooking(studentID, TutorID, cboBlock.Text, int.Parse(cboLessons.Text), cboPayment.Text, int.Parse(tbAmount.Text), cboTime.Text, cboDay.Text); } 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(); frmViewBookings nextScreen = new frmViewBookings(); nextScreen.ShowDialog(); this.Dispose(); this.Hide(); } catch (Exception ex) { if (count1 < 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(); count1++; } }
private void btnSave_Click(object sender, EventArgs e) { try { if (Controls.OfType <TextBox>().Any(t => t.Text == "")) { add.showBalloonTip("Field entries", "All Fields must be used"); } else if (Controls.OfType <ComboBox>().Any(t => t.Text == "")) { add.showBalloonTip("Field entries", "All Fields must be used"); } else { string _Connectionstring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MitchellMusicDB.mdf;Integrated Security=True"; int studentID, TutorID, RoomID; string[] nameParts = cboStudent.Text.Split(' '); string firstName = nameParts[0]; string lastName = nameParts[1]; string[] nameParts1 = cboTutor.Text.Split(' '); string firstName1 = nameParts1[0]; string lastName1 = nameParts1[1]; bool studentBooking, TutorAvailable, RoomUsed; using (SqlConnection connection = new SqlConnection(_Connectionstring)) { connection.Open(); string sqlQuery = string.Format("SELECT * FROM tbl_Student where Student_Name = '{0}' and Student_Surname = '{1}'", firstName.ToString(), lastName.ToString()); string sqlQuery1 = string.Format("SELECT * FROM Tbl_Tutor where Tutor_Name = '{0}' and Tutor_Surname = '{1}'", firstName1.ToString(), lastName1.ToString()); // string sqlQuery2 = string.Format("SELECT * FROM tbl_Lesson where Student_ID = '{0}' and Lesson_Allocated = '{1}'", studentID, cboBlock.Text); SqlCommand insertCommand = new SqlCommand(sqlQuery, connection); //SqlCommand insertCommand1 = new SqlCommand(sqlQuery1, connection); SqlDataReader rdr = insertCommand.ExecuteReader(); // if (rdr.Read()) { studentID = int.Parse(rdr[0].ToString()); rdr.Close(); } else { studentID = 0; } rdr.Close(); SqlCommand insertCommand1 = new SqlCommand(sqlQuery1, connection); SqlDataReader rdr1 = insertCommand1.ExecuteReader(); if (rdr1.Read()) { TutorID = int.Parse(rdr1[0].ToString()); rdr1.Close(); } else { TutorID = 0; } #region student Double Booked Check string sqlQuery2 = string.Format("SELECT COUNT(*) FROM tbl_Lesson where Student_ID = '{0}' and Lesson_Allocated = '{1}'", studentID, cboBlock.Text); SqlCommand insertCommand2 = new SqlCommand(sqlQuery2, connection); SqlDataReader rdr2 = insertCommand2.ExecuteReader(); if (rdr2.Read()) { int x = int.Parse(rdr2[0].ToString()); if (x <= 1) { studentBooking = true; } else { studentBooking = false; } } else { studentBooking = false; } rdr2.Close(); #endregion #region Room Check string sqlQuery3 = string.Format("SELECT * FROM tbl_Lesson where Room_ID = '{0}' and Lesson_Allocated = '{1}' and Lesson_Day = '{2}' and Lesson_Time = '{3}'", int.Parse(cboRooms.Text), cboBlock.Text, cboDay.Text, cboTime.Text); SqlCommand insertCommand3 = new SqlCommand(sqlQuery3, connection); SqlDataReader rdr3 = insertCommand3.ExecuteReader(); if (rdr3.HasRows) { RoomUsed = false; } else { RoomUsed = true; } rdr3.Close(); #endregion #region Tutor Already used string sqlQuery4 = string.Format("SELECT * FROM tbl_Lesson where Tutor_ID = '{0}' and Lesson_Allocated = '{1}' and Lesson_Day = '{2}' and Lesson_Time = '{3}'", TutorID, cboBlock.Text, cboDay.Text, cboTime.Text); SqlCommand insertCommand4 = new SqlCommand(sqlQuery4, connection); SqlDataReader rdr4 = insertCommand3.ExecuteReader(); if (rdr4.HasRows) { TutorAvailable = false; } else { TutorAvailable = true; } rdr4.Close(); #endregion connection.Dispose(); connection.Close(); //MessageBox.Show("Student ID: " + studentID.ToString() + "Tutor ID: " + TutorID.ToString()); } RoomID = int.Parse(cboRooms.Text); try { if (studentBooking == false) { MessageBox.Show("Student Booking Already Exists"); } else if (TutorAvailable == false) { MessageBox.Show("Tutor already Utilised"); } else if (RoomUsed == false) { MessageBox.Show("Room Already in use"); } else { switch (cboTime.Text) { case "13:00-13:30": AdultClass(studentID, TutorID, RoomID); break; case "13:30-14:00": AdultClass(studentID, TutorID, RoomID); break; case "14:00-14:30": AdultClass(studentID, TutorID, RoomID); break; case "14:30-15:00": AdultClass(studentID, TutorID, RoomID); break; default: break; } switch (cboLessons.Text) { case "20": MessageBox.Show("There is a 5% Discount available with the 20 Lessons. Please Use Promo Code: 20L5%"); break; case "30": MessageBox.Show("There is a 10% Discount available with the 30 Lessons. Please Use Promo Code: 30L10%"); break; default: MessageBox.Show("No Discount Available"); break; } int rowsAffected = DAL.BookingDAL.addNewBooking(studentID, TutorID, RoomID, cboBlock.Text, cboDay.Text, cboTime.Text, cboPayment.Text, int.Parse(tbAmount.Text), int.Parse(cboLessons.Text)); if (rowsAffected == 1) { this.Hide(); frmViewBookings nextScreen = new frmViewBookings(); nextScreen.ShowDialog(); this.Dispose(); } else { } } } 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++; } } } catch (Exception ex) { if (count1 < 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(); count1++; } }
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++; } }
private void BtnLogin_Click(object sender, EventArgs e) { //Creates a connection to the sql Database using the connection string and uses the query string //to execute the command and retrieve data from the Database. //It then uses a Data Reader To retrieve the data and therefore check against user inputed //Data try { string _Connectionstring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MitchellMusicDB.mdf;Integrated Security=True"; using (SqlConnection connection = new SqlConnection(_Connectionstring)) { connection.Open(); string sqlQuery = string.Format("SELECT * FROM Login where Username = '******' AND Password ='******'", tbUsername.Text, tbPassword.Text); SqlCommand insertCommand = new SqlCommand(sqlQuery, connection); SqlDataReader dr = insertCommand.ExecuteReader(); //Retrieves data from database and writes them to Local Variable //Checks whether the user inputted the correct username and password //To Access the program if (dr.Read()) { string username = dr[1].ToString(); string password = dr[2].ToString(); if (tbUsername.Text == username) { if (tbPassword.Text == password) { Hide(); AdminPassword = password; frmMenu a = new frmMenu(); add.showBalloonTip("Success", "Successfully Logged on"); a.ShowDialog(); dr.Close(); } //If the user used incorrect Details it displays a Balloon Notification but //if clicked multiple times it will only display one balloon tip else { if (count < 1) { add.showBalloonTip("Unsuccessful", "Could Not Login!"); } count++; dr.Close(); } } else { if (count1 < 1) { add.showBalloonTip("Unsuccessful", "Could Not Login!"); } count1++; dr.Close(); } dr.Close(); } else { if (count2 < 1) { add.showBalloonTip("Unsuccessful", "Could Not Login!"); } count2++; dr.Close(); } connection.Dispose(); connection.Close(); } } //If Try Catch catches any error it will display a balloon notification aslong as it has //not already done so in this instance of the form. //It then Writes the error on a new line in a Error Log Text File. Before adding 1 to the Counter 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++; } }