private void saveButton_Click(object sender, EventArgs e)
 {
     Department aDepartment = new Department();
     aDepartment.Name = nameTextBox.Text;
     aDepartment.Code = codeTextBox.Text;
     DepartmentManager aDeptManager = new DepartmentManager();
     string msg = aDeptManager.Save(aDepartment);
     MessageBox.Show(msg);
 }
        public string Save(Department aDepartment)
        {
            if (aDepartment.Name.Length <= 5)
            {
                return "Department name must be 6 (or more) char long";
            }

            if (aDepartmentGateway.Get(aDepartment.Code) != null)
            {
                return "Code of this department already exists in your system";
            }

            return aDepartmentGateway.Save(aDepartment);
        }
        public Department Get(string code)
        {
            string query = "SELECT * FROM t_department WHERE code = '" + code + "'";
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

            if (sqlDataReader.HasRows)
            {
                sqlDataReader.Read();
                Department aDepartment = new Department();
                aDepartment.Name = sqlDataReader[1].ToString();
                aDepartment.Code = sqlDataReader[2].ToString();
                sqlConnection.Close();
                return aDepartment;
            }

            sqlConnection.Close();
            return null;
        }
        public List<Department> GetAll()
        {
            string query = "SELECT * FROM t_department";
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

            List<Department> departments = new List<Department>();
            while(sqlDataReader.Read())
            {
                Department aDepartment = new Department();
                aDepartment.Id = Convert.ToInt32(sqlDataReader[0]);
                aDepartment.Name = sqlDataReader[1].ToString();
                aDepartment.Code = sqlDataReader[2].ToString();
                departments.Add(aDepartment);

            }

            sqlConnection.Close();
            return departments;
        }
 public string Save(Department aDepartment)
 {
     string query = "INSERT INTO t_department(name, code) VALUES ('" + aDepartment.Name + "','" + aDepartment.Code + "')";
     SqlConnection sqlConnection = new SqlConnection(connectionString);
     sqlConnection.Open();
     SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);
     sqlCommand.ExecuteNonQuery();
     sqlConnection.Close();
     return "Department saved";
 }