如何从树视图节点中检索已检查的数据并将其存储到数据库中

时间:2019-07-10 07:46:25

标签: c# asp.net treeview

我的数据库由用于存储问题的调查表和用于存储与各个问题相关的选项的选择表组成。我成功地将它们显示在使用TreeView和CheckBoxes的层次结构中。但无法检索已检查的内容。谁能帮忙。   我尝试了很多方法,但是没有运气。我尝试使用下面的代码仅显示带有值的消息。

 foreach (TreeNode node in treeView1.Nodes)//.CheckedNodes)
            {


                //TreeNodeCollection Child = ;
                MessageBox.Show(node.TreeView.Nodes.ToString());
                if (node.IsSelected)
                    MessageBox.Show(node.Text + " " + node.Tag + "\\n");


                //message += node.Text + " " + node.Tag + "\\n";


            }




private void Load_CloseEnded_Questions()
        {
            treeView1.Nodes.Clear();

            con = new OleDbConnection();
            treeView1.CheckBoxes = true;
            con.ConnectionString = GlobalClass.GlobalVar;
            TreeNode child;
            try
            {
                OleDbCommand cmd = new OleDbCommand();
                OleDbCommand cmd2 = new OleDbCommand();


                cmd.Connection = con;
                cmd2.Connection = con;
                con.Open();

                //SQL Query to retrive questions
                cmd.CommandText = "SELECT q2.questionText, q2.questionId, q2.sequenceNo, q2.questionType FROM questions q2 WHERE q2.sequenceNo <( SELECT q1.sequenceNo FROM questions q1 WHERE q1.questionId = " + QUESTIONID + " )  and q2.questionType = \"Close Ended\" ORDER BY q2.sequenceNo;";
                OleDbDataReader dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        //Store questions from database to Tree Parent nodes
                        TreeNode parent = new TreeNode
                        {
                            Text = dr[0].ToString(),
                            Tag = Convert.ToInt32(dr[1])
                        };

                        //SQL Query to retrive options associated to the question 
                        cmd2.CommandText = "SELECT choices.choiceName, choices.choiceId FROM ((questions INNER JOIN parameter ON questions.parameterId = parameter.parameterId) INNER JOIN choices ON parameter.parameterId = choices.parameterId) WHERE (questions.questionId = " + Convert.ToInt32(dr[1]) + ")";
                        OleDbDataReader dr2 = cmd2.ExecuteReader();

                        if (dr2.HasRows)
                            {
                                while (dr2.Read())
                                {
                                  //Store the options from database to Child Node
                                    child = new TreeNode
                                    {
                                        Text = dr2[0].ToString(),
                                        Tag = Convert.ToInt32(dr[1])
                                    };

                                    TreeNode node1 = new TreeNode();
                                child.Checked = false;
                                parent.Nodes.Add(child);

                             }
                            treeView1.Nodes.Add(parent);
                            dr2.Close();

                        }
                    }
                }
                else
                {
                    MessageBox.Show("No Questions were found.", "STAR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    treeView1.Enabled = false;

                }

                dr.Close();
                con.Close();
                con.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("" + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                con.Dispose();
                con.Close();
            }
        }

I expected to retrive the nodes values(text and tag) those are checked

0 个答案:

没有答案