Duplicate subNodes Telerik c#

24 Views Asked by At

I am trying to obtain the subnodes of a route, I succeed but duplicates are generated as many times as turns are given to the foreach I think, how could I solve that problem?

        private void AgregarSubnodos(RadTreeNode parentNode, string carpetaPadre)
        {
            try
            {
                // Abrir conexión SQL
                string cadenaConexionSQL = Global.GeneraConexionSQL();
                SqlConnection connection = new SqlConnection(cadenaConexionSQL);
                connection.Open();

                // Obtener las subcarpetas de la carpeta padre
                string[] subCarpetas = Directory.GetDirectories(carpetaPadre);

                foreach (string subCarpeta in subCarpetas)
                {
                    string cadenaSQL = "SELECT carpeta_nodo FROM SEG_NODOS WHERE carpeta_nodo LIKE '" + subCarpeta + "%'";
                    string mensajeVuelta = "";

                    SqlDataReader dataReaderNodos;

                    dataReaderNodos = Global.EjecutaComandoReaderSQL(cadenaConexionSQL, cadenaSQL, ref mensajeVuelta);
                    if (dataReaderNodos.HasRows)
                    {
                        while (dataReaderNodos.Read())
                        {
                            // Get the folder name of the current data reader row
                            string nombreCarpeta = dataReaderNodos.GetString(0);

                            // Create a new node for the subfolder
                            RadTreeNode subNode = new RadTreeNode();
                            subNode.Text = Path.GetFileName(subCarpeta);
                            subNode.Tag = subCarpeta;

                            // Node source
                            subNode.Font = new System.Drawing.Font("Segoe UI", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

                            // Add the node as a subnode of the parent node
                            parentNode.Nodes.Add(subNode);

                            // Recursively add subnodes for current subfolder
                            AgregarSubnodos(subNode, subCarpeta);

                        }
                    }
                    dataReaderNodos.Close();
                }
                connection.Close();
            }
            catch (UnauthorizedAccessException)
            {
            }
            catch (Exception ex)
            {
                Global.SuperLog("Error al agregar subnodos: " + ex.Message);
            }
        }

this is what you see in the tree, the nodes are duplicated

Result on the app

0

There are 0 best solutions below