internal ResourceTopic populateTopicFromDB(int id)
        {
            using (ResourcesDataContext db = new ResourcesDataContext())
            {
                ResourceTopic currentTopic = new ResourceTopic();
                var dbTopic = db.bhdResourceTopics.Single((x) => x.id == id);
                currentTopic.topicId = dbTopic.id;
                currentTopic.parentId = dbTopic.parentId;
                currentTopic.name = dbTopic.name;
                currentTopic.description = dbTopic.description;
                currentTopic.active = dbTopic.isActive;
                if (currentTopic.parentId != null)
                {
                    FindParent(currentTopic);
                    currentTopic.isParent = false;
                }
                else
                {
                    currentTopic.isParent = true;
                    currentTopic.phaseId = currentTopic.topicId.Value;
                }

                return currentTopic;
            }
        }
        private void FindParent(ResourceTopic currentTopic)
        {
            bool isParent = !currentTopic.parentId.HasValue;
            if (isParent)
            {
                currentTopic.isSubject = false;
                currentTopic.subjectId = null;
            }
            using (ResourcesDataContext db = new ResourcesDataContext())
            {
                bhdResourceTopic parent = db.bhdResourceTopics.Single((x) => x.id == currentTopic.parentId);
                isParent = !parent.parentId.HasValue;
                if (isParent)
                {
                    currentTopic.isParent = false;
                    currentTopic.isSubject = true;
                    currentTopic.phaseId = parent.id;
                    currentTopic.parentname = parent.name;
                }
                else
                    currentTopic.isSubject = false;

                while (!isParent)
                {
                    
                    isParent = !parent.parentId.HasValue;
                    if (!isParent)
                    {
                        currentTopic.subjectId = parent.id;
                        parent = db.bhdResourceTopics.Single((x) => x.id == parent.parentId);
                    }
                    
                }
                currentTopic.phaseId = parent.id;
            }
        }
        protected void cmdSave_Click(object sender, EventArgs e)
        {
            int? parentId = 0;
            if (!String.IsNullOrEmpty(ddSubject.SelectedValue))
            {
                if (ddSubject.SelectedValue == "0")
                {
                    if (ddParentId.SelectedValue == "0")
                    {
                        parentId = null;
                    }
                    else
                    {
                        parentId = int.Parse(ddParentId.SelectedValue);
                    }
                }
                else
                {
                    parentId = int.Parse(ddSubject.SelectedValue);
                }
            }
            else
                parentId = null;
            ResourceTopic newTopic = new ResourceTopic();
            newTopic.topicId = String.IsNullOrEmpty(hidTopicId.Value) ? 0 : int.Parse(hidTopicId.Value);
            newTopic.parentId = parentId;
            newTopic.name = txtName.Text;
            newTopic.description = txtDesc.Text;
            newTopic.active = chkActive.Checked;

            HttpWebRequest request = WebRequest.Create(dashboardUrlBase + "resourcetopic/") as HttpWebRequest;
            request.ContentType = "text/json";
            request.Method = "PUT";
            try
            {
                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    string json = JsonConvert.SerializeObject(newTopic);
                    streamWriter.Write(json);
                    streamWriter.Flush();
                    streamWriter.Close();
                }

                var httpResponse = (HttpWebResponse)request.GetResponse();
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.ToString());
            }

            hidTopicId.Value = "";
            txtName.Text = "";
            txtDesc.Text = "";
            chkActive.Checked = true;
            divSubject.Visible = false;
            divAddEdit.Visible = false;
            divTopics.Visible = true;
            ddSubject.Items.Clear();
            ddParentId.Items.Clear();
            upAddEdit.Update();

            tvTopics.Nodes.Clear();
            string url = dashboardUrlBase + "resourcetopic";
            WebClient client = new WebClient();
            List<ResourceTopic> result = JsonConvert.DeserializeObject<List<ResourceTopic>>(client.DownloadString(url));
            foreach (ResourceTopic parentTopics in result.Where((x) => x.parentId == null))
            {
                TreeNode parentNode = new TreeNode(parentTopics.name, parentTopics.topicId.ToString());
                AddChildNodes(parentNode, result, int.Parse(parentTopics.topicId.ToString()));
                tvTopics.Nodes.Add(parentNode);
            }

        }