private void saveCertificates(Student aStudent)
        {
            foreach (Certificate aCertificate in aStudent.Certificates)
            {

                string id = GetExamId(aCertificate.ExaminationType);
                if (id != null)
                {

                    try
                    {
                        connection.Open();
                        string queryString = "insert into t_Certificate values(@grade,@cgpa,@certificateLocation,@examinationId,@registationNO)";
                        command.CommandText = queryString;
                        command.Parameters.Clear();
                        command.Parameters.AddWithValue("@grade", aCertificate.GradeLetter);
                        command.Parameters.AddWithValue("@cgpa", aCertificate.Cgpa);
                        command.Parameters.AddWithValue("@certificateLocation", aCertificate.CertificateLocation);
                        command.Parameters.AddWithValue("@examinationId", id);
                        command.Parameters.AddWithValue("@registationNo", aStudent.RegistationNo);
                        command.ExecuteNonQuery();
                    }

                    finally
                    {
                        connection.Close();
                    }

                }

            }
        }
 public Student GetStudentInformation(string regNo, int depeartmentId)
 {
     try
     {
         Student aStudent = new Student();
         connection.Open();
         string departmentQuery = "select * from t_studentInfo where registationNo=@regNo and departmentId=@depeartmentId";
         command.CommandText = departmentQuery;
         command.Parameters.Clear();
         command.Parameters.AddWithValue("@regNo", regNo);
         command.Parameters.AddWithValue("@depeartmentId", depeartmentId);
         SqlDataReader studentReader = command.ExecuteReader();
         while (studentReader.Read())
         {
             aStudent.RegistationNo = studentReader[0].ToString();
             aStudent.Name = studentReader[1].ToString();
             aStudent.Email = studentReader[2].ToString();
             aStudent.ContactNo = studentReader[3].ToString();
             aStudent.Address = studentReader[4].ToString();
             aStudent.DepartmentId = studentReader[6].ToString();
         }
         return aStudent;
     }
     finally
     {
         connection.Close();
     }
 }
        public int GetStudentRegistationNO(Student aStudent)
        {
            try
            {
                connection.Open();
                int id = 1;
                string queryString = "select registationDate from t_StudentInfo where departmentId=@id";
                command.CommandText = queryString;
                command.Parameters.Clear();
                command.Parameters.AddWithValue("@id", aStudent.DepartmentId);

                SqlDataReader dateReader = command.ExecuteReader();
                while (dateReader.Read())
                {
                    if (Convert.ToDateTime(aStudent.RegistationDate.ToString()).ToString("yyyy") == Convert.ToDateTime(dateReader[0].ToString()).ToString("yyyy"))
                        id++;
                }
                return id;

            }
            finally
            {
                connection.Close();
            }
        }
        public string SaveStudent(Student aStudent)
        {
            try
            {
                connection.Open();
                string addStudentQuery = "insert into t_StudentInfo values(@regNo,@name,@email,@contactNo,@address,@registationDate,@deptId)";
                command.CommandText = addStudentQuery;
                command.Parameters.Clear();
                command.Parameters.AddWithValue("@regNo", aStudent.RegistationNo);
                command.Parameters.AddWithValue("@name", aStudent.Name);
                command.Parameters.AddWithValue("@email", aStudent.Email);
                command.Parameters.AddWithValue("@contactNo", aStudent.ContactNo);
                command.Parameters.AddWithValue("@address", aStudent.Address);
                command.Parameters.AddWithValue("@registationDate", aStudent.RegistationDate);
                command.Parameters.AddWithValue("@deptId", aStudent.DepartmentId);
                command.ExecuteNonQuery();
                return "Saved";
            }

            finally
            {
                connection.Close();
                saveCertificates(aStudent);
            }
        }
        public string GetStudentRegistationNO(Student aStudent)
        {
            string regNo = "";
            int id;
            aStudentGateway = new StudentGateway();
            id = aStudentGateway.GetStudentRegistationNO(aStudent);
            if (id < 10)
                regNo = "000" + id;
            if (id >= 10 && id < 100)
                regNo = "00" + id;
            if (id >= 100 && id < 1000)
                regNo = "0" + id;
            if (id >= 1000)
                regNo = id.ToString();

            return regNo;
        }
        protected void searchButton_Click(object sender, EventArgs e)
        {
            if(!IsValid)
            {
                return;

            }

            try
            {
                string regNo = regNoTextBox.Value;
                int depeartmentId = Convert.ToInt16(departmentDropDownList.Text);
                StudentManager aStudentManager = new StudentManager();
                Student aStudent = new Student();
                aStudent = aStudentManager.GetStudentInformation(regNo, depeartmentId);
                if (aStudent.RegistationNo == null)
                {
                    msgLabel.ForeColor = Color.Red;
                    msgLabel.Text = "Invalid Registation Number";
                }
                nameTextBox.Value = aStudent.Name;
                emailTextBox.Value = aStudent.Email;
            }
            catch (SqlException sqlException)
            {
                msgLabel.ForeColor = Color.Red;
                msgLabel.Text = "Database error.See details error: " + sqlException.Message;

            }
            catch (Exception exception)
            {
                msgLabel.ForeColor = Color.Red;
                string errorMessage = "Unknow error occured.";
                errorMessage += exception.Message;
                if (exception.InnerException != null)
                {
                    errorMessage += exception.InnerException.Message;
                }
                msgLabel.Text = errorMessage;
            }
        }
 private bool DoesThisEmailExist(Student aStudent)
 {
     aStudentGateway = new StudentGateway();
     List<string> emails = new List<string>();
     emails = aStudentGateway.GetStudentEmails();
     foreach (string email in emails)
     {
         if (email == aStudent.Email)
             return true;
     }
     return false;
 }
 public string SaveStudent(Student aStudent)
 {
     aStudentGateway = new StudentGateway();
     if (!DoesThisEmailExist(aStudent))
         return aStudentGateway.SaveStudent(aStudent);
     else
         return "This Email Already Exist";
 }
        protected void Page_Load(object sender, EventArgs e)
        {
            msgLabel.Text = "";
            try
            {
                departmentDropDownList.DataTextField = "DepartmentCode";
                departmentDropDownList.DataValueField = "departmentId";
                examSelectDropDownList.DataTextField = "ExaminationType";
                examSelectDropDownList.DataValueField = "ExaminationId";

                if (!IsPostBack)
                {
                    aStudent = new Student();
                    ViewState["RegistationNo"] = "";
                    ViewState["Certificates"] = certificates;
                    Session["aStudent"] = aStudent;
                    DepartmentManager aDepartmentManager = new DepartmentManager();
                    departments = aDepartmentManager.GetAllDepartments();
                    ExaminationManager anExaminationManager = new ExaminationManager();
                    examinations = anExaminationManager.GetAllExaminations();
                    examSelectDropDownList.DataSource = examinations;
                    examSelectDropDownList.DataBind();
                    departmentDropDownList.DataSource = departments;
                    departmentDropDownList.DataBind();

                }
            }
            catch (SqlException sqlException)
            {
                msgLabel.ForeColor = Color.Red;
                msgLabel.Text = "Database error.See details error: " + sqlException.Message;

            }
            catch (Exception exception)
            {
                msgLabel.ForeColor = Color.Red;
                string errorMessage = "Unknow error occured.";
                errorMessage += exception.Message;
                if (exception.InnerException != null)
                {
                    errorMessage += exception.InnerException.Message;
                }
                msgLabel.Text = errorMessage;
            }
        }
        protected void addIntoListButton_Click(object sender, EventArgs e)
        {
            try
            {
                certificateGridView.DataSource = "";
                if (ViewState["RegistationNo"].ToString() == "")
                {
                    aStudent = GetStudentInfo();
                    Session["aStudent"] = aStudent;
                    ViewState["RegistationNo"] = aStudent.RegistationNo;
                }
                certificates = (List<Certificate>)ViewState["Certificates"];
                Certificate aCertificate = new Certificate();
                aCertificate.ExaminationType = examSelectDropDownList.SelectedItem.Text;
                aCertificate.GradeLetter = gradeLetterTextBox.Value;
                aCertificate.Cgpa = cgpaTextBox.Value;
                if (certificateUpload.HasFile)
                {
                    statusLabel.Text = "";
                    if (!certificateUpload.PostedFile.ContentType.ToLower().StartsWith("image") && certificateUpload.PostedFile.ContentType != "application/pdf")
                    {
                        msgLabel.ForeColor = Color.Red;
                        statusLabel.Text = "Incorrect file type.";
                    }

                    if (certificateUpload.PostedFile.ContentLength > 5120000)
                    {
                        msgLabel.ForeColor = Color.Red;
                        statusLabel.Text = "Image  size is large.";
                    }

                    string newDir = Server.MapPath("~/Certificate/" + ViewState["RegistationNo"].ToString() + "/" + examSelectDropDownList.SelectedItem.Text + "/");
                    MakeDirectoryIfNotExists(newDir);
                    if (ViewState["RegistationNo"].ToString() == "")
                    {
                        msgLabel.ForeColor = Color.Red;
                        statusLabel.Text = "Please Enter correct regNo";
                    }
                    else
                    {
                        string fileName = ViewState["RegistationNo"].ToString() + ".jpg";
                        string savePath = newDir + "/" + fileName;
                        certificateUpload.SaveAs(savePath);
                        aCertificate.CertificateLocation = savePath.ToString();
                        aCertificate.Status = "yes";
                        certificates.Add(aCertificate);
                        ViewState["Certificates"] = certificates;
                        statusLabel.ForeColor = Color.Green;
                        statusLabel.Text = "File Uploaded" + certificates.Count.ToString();
                        certificateGridView.DataSource = certificates;
                        certificateGridView.DataBind();
                    }
                }
                else
                {
                    msgLabel.ForeColor = Color.Red;
                    statusLabel.Text = "File not selected.";
                }
            }
            finally
            {

            }
        }
        private Student GetStudentInfo()
        {
            try
            {
                aStudentManager = new StudentManager();
                aStudent = new Student();
                aStudent.Name = nameText.Value;
                aStudent.Email = emailText.Value;
                aStudent.ContactNo = contactNoText.Value;
                aStudent.DepartmentId = departmentDropDownList.Text;
                aStudent.RegistationDate = dateText.Value;
                string year = aStudentManager.GetYear(dateText.Value);
                aStudent.Address = addressText.Value;
                string id = aStudentManager.GetStudentRegistationNO(aStudent);
                aStudent.RegistationNo = year + departmentDropDownList.SelectedItem.Text + id;
                return aStudent;
            }

            catch (Exception exception)
            {

                throw exception;
            }
        }