private DataTable FillTasks()
    {
        BCCTask task = new BCCTask();
        task.TaskVisible = 1;
        // Get me all the active tasks.

        DataTable dtTasks = null;

        if (ddlOptions.SelectedValue.Equals("1"))
        {
            task.AssignedToUser = HttpContext.Current.User.Identity.Name;
            task.TaskStatus = string.Empty;
            dtTasks = BCCTask.RetrieveTasks(task);
        }
        else if (ddlOptions.SelectedValue.Equals("2")) // All Tasks
        {
            task.TaskStatus = string.Empty;
            dtTasks = BCCTask.RetrieveTasks(task);
        }
        else
        {
            task.TaskStatus = BCCTask.TaskState.Completed.ToString();
            dtTasks = BCCTask.RetrieveTasks(task);
        }

        if (dtTasks != null)
        {
            taskView.DataSource = dtTasks;
            taskView.DataBind();
            taskView.Visible = true;
        }

        DataBindChart();

        return dtTasks;
    }
    protected void archivedTasks_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Archive")
        {
            GridViewRow clickedRow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

            HiddenField hTaskID = clickedRow.FindControl("hTaskID") as HiddenField;

            if (hTaskID != null)
            {
                Guid taskGuid = new Guid(hTaskID.Value);

                //Clone the task
                BCCTask task = new BCCTask();
                task.TaskID = taskGuid;

                // Hard delete
                BCCTask.DeactivateTask(task, true);
            }

            FillArchivedTasks();
        }
    }
    protected void taskView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow updatableRow = taskView.Rows[e.RowIndex] as GridViewRow;

        if (updatableRow != null)
        {
            HiddenField htaskID = updatableRow.FindControl("taskID") as HiddenField;
            HiddenField taskRef = updatableRow.FindControl("taskRef") as HiddenField;

            DropDownList ddlPriority = updatableRow.FindControl("ddlPriority") as DropDownList;
            DropDownList ddlProjectName = updatableRow.FindControl("ddlProjectName") as DropDownList;
            DropDownList ddlTaskType = updatableRow.FindControl("ddlTaskType") as DropDownList;
            DropDownList ddlUsers = updatableRow.FindControl("ddlUsers") as DropDownList;

            TextBox tTaskDueDate = updatableRow.FindControl("tTaskDueDate") as TextBox;
            TextBox tTaskTitle = updatableRow.FindControl("tTaskTitle") as TextBox;

            if (htaskID != null)
            {
                BCCTask task = new BCCTask();

                Guid taskGuid = new Guid(htaskID.Value);
                task.TaskID = taskGuid;
                task.TaskRef = taskRef.Value;
                task.TaskTitle = tTaskTitle.Text;
                task.AssignedToUser = ddlUsers.SelectedValue;

                int priority = 1;
                Int32.TryParse(ddlPriority.SelectedValue, out priority);
                task.TaskPriority = priority;

                task.TaskDueDate = DateTime.Parse(tTaskDueDate.Text);
                task.TaskType = ddlTaskType.SelectedValue;
                task.TaskProjectName = ddlProjectName.SelectedValue;
                task.UpdatedbyUser = HttpContext.Current.User.Identity.Name;

                BCCTaskSystemMessage message = BCCTask.VerifyTask(task);

                if (message != null)
                {
                    DisplayError(message.ErrorMessage);
                }
                else
                {
                    BCCTask.UpdateTask(task);
                    DisplayInfo( string.Format("Task #{0} was updated", taskRef.Value));
                }
            }
        }

        // Refresh the data
        taskView.EditIndex = -1;
        FillTasks();
        DataBindChart();
    }
    private void FillArchivedTasks()
    {
        BCCTask task = new BCCTask();
        task.TaskVisible = 0;
        // Get me all the active tasks.
        DataTable dtTasks = BCCTask.RetrieveAllTasks(task);

        if (dtTasks != null)
        {
            archivedTasks.DataSource = dtTasks;
            archivedTasks.DataBind();
            archivedTasks.Visible = true;
        }
    }
    protected void taskView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Add"))
        {
            TextBox tNewProjectName = taskView.FooterRow.FindControl("tNewTaskTitle") as TextBox;
            DropDownList ddlNewProjectName = taskView.FooterRow.FindControl("ddlNewProjectName") as DropDownList;
            DropDownList ddlNewTaskType = taskView.FooterRow.FindControl("ddlNewTaskType") as DropDownList;

            if (tNewProjectName != null && ddlNewProjectName != null && ddlNewTaskType != null)
            {
                BCCTask task = new BCCTask();

                task.TaskID = System.Guid.NewGuid();
                task.TaskTitle = tNewProjectName.Text;
                task.TaskProjectName = ddlNewProjectName.SelectedValue;
                task.TaskCreatedDate = DateTime.Now;
                // 7 days from today!
                task.TaskDueDate = DateTime.Today.AddDays(7);
                task.TaskDescription = string.Empty;
                task.TaskType = ddlNewTaskType.SelectedValue;
                task.CreatedbyUser = HttpContext.Current.User.Identity.Name;
                task.AssignedToUser = HttpContext.Current.User.Identity.Name;

                BCCTask.CreateTask(task);
                DisplayInfo("New task created successfully");
            }

            FillTasks();
        }
        else if (e.CommandName.Equals("AddOnEmpty"))
        {
            TextBox tNewTaskName = taskView.Controls[0].Controls[0].FindControl("tNewTaskName") as TextBox;
            DropDownList ddlPriority = taskView.Controls[0].Controls[0].FindControl("ddlPriority") as DropDownList;
            DropDownList ddlNewProjectName = taskView.Controls[0].Controls[0].FindControl("ddlNewProjectName") as DropDownList;
            DropDownList ddlNewTaskType = taskView.Controls[0].Controls[0].FindControl("ddlNewTaskType") as DropDownList;

            if (tNewTaskName != null && ddlPriority != null && ddlNewProjectName != null && ddlNewTaskType != null)
            {
                BCCTask task = new BCCTask();

                task.TaskID = System.Guid.NewGuid();
                task.TaskTitle = tNewTaskName.Text;
                task.TaskProjectName = ddlNewProjectName.SelectedValue;
                task.TaskCreatedDate = DateTime.Now;
                // 7 days from today!
                task.TaskDueDate = DateTime.Today.AddDays(7);
                task.TaskDescription = string.Empty;
                task.TaskType = ddlNewTaskType.SelectedValue;
                task.CreatedbyUser = HttpContext.Current.User.Identity.Name;
                task.AssignedToUser = HttpContext.Current.User.Identity.Name;

                if (!(task.TaskProjectName.Equals(string.Empty)))
                {
                    BCCTask.CreateTask(task);
                    DisplayInfo("New task created successfully");
                }
                else
                {
                    DisplayError("You must create a Project before you can create a task");
                }
            }

            FillTasks();
        }
        else if (e.CommandName.Equals("TaskComplete"))
        {
            GridViewRow clickedRow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

            HiddenField hGuid = clickedRow.FindControl("taskID") as HiddenField;
            HiddenField taskRef = clickedRow.FindControl("taskRef") as HiddenField;
            Label lblAssignedToUser = clickedRow.FindControl("lblAssignedToUser") as Label;

            if (hGuid != null && taskRef != null && lblAssignedToUser != null)
            {
                Guid taskGuid = new Guid(hGuid.Value);

                BCCTask task = new BCCTask();

                task.TaskID = taskGuid;
                task.TaskRef = taskRef.Value;
                task.TaskStatus = Enum.GetName(typeof(BCCTask.TaskState), BCCTask.TaskState.Completed);
                task.UpdatedbyUser = HttpContext.Current.User.Identity.Name;

                if (lblAssignedToUser.Text.Equals(BCCUIHelper.Constants.NOT_ASSIGNED))
                {
                    task.AssignedToUser = string.Empty;
                }
                else
                {
                    task.AssignedToUser = lblAssignedToUser.Text;
                }

                BCCTaskSystemMessage message = BCCTask.VerifyTask(task);

                if (message != null)
                {
                    DisplayError(message.ErrorMessage);
                }
                else
                {
                    BCCTask.UpdateTaskStatus(task);
                }
            }

            FillTasks();
        }
    }
    protected void taskView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow deletedRow = taskView.Rows[e.RowIndex] as GridViewRow;

        if (deletedRow != null)
        {
            HiddenField hGuid = deletedRow.FindControl("taskID") as HiddenField;
            Guid taskGuid = new Guid(hGuid.Value);
            BCCTask task = new BCCTask();
            task.TaskID = taskGuid;
            task.UpdatedbyUser = HttpContext.Current.User.Identity.Name;
            // Just do a soft delete
            BCCTask.DeactivateTask(task, false);
        }

        // Refresh the data
        taskView.EditIndex = -1;
        FillTasks();
        FillArchivedTasks();
    }
        public static void UpdateTaskStatus(BCCTask task)
        {
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["authStore"].ConnectionString))
            {
                SqlCommand command = new SqlCommand("[dbo].[bcc_TaskList_UpdateTaskStatus]", connection);
                command.CommandType = CommandType.StoredProcedure;

                SqlParameter param = new SqlParameter("@taskID", task.TaskID);
                command.Parameters.Add(param);

                param = new SqlParameter("@taskStatus", task.TaskStatus);
                command.Parameters.Add(param);

                param = new SqlParameter("@updatedbyUser", task.UpdatedbyUser);
                command.Parameters.Add(param);

                connection.Open();
                command.ExecuteNonQuery();
            }
        }
        public static BCCTaskSystemMessage VerifyTask(BCCTask task)
        {
            BCCTaskSystemMessage systemMessage = null;
            string errorMessage = string.Empty;

            if (task.TaskStatus == BCCTask.TaskState.Completed.ToString() && task.AssignedToUser == string.Empty)
            {
                errorMessage = string.Format("Un-assigned task #{0} cannot be set to Completed", task.TaskRef);
                systemMessage = new BCCTaskSystemMessage("101", errorMessage);
            }
            else
                if (task.TaskStatus == BCCTask.TaskState.Completed.ToString()
                    && task.AssignedToUser != string.Empty
                    && task.TaskDuration == 0)
                {
                    errorMessage = string.Format("The number of hours spent for the task #{0} must be specified before setting it to Completed", task.TaskRef);
                    systemMessage = new BCCTaskSystemMessage("100", errorMessage);
                }

            return systemMessage;
        }
        /// <summary>
        /// Create a Task
        /// </summary>
        /// <param name="task">BCC Task</param>
        ///// 		@taskID uniqueIdentifier,
        //@taskTitle    nvarchar(50),
        //@taskDescription nvarchar(max),
        //@taskSpeedcode int,
        //@taskPriority int,
        //@updatedbyUser nvarchar(50),
        //@taskDueDate datetime,
        //@taskType nvarchar(50),
        //@taskDuration int,
        //@taskStatus nvarchar(50),
        //@projectName nvarchar(50),
        //@showFlag bit
        /// <returns>DataTable</returns>
        public static void UpdateTask(BCCTask task)
        {
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["authStore"].ConnectionString))
            {
                SqlCommand command = new SqlCommand("[dbo].[bcc_TaskList_UpdateTask]", connection);
                command.CommandType = CommandType.StoredProcedure;

                SqlParameter param = new SqlParameter("@taskID", task.TaskID);
                command.Parameters.Add(param);

                param = new SqlParameter("@taskTitle", task.TaskTitle);
                command.Parameters.Add(param);

                param = new SqlParameter("@taskDescription", task.TaskDescription);
                command.Parameters.Add(param);

                param = new SqlParameter("@taskSpeedCode", task.TaskSpeedCode);
                command.Parameters.Add(param);

                param = new SqlParameter("@taskPriority", task.TaskPriority);
                command.Parameters.Add(param);

                param = new SqlParameter("@updatedbyUser", task.UpdatedbyUser);
                command.Parameters.Add(param);

                //@assignedToUser
                param = new SqlParameter("@assignedToUser", task.AssignedToUser);
                command.Parameters.Add(param);

                param = new SqlParameter("@taskDueDate", task.TaskDueDate);
                command.Parameters.Add(param);

                param = new SqlParameter("@taskType", task.TaskType);
                command.Parameters.Add(param);

                //param = new SqlParameter("@taskStatus", task.TaskStatus);
                //command.Parameters.Add(param);

                param = new SqlParameter("@projectName", task.TaskProjectName);
                command.Parameters.Add(param);

                param = new SqlParameter("@showFlag", task.TaskVisible);
                command.Parameters.Add(param);

                connection.Open();

                command.ExecuteNonQuery();
            }
        }
        /// <summary>
        /// Retrieve All Tasks
        /// </summary>
        /// <param name="task">BCC Task</param>
        /// <returns>DataTable</returns>
        public static DataTable RetrieveTasks(BCCTask task)
        {
            DataTable dt = null;
            SqlParameter param = null;
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["authStore"].ConnectionString))
            {
                SqlCommand command = new SqlCommand("[dbo].[bcc_TaskList_Tasks]", connection);
                command.CommandType = CommandType.StoredProcedure;

                if (task.AssignedToUser == string.Empty)
                {
                    param = new SqlParameter("@assignedToUserName", DBNull.Value);
                }
                else
                {
                    param = new SqlParameter("@assignedToUserName", task.AssignedToUser);
                }

                command.Parameters.Add(param);

                if (task.TaskStatus == string.Empty)
                {
                    param = new SqlParameter("@taskStatus", DBNull.Value);
                }
                else
                {
                    param = new SqlParameter("@taskStatus", task.TaskStatus);
                }

                command.Parameters.Add(param);

                param = new SqlParameter("@showFlag", task.TaskVisible);
                command.Parameters.Add(param);

                connection.Open();

                SqlDataReader reader = command.ExecuteReader();

                dt = new DataTable();
                dt.Load(reader);

                reader.Close();
            }

            return dt;
        }
        /// <summary>
        /// Retrieve All Tasks whatever the case may be.
        /// </summary>
        /// <param name="task">BCC Task</param>
        /// <returns>DataTable</returns>
        public static DataTable RetrieveAllTasks(BCCTask task)
        {
            DataTable dt = null;

            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["authStore"].ConnectionString))
            {
                SqlCommand command = new SqlCommand("[dbo].[bcc_TaskList_AllTasks]", connection);
                command.CommandType = CommandType.StoredProcedure;

                SqlParameter param = new SqlParameter("@showFlag", task.TaskVisible);
                command.Parameters.Add(param);

                connection.Open();

                SqlDataReader reader = command.ExecuteReader();

                dt = new DataTable();
                dt.Load(reader);

                reader.Close();
            }

            return dt;
        }
        public static void DeactivateTask(BCCTask task, bool hardDelete)
        {
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["authStore"].ConnectionString))
            {
                SqlCommand command = new SqlCommand("[dbo].[bcc_TaskList_DeactivateTask]", connection);
                command.CommandType = CommandType.StoredProcedure;

                SqlParameter param = new SqlParameter("@taskID", task.TaskID);
                command.Parameters.Add(param);

                param = new SqlParameter("@updatedbyUser", task.UpdatedbyUser);
                command.Parameters.Add(param);

                int flag = 0;

                if (hardDelete)
                {
                    flag = 1;
                }

                param = new SqlParameter("@deleteFlag", flag);
                command.Parameters.Add(param);

                connection.Open();
                command.ExecuteNonQuery();
            }
        }
        /// <summary>
        /// Create a Task
        /// </summary>
        /// <param name="task">BCC Task</param>
        /// <returns>DataTable</returns>
        public static DataTable CreateTask(BCCTask task)
        {
            DataTable dt = null;

            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["authStore"].ConnectionString))
            {
                SqlCommand command = new SqlCommand("[dbo].[bcc_TaskList_CreateTask]", connection);
                command.CommandType = CommandType.StoredProcedure;

                SqlParameter param = new SqlParameter("taskTitle", task.TaskTitle);
                command.Parameters.Add(param);

                param = new SqlParameter("@taskDescription", task.TaskDescription);
                command.Parameters.Add(param);

                param = new SqlParameter("@taskSpeedCode", task.TaskSpeedCode);
                command.Parameters.Add(param);

                param = new SqlParameter("@taskPriority", task.TaskPriority);
                command.Parameters.Add(param);

                param = new SqlParameter("@createdbyUser", task.CreatedbyUser);
                command.Parameters.Add(param);

                //@assignedToUser
                param = new SqlParameter("@assignedToUser", task.AssignedToUser);
                command.Parameters.Add(param);

                param = new SqlParameter("@taskDueDate", task.TaskDueDate);
                command.Parameters.Add(param);

                param = new SqlParameter("@taskType", task.TaskType);
                command.Parameters.Add(param);

                param = new SqlParameter("@taskDuration", task.TaskDuration);
                command.Parameters.Add(param);

                param = new SqlParameter("@projectName", task.TaskProjectName);
                command.Parameters.Add(param);

                //@taskId
                param = new SqlParameter("@taskID", System.Guid.Empty);
                param.Direction = ParameterDirection.Output;
                command.Parameters.Add(param);

                connection.Open();

                SqlDataReader reader = command.ExecuteReader();

                dt = new DataTable();
                dt.Load(reader);

                reader.Close();
            }

            return dt;
        }