/// <summary> /// This is the click event of Button of add student /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAddStudent_Click(object sender, EventArgs e) { if (btnAddStudent.Text == "Update Student") { String FirstName, LastName, Contact, Email, RegistrationNumber; int Status; string ID = lblID.Text; if (IsValidString(txtFirstName.Text)) { if (IsValidString(txtLastName.Text) || txtLastName.Text.Length == 0) { if (isValidContact(txtContact.Text) || txtContact.Text.Length == 0) { if (isDigit(cmbStatus.Text) && isDigit(cmbStatus.Text)) { if (IsValidEmail(txtEmail.Text) && isValidRegistrationNumber(txtRegistrationNumber.Text)) { FirstName = txtFirstName.Text; LastName = txtLastName.Text; Contact = txtContact.Text; Email = txtEmail.Text; RegistrationNumber = txtRegistrationNumber.Text; Status = Convert.ToInt32(cmbStatus.Text); conn.Open(); string cmd = String.Format("UPDATE Student SET FirstName = @FirstName, LastName = @LastName, Contact = @Contact, Email = @Email, RegistrationNumber = @RegistrationNumber, Status = @Status WHERE Id = @ID"); SqlCommand command = new SqlCommand(cmd, conn); command.Parameters.Add(new SqlParameter("@FirstName", FirstName)); command.Parameters.Add(new SqlParameter("@LastName", LastName)); command.Parameters.Add(new SqlParameter("@Contact", Contact)); command.Parameters.Add(new SqlParameter("@Email", Email)); command.Parameters.Add(new SqlParameter("@RegistrationNumber", RegistrationNumber)); command.Parameters.Add(new SqlParameter("@Status", Status)); command.Parameters.Add(new SqlParameter("@Id", ID)); SqlDataReader reader = command.ExecuteReader(); btnAddStudent.Text = "Add Student"; SqlDataAdapter adapter = new SqlDataAdapter(cmd, conn); txtContact.Text = ""; txtEmail.Text = ""; txtFirstName.Text = ""; txtLastName.Text = ""; txtRegistrationNumber.Text = ""; cmbStatus.Text = ""; conn.Close(); TabADDStudent.Hide(); TabViewStudent.Show(); conn.Open(); cmd = String.Format("SELECT *FROM Student"); command = new SqlCommand(cmd, conn); reader = command.ExecuteReader(); adapter = new SqlDataAdapter(cmd, conn); DataTable table = new DataTable(); adapter.Fill(table); gridStudentInformation.DataSource = table; TabViewStudent.Refresh(); conn.Close(); } } } } } } else { lblID.Hide(); String FirstName, LastName, Contact, Email, RegistrationNumber; int Status; if (IsValidString(txtFirstName.Text)) { if (IsValidString(txtLastName.Text) || txtLastName.Text.Length == 0) { if (isValidContact(txtContact.Text) || txtContact.Text.Length == 0) { if (isDigit(cmbStatus.Text) && isDigit(cmbStatus.Text)) { if (IsValidEmail(txtEmail.Text) && isValidRegistrationNumber(txtRegistrationNumber.Text)) { ClassStudent student = new ClassStudent(); student.FirstName = txtFirstName.Text; student.LastName = txtLastName.Text; student.Contact = txtContact.Text; student.Email = txtEmail.Text; student.RegistrationNumber = txtRegistrationNumber.Text; student.Status = Convert.ToInt32(cmbStatus.Text); FirstName = txtFirstName.Text = student.FirstName; LastName = txtLastName.Text = student.LastName; Contact = txtContact.Text = student.Contact; Email = txtEmail.Text = student.Email; RegistrationNumber = txtRegistrationNumber.Text = student.RegistrationNumber; Status = Convert.ToInt32(cmbStatus.Text); conn.Open(); String cmd = String.Format("INSERT INTO Student(FirstName, LastName,Contact, Email, RegistrationNumber, Status) values('{0}','{1}','{2}','{3}','{4}','{5}')", FirstName, LastName, Contact, Email, RegistrationNumber, Status); SqlCommand command = new SqlCommand(cmd, conn); command.Parameters.Add(new SqlParameter("0", 1)); SqlDataReader reader = command.ExecuteReader(); SqlDataAdapter adapter = new SqlDataAdapter(cmd, conn); MessageBox.Show("Student has been added"); txtContact.Text = ""; txtEmail.Text = ""; txtFirstName.Text = ""; txtLastName.Text = ""; txtRegistrationNumber.Text = ""; cmbStatus.Text = ""; conn.Close(); } else { if (!IsValidEmail(txtEmail.Text)) { MessageBox.Show("Invalid Email"); } if (!isValidRegistrationNumber(txtRegistrationNumber.Text)) { MessageBox.Show("Invalid Registration Number"); } } } else { MessageBox.Show("Invalid Status"); if (!IsValidEmail(txtEmail.Text)) { MessageBox.Show("Invalid Email"); } if (!isValidRegistrationNumber(txtRegistrationNumber.Text)) { MessageBox.Show("Invalid Registration Number"); } } } else { MessageBox.Show("Invalid Contact"); if (!IsValidEmail(txtEmail.Text)) { MessageBox.Show("Invalid Email"); } if (!isValidRegistrationNumber(txtRegistrationNumber.Text)) { MessageBox.Show("Invalid Registration Number"); } if (!isDigit(cmbStatus.Text)) { MessageBox.Show("Invalid Status"); } } } else { MessageBox.Show("Invalid Last Name"); if (!isValidContact(txtContact.Text) && txtContact.Text.Length > 0) { MessageBox.Show("Invalid Contact"); } if (!IsValidEmail(txtEmail.Text)) { MessageBox.Show("Invalid Email"); } if (!isValidRegistrationNumber(txtRegistrationNumber.Text)) { MessageBox.Show("Invalid Registration Number"); } if (!isDigit(cmbStatus.Text)) { MessageBox.Show("Invalid Status"); } } } else { MessageBox.Show("Invalid First Name"); if (!IsValidString(txtLastName.Text) && txtLastName.Text.Length > 0) { MessageBox.Show("Invalid Last Name"); } if (!isValidContact(txtContact.Text) && txtContact.Text.Length > 0) { MessageBox.Show("Invalid Contact"); } if (!IsValidEmail(txtEmail.Text)) { MessageBox.Show("Invalid Email"); } if (!isValidRegistrationNumber(txtRegistrationNumber.Text)) { MessageBox.Show("Invalid Registration Number"); } if (!isDigit(cmbStatus.Text)) { MessageBox.Show("Invalid Status"); } } } }