//========================================================================================== //==================================Additional functions==================================== //========================================================================================== //Load all topic from sql server public void loadTopicList() { topicList.Clear(); lbTopic.Items.Clear(); topicIDList.Clear(); BOTopic bot = new BOTopic(); topicList = bot.ListAll(); for (int i = 0; i < topicList.Count; i++) { lbTopic.Items.Add(topicList[i]); topicIDList.Add(topicList[i].TopicID); } lbTopic.Sorted = true; }
//==================================Additional functions==================================== //========================================================================================== //Load data from sql server public void loadData() { //Load topic BOTopic bot = new BOTopic(); topicList = bot.ListAll(); cbbTopic.DataSource = topicList; //Load questions and answers BOQuestion boq = new BOQuestion(); BOAnswer boa = new BOAnswer(); answerList = boa.ListAll(); questionList = boq.ListAll(); for (int i = 0; i < questionList.Count; i++) { questionList[i].Answers = new List<Answer>(); for (int j = 0; j < answerList.Count; j++) if (answerList[j].QCode == questionList[i].Code) { questionList[i].Answers.Add(answerList[j]); } } }
//========================================================================================== //==================================Button functions======================================== //========================================================================================== private void btAdd_Click(object sender, EventArgs e) { //validate inputs if (topicIDList.Contains(txtTopicID.Text)==false) { //create Topic object Topic t = new Topic(); t.TopicID = this.txtTopicID.Text; t.TopicName = this.txtTopicName.Text; //insert topic object into Topic table BOTopic bot = new BOTopic(); bool success = bot.Add(t); if (success) { MessageBox.Show("Add topic success!"); loadTopicList(); } else MessageBox.Show("Failed to add a topic!"); txtTopicID.Clear(); txtTopicName.Clear(); } else MessageBox.Show("The topic code is exist"); }
private void btUpdate_Click(object sender, EventArgs e) { int i = lbTopic.SelectedIndex; if (i == -1) { MessageBox.Show("Select a topic to change"); return; } else { string oldTopicID = topicList[i].TopicID; BOTopic bot = new BOTopic(); Topic t = new Topic(txtTopicID.Text, txtTopicName.Text); bool success =bot.Update(t, oldTopicID); if (success) { MessageBox.Show("Update topic success!"); loadTopicList(); } else MessageBox.Show("Failed to update a topic!"); txtTopicID.Clear(); txtTopicName.Clear(); btUpdate.Enabled = false; btDelete.Enabled = false; loadTopicList(); } }