public Message Save(Student newStudent)
        {
            Message message = new Message();
            bool isStudentExists = IsStudentExists(newStudent);

            if (isStudentExists)
            {
                message.Status = "warning";
                message.Details = "Registration Number is already exists.";
                return message;
            }

            int totalRowsAffected = aStudentGateway.AddStudent(newStudent);
            if (totalRowsAffected > 0)
            {
                message.Status = "success";
                message.Details = "Data Saved Successfully";
            }
            else
            {
                message.Status = "error";
                message.Details = "Operation Failed.";
            }

            return message;
        }
        protected void saveButton_Click(object sender, EventArgs e)
        {
            string name = nameTextBox.Text;
            string regNo = regNoTextBox.Text;
            string phone = phoneTextBox.Text;
            string address = addressTextBox.Text;

            Student aStudent = new Student(name, regNo, phone, address);

            bool isStudentExists = IsStudentExists(aStudent);
            if (isStudentExists)
            {
                messageLabel.Text = "Registration Number already exist";
                return;
            }

            SqlConnection connection = new SqlConnection(connectionString);

            connection.Open();
            string query = "INSERT INTO Students(RegNo, Name, Address, Phone) VALUES('" + aStudent.RegNo + "', '" + aStudent.Name + "', '" + aStudent.Address + "', '" + aStudent.Phone + "')";
            SqlCommand command = new SqlCommand(query, connection);
            int totalAffectedRow = command.ExecuteNonQuery();
            connection.Close();
            if (totalAffectedRow > 0)
            {
                messageLabel.Text = "Information Saved";
            }
            else
            {
                messageLabel.Text = "Operation Failed";
            }
        }
        public void Save(Student aStudent)
        {
            string query = "INSERT INTO Students (RegNo, StudentName, StudentEmail, StudentContactNo, StudentAddress, DepartmentId, RegDate) VALUES (@regNo, @name, @email, @contactNo, @address, @deptId, @regDate)";
            using (connection = new SqlConnection(connectionString))
            using (SqlCommand command = new SqlCommand(query, connection))
            {
                command.Parameters.Clear();
                command.Parameters.Add("regNo", sqlDbType: SqlDbType.NVarChar);
                command.Parameters["regNo"].Value = aStudent.RegNo;

                command.Parameters.Add("name", sqlDbType: SqlDbType.NVarChar);
                command.Parameters["name"].Value = aStudent.StudentName;

                command.Parameters.Add("email", sqlDbType: SqlDbType.NVarChar);
                command.Parameters["email"].Value = aStudent.StudentEmail;

                command.Parameters.Add("contactNo", sqlDbType: SqlDbType.NVarChar);
                command.Parameters["contactNo"].Value = aStudent.StudentContactNo;

                command.Parameters.Add("address", sqlDbType: SqlDbType.NVarChar);
                command.Parameters["address"].Value = aStudent.StudentAddress;

                command.Parameters.Add("deptId", sqlDbType: SqlDbType.Int);
                command.Parameters["deptId"].Value = aStudent.DepartmentId;

                command.Parameters.Add("regDate", sqlDbType: SqlDbType.Date);
                command.Parameters["regDate"].Value = aStudent.RegDate.Date;

                connection.Open();
                command.ExecuteNonQuery();
            }
        }
        public ActionResponse Save(Student aStudent)
        {
            ActionResponse response = new ActionResponse();
            try
            {
                bool isEmailExists = aStudentGateway.IsEmailExists(aStudent.StudentEmail);
                if (isEmailExists)
                {
                    response.Class = "danger";
                    response.Message = "Email [" + aStudent.StudentEmail + "] is already exists.";
                    return response;
                }
                //EEE-2016-001
                string deptCode = aDepartmentGateway.GetDepartmentCodeById(aStudent.DepartmentId);
                //int stdId = aStudentGateway.GetMaxStudentId();
                //string regNo = deptCode + "-" + aStudent.RegDate.Year + "-" + stdId.ToString("D3");

                int nextId = aStudentGateway.GetRegSl(aStudent.DepartmentId, aStudent.RegDate.Year);
                string regNo = deptCode + "-" + aStudent.RegDate.Year + "-" + nextId.ToString("D3");
                aStudent.RegNo = regNo;

                aStudentGateway.Save(aStudent);
                response.Class = "success";
                response.Message = regNo;
            }
            catch (SqlException ex)
            {
                response.Class = "warning";
                response.Message = ex.Message;
            }
            return response;
        }
 private bool IsStudentExists(Student student)
 {
     if (GetStudentByRegNo(student.RegNo) != null)
     {
         return true;
     }
     return false;
 }
        public int AddStudent(Student newStudent)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            string sql = "INSERT INTO Students (RegNo, Name, Phone, Address) VALUES ('" + newStudent.RegNo + "', '" + newStudent.Name + "', '" + newStudent.Phone + "', '" + newStudent.Address + "')";
            connection.Open();
            SqlCommand command = new SqlCommand(sql, connection);

            int totalRowsAffected = command.ExecuteNonQuery();
            connection.Close();

            return totalRowsAffected;
        }
        private bool IsStudentExists(Student newStudent)
        {
            StudentGateway aStudentGateway = new StudentGateway();

            Student aStudent = aStudentGateway.GetStudentByRegNo(newStudent.RegNo);

            if (aStudent != null)
            {
                return true;
            }
            return false;
        }
        protected void saveButton_Click(object sender, EventArgs e)
        {
            string regNo = regNoTextBox.Text;
            string name = nameTextBox.Text;
            string phone = phoneTextBox.Text;
            string address = addressTextBox.Text;

            Student aStudent = new Student(regNo, name, phone, address);

            Message message = studentManager.Save(aStudent);
            messageLabel.Text = message.Details;

            LoadStudentList();
        }
        public Message Update(Student aStudent)
        {
            Message message = new Message();
            int totalRowsAffected = aStudentGateway.UpdateStudent(aStudent);
            if (totalRowsAffected > 0)
            {
                message.Status = "success";
                message.Details = "Data Successfully Updated";
            }
            else
            {
                message.Status = "error";
                message.Details = "Operation Failed.";
            }

            return message;
        }
        protected void saveButton_Click(object sender, EventArgs e)
        {
            string regNo = regNoTextBox.Text;
            string name = nameTextBox.Text;
            string phone = phoneTextBox.Text;
            string address = addressTextBox.Text;
            int departmentId = Convert.ToInt32(departmentDropDown.SelectedValue);

            Message message = new Message();
            if (saveButton.Text == "Save")
            {
                Student aStudent = new Student(regNo, name, phone, address);
                aStudent.DepartmentId = departmentId;
                message = studentManager.Save(aStudent);
            }
            else if (saveButton.Text == "Update")
            {
                int id = Convert.ToInt32(pkHiddenField.Value);
                Student aStudent = new Student(id, regNo, name, phone, address);
                aStudent.DepartmentId = departmentId;
                message = studentManager.Update(aStudent);

                saveButton.Text = "Save";
                regNoTextBox.Enabled = true;
            }
            else if (saveButton.Text == "Delete")
            {
                int id = Convert.ToInt32(pkHiddenField.Value);
                message = studentManager.Delete(id);

                regNoTextBox.Enabled = true;
                nameTextBox.Enabled = true;
                phoneTextBox.Enabled = true;
                addressTextBox.Enabled = true;
                saveButton.Text = "Save";
            }

            messageLabel.Text = message.Details;

            ClearTextFields();
            LoadStudentList();
        }
        private Student GetStudentByRegNo(string regNo)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();
            string query = "SELECT * FROM Students WHERE RegNo ='" + regNo + "'";
            SqlCommand command = new SqlCommand(query, connection);
            SqlDataReader reader = command.ExecuteReader();

            Student aStudent = null;
            if (reader.Read())
            {
                aStudent = new Student();

                aStudent.Name = reader["Name"].ToString();
                aStudent.RegNo = reader["RegNo"].ToString();
                aStudent.Address = reader["Address"].ToString();
                aStudent.Phone = reader["Phone"].ToString();
                aStudent.Id = Convert.ToInt32(reader["Id"].ToString());
            }

            return aStudent;
        }
        public Student GetStudentById(int id)
        {
            Student aStudent = null;
            SqlConnection connection = new SqlConnection(connectionString);
            string sql = "SELECT * FROM Students WHERE Id=" + id;
            connection.Open();
            SqlCommand command = new SqlCommand(sql, connection);

            SqlDataReader reader = command.ExecuteReader();

            if (reader.Read())
            {
                aStudent = new Student();
                aStudent.Id = Convert.ToInt32(reader["Id"].ToString());
                aStudent.RegNo = reader["RegNo"].ToString();
                aStudent.Name = reader["Name"].ToString();
                aStudent.Phone = reader["Phone"].ToString();
                aStudent.Address = reader["Address"].ToString();
            }
            connection.Close();

            return aStudent;
        }
        public List<Student> GetAll()
        {
            List<Student> students = new List<Student>();
            SqlConnection connection = new SqlConnection(connectionString);
            string sql = "SELECT * FROM Students";
            connection.Open();
            SqlCommand command = new SqlCommand(sql, connection);

            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                Student aStudent = new Student();
                aStudent.Id = Convert.ToInt32(reader["Id"].ToString());
                aStudent.RegNo = reader["RegNo"].ToString();
                aStudent.Name = reader["Name"].ToString();
                aStudent.Phone = reader["Phone"].ToString();
                aStudent.Address = reader["Address"].ToString();
                students.Add(aStudent);
            }
            connection.Close();

            return students;
        }
 public JsonResult Save(Student aStudent)
 {
     ActionResponse response = aStudentManager.Save(aStudent);
     return Json(response, JsonRequestBehavior.AllowGet);
 }
        public int UpdateStudent(Student aStudent)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            string sql = "UPDATE Students SET RegNo='" + aStudent.RegNo + "', Name='" + aStudent.Name + "', Phone='" + aStudent.Phone + "', Address='" + aStudent.Address + "' WHERE Id=" + aStudent.Id;
            connection.Open();
            SqlCommand command = new SqlCommand(sql, connection);

            int totalRowsAffected = command.ExecuteNonQuery();
            connection.Close();

            return totalRowsAffected;
        }
 private void LoadFieldsBySutdent(Student student)
 {
     pkHiddenField.Value = student.Id.ToString();
     regNoTextBox.Text = student.RegNo;
     nameTextBox.Text = student.Name;
     phoneTextBox.Text = student.Phone;
     addressTextBox.Text = student.Address;
     departmentDropDown.SelectedValue = student.DepartmentId.ToString();
 }