|
Ok, here's what I'm doing now:
private DataTable GenDT(string strSelect)
{
if (con.State != ConnectionState.Open)
con.Open();
DataTable _dt = new DataTable();
try
{
new OracleDataAdapter(strSelect, con).Fill(_dt);
}
catch (Exception) { }
return _dt;
}
private void pop_tree()
{
DataTable dt = GenDT("SELECT ID,PARENT,NAME FROM TT ORDER BY ID");
bool firstIteration = true;
Node baseLine = null;
foreach (DataRow row in dt.Rows)
{
if (firstIteration)
{
baseLine = new Node(int.Parse(row["Id"].ToString()),
row["Name"].ToString());
firstIteration = false;
}
else
{
int parentId = int.Parse(row["Parent"].ToString());
Node parent = (baseLine.Id == parentId) ? baseLine : baseLine.FindNode(parentId);
parent.AddChild(new Node(int.Parse(row["Id"].ToString()),
row["Name"].ToString()));
}
TreeNodeBuilder nodeBuilder = new TreeNodeBuilder(baseLine);
treeView1.Nodes.Add(nodeBuilder.BuildTreeNode());
}
}
private void Form1_Load(object sender, EventArgs e)
{
pop_tree();
}
And I'm getting multiple baseline nodes.. I guess I'm placing your lines in the wrong place of the loop right??
|
|
|
|
|
Just have to move the last two lines outside the loop
You know what?? I love you
5 for all your posts and articles!!
You're one of the most most valuable MVPs and I'm going to didicate my next article to your honor
Thank you Navaneeth.. Your the best!!
|
|
|
|
|
Hey,
I'm sorry for the headache but when adding to A1 or A2 a sub node like:
ID Parent Name
------------------------
0 0 BaseLine
1 0 A
2 1 A1
3 1 A2
4 0 B
5 4 B1
6 4 B2
7 2 A1.1
8 2 A1.2
I get null reference exception here:
parent.AddChild(new Node(int.Parse(row["Id"].ToString()),
row["Name"].ToString()));
Please help
modified on Monday, August 10, 2009 2:43 PM
|
|
|
|
|
Good catch! FindNode method in the Node class was the culprit. It was greedy and not stopping the iteration even it got a successful match. I made the algorithm lazy and it works fine now. Here is the changed FindNode method.
public Node FindNode(int id)
{
Node result = childNodes.SingleOrDefault(node => node.Id == id);
if (result == null)
{
foreach (Node node in childNodes)
{
result = node.FindNode(id);
if (result != null)
break;
}
}
return result;
} Lines in bold are the changed part.
As a best practice, you may need to add sanity checks like checking the parent exist before adding a child. Also do NULL checking before methods are called. Only thing you need to take care is to ensure the parent exist before a child is added.
Thanks for the kind words. All the best
|
|
|
|
|
|
|
|
Your code is not compiling at my end. The third party control you have used is not available on my machine. Oracle is not available as well. I am using LINUX host running windows as a guest OS on a VM. So I don't have much things installed. I'd recommend to try my code first with a normal TreeView . Once you have done that and got it working, using it with your third party tree view will be trivial.
You are so kind to recommend me to MVP program. Thanks
|
|
|
|
|
Hey there Nav,
I've reached a blocked road "again".. You see, in your code bellow, can we add nodes directly to the treeview control instead of building a treenode first..
Please Nav, this's the last of it.. Once done it will solve my problem with the control.. I'm also thinking of writing an article with your member ID included so it can be from both of us, what do you think??
private void pop_GTree()
{
DataTable dt = GenDT("SELECT ID,PARENT,NAME FROM TT ORDER BY ID");
bool firstIteration = true;
Node baseLine = null;
foreach (DataRow row in dt.Rows)
{
if (firstIteration)
{
baseLine = new Node(int.Parse(row["ID"].ToString()), row["NAME"].ToString());
treeView1.Nodes.Add(row["ID"].ToString(), row["NAME"].ToString());
firstIteration = false;
}
else
{
int parentId = int.Parse(row["PARENT"].ToString());
Node parent = (baseLine.Id == parentId) ? baseLine : baseLine.FindNode(parentId);
parent.AddChild(new Node(int.Parse(row["ID"].ToString()),
row["NAME"].ToString()));
treeGridView1.Nodes.Add(parent);
treeView1.Nodes[baseLine.Id].Nodes.Add(row["ID"].ToString(),
row["NAME"].ToString());
}
}
}
|
|
|
|
|
Muammar© wrote: treeView1.Nodes.Add
This method creates a TreeNode internally. So it makes no difference.
|
|
|
|
|
Sorry Nav, I know this's becoming a headache but please take a look at this:
private void pop_GTree()
{
DataTable dt = GenDT("SELECT ID,PARENT,NAME FROM TT ORDER BY ID");
bool firstIteration = true;
Node baseLine = null;
foreach (DataRow row in dt.Rows)
{
if (firstIteration)
{
baseLine = new Node(int.Parse(row["ID"].ToString()), row["NAME"].ToString());
treeView1.Nodes.Add(row["ID"].ToString(), row["NAME"].ToString());
firstIteration = false;
}
else
{
int parentId = int.Parse(row["PARENT"].ToString());
Node parent = (baseLine.Id == parentId) ? baseLine : baseLine.FindNode(parentId);
parent.AddChild(new Node(int.Parse(row["ID"].ToString()), row["NAME"].ToString()));
treeView1.Nodes[baseLine.FindNode(parent.Id).Id].Nodes.Add(row["ID"].ToString(), row["NAME"].ToString());
}
}
TreeNodeBuilder nodeBuilder = new TreeNodeBuilder(baseLine);
}
I'm sorry but I'm totally messed up and I find it difficult to modify your code.. I just need to build it in a treeview directly instead of a seperate node and finally add it and no it's not the same as the control i'm using doesnt support the addition of a treenode with sub nodes, it will just show the first node "the baseline node" while if it's possible to add the nodes one by one in the loop directly to the tree, it will work.. I'll thankful if you can help on this as well dear friend.
|
|
|
|
|
Ok, I think I'm getting somewhere..
After the creation of your treeNode object, we dive in it again in a loop to fetch a node and add it to its parent in the tree.. However, I'm still missing something, please take a look..
foreach (DataRow row in dt.Rows)
{
int parentId = int.Parse(row["PARENT"].ToString());
treeView1.Nodes[baseLine.FindNode(parentId).Id].Nodes.Add(row["ID"].ToString(), row["NAME"].ToString());
}
|
|
|
|
|
Hey there Nav,
Just came up with another way to do it and thought you should know...
private DataTable GenDT(string strSelect)
{
if (con.State != ConnectionState.Open)
con.Open();
DataTable _dt = new DataTable();
try
{
new OracleDataAdapter(strSelect, con).Fill(_dt);
}
catch (Exception) { }
return _dt;
}
private void LoadChilds(string _Parent, TreeNode _Node)
{
DataTable dt = GenDT("SELECT ID,PARENT,NAME FROM TT WHERE PARENT ="+_Parent+" ORDER BY ID");
for (int x = 0; x < dt.Rows.Count;x++ )
{
_Node.Nodes.Add(dt.Rows[x]["ID"].ToString(), dt.Rows[x]["NAME"].ToString());
LoadChilds(dt.Rows[x]["ID"].ToString(), _Node.Nodes[x]);
}
}
private void pop_GTree()
{
DataTable dt;
dt = GenDT("SELECT ID,PARENT,NAME FROM TT WHERE PARENT = 0 ORDER BY ID");
foreach (DataRow row in dt.Rows)
treeView1.Nodes.Add(row["ID"].ToString(), row["NAME"].ToString());
foreach(TreeNode tn in treeView1.Nodes)
LoadChilds(tn.Name, tn);
}
Haven't put it through a heavy test but it looks fine.. Can you please take a look with your sharp logic and let me know if there's any potential run-time problem in the LoadChilds method?? Thank you Nav, and what about the co-article thing?? Are you in?? I suppose we write about this together and I manage the control part and loading the tree using this way, and you handle the linq part.. what do you say?? CPians love co-articles
|
|
|
|
|
This looks OK other than it is requesting each time to database. If you can afford that, this will work. Other alternative is to load the whole data into a DataTable first, and LoadChilds method look into this DataTable instead of querying database. DataTable has a Select() method which can be useful here.
Muammar© wrote: and what about the co-article thing??
I appreciate your offer. But right now I am in a hurry and getting my things packed as I am traveling to US for an official trip. So I guess my schedule in the coming days will be busy and probably not even gets time to check mail.
Also, this is a basic concept and CPians will have pretty good idea about this. Not sure that article will be clicked. You can try to post it as a blog. I found this[^] article to be quite related to the tree view concept. It has got a similar table structure.
|
|
|
|
|
Hey Nav,
N a v a n e e t h wrote: DataTable has a Select() method
It's a stand alone database and yet I'd better adopt that, Thank you for your valuable comment.
N a v a n e e t h wrote: I am traveling to US
Do you work for the united nations?? Cuz I do.. Anyways, have a safe flight, enjoy being there and good luck on your mission!
About the article, yes it has the same data structure but you cant find one similar article in windows forms but anyways, I'll let you know when my part is ready and email it to you and if you're interested/have the time by then, you can simply add your part "mainly the linq part"..
Nice to meet you Nav.. I really appreciate all the time you spent on this thread and I have to say, I learn a lot from you.. Thank you!
Truly Yours,
Muammar.
|
|
|
|
|
Hello,
VS 2008 SP1
I am using the DownloadDataAysnc. But the ProgressChanged event doesn't show progress until after the data has been downloaded.
Even when I try and download a data which is contained in a big file. The programs remains responsive so I know it is doing something. However, it is when the progress has completed that the progressChanged event fires.
I known this as the progressChanged and the DownloadDataCompleted fire immediately after each other. However, they should be a pause as the file is quite big.
This is the code snippet I am currently using. And the output below. What is strange the e.progresspercentage is 100%. And seems to get called twice.
Many thanks for any advise,
Results:
Progress changed Version userstate: [ Version1 ]
progressBar1.Value [ 100 ]
Progress changed Version userstate: [ Version1 ]
progressBar1.Value [ 100 ]
Completed data: [ 1.0.11 ]
private void UpdateAvailable()
{
WebClient wbCheckUpdates = new WebClient();
wbCheckUpdates.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wbCheckUpdates_DownloadProgressChanged);
wbCheckUpdates.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wbCheckUpdates_DownloadDataCompleted);
DownloadFiles df = new DownloadFiles();
string webServerURL = df.webServerPath;
wbCheckUpdates.DownloadDataAsync(new Uri(Path.Combine(webServerURL, "version.txt")), "Version1");
}
void wbCheckUpdates_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine("Progress version changed userstate: [ " + e.UserState + " ]");
progressBar1.Value = e.ProgressPercentage;
Console.WriteLine("progressBar1.Value [ " + this.progressBar1.Value + " ]");
}
void wbCheckUpdates_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
byte[] result = e.Result;
Console.WriteLine("Completed data: [ " + System.Text.ASCIIEncoding.Default.GetString(result) + " ]");
}
|
|
|
|
|
steve_rm wrote: But the ProgressChanged event doesn't show progress until after the data has been downloaded.
Yes, this because of UI thread is different from you background thread.
You have to use Background Worker for resolve is Issue. Where you can so Progressbar continuing along with dowloading.
Here is an sample example of Background worker,
Using the BackgroundWorker Component in .NET 2 applications
Hope this will help you
|
|
|
|
|
Hello,
Thanks for the reply.
According to this msdn.
http://msdn.microsoft.com/en-us/library/system.net.webclient.downloadprogresschanged.aspx
The DownloadDataAysnc does show progress.
Not sure why mine doesn't.
Many thanks for any other suggestions,
|
|
|
|
|
Are you by any chance downloading from a passive FTP-server? Just found this in the documentation;
If the server does not send the size of the downloaded file (such as in the case of a passive FTP connection), ProgressPercentage may always be zero.
What happens if you put a breakpoint in the DownloadProgressChangedEventHandler ? Does the break get hit during download?
I are Troll
|
|
|
|
|
Hello,
I am downloading from a http web server using IIS;
Thanks,
|
|
|
|
|
Can you verify (using a new project, just for testing) whether this adapted code works as expected?
static void UpdateAvailable()
{
WebClient wbCheckUpdates = new WebClient();
wbCheckUpdates.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wbCheckUpdates_DownloadProgressChanged);
wbCheckUpdates.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wbCheckUpdates_DownloadDataCompleted);
string webServerURL =
@"http://download.wikimedia.org/simplewiktionary/20090801/simplewiktionary-20090801-pages-articles.xml.bz2";
wbCheckUpdates.DownloadDataAsync(new Uri(webServerURL));
}
static void wbCheckUpdates_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine("Download progres: " + e.ProgressPercentage);
}
static void wbCheckUpdates_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
Console.WriteLine("Completed");
}
static void Main(string[] args)
{
UpdateAvailable();
Console.ReadKey();
}
"The human mind ordinarily operates at only ten 10% of its capacity, the rest is overhead for the operating system"
|
|
|
|
|
Hi,
what Abhijit Jana told you is correct.
Steve1_rm wrote: The DownloadDataAysnc does show progress.
No. What the MSDN page says is "(This event) occurs when an asynchronous download operation successfully transfers some or all of the data." So it tells you some progress has been made, it does however run on some thread (most probably a ThreadPool thread) different from the main thread, and hence it is not allowed to touch any Control.
You may want to read some of my articles:
- http://www.perceler.com/articles1.php?art=asyncpool1[^]
- http://www.perceler.com/articles1.php?art=crossthreads1[^]
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Hi,
I've got a few comments on the code although it does look essentially correct and I can't see anything that is obviously broken.
1) In the completion handler there is no check for success and without that it is not possible to draw any conclusions about other parts of the operation.
2) In the example given the results string is very short, only 6 characters, and so it is quite possible that the first progress notification is 100%. Log the e.BytesReceived and e.TotalBytesToReceive values to get a bit more info here.
3) Bear in mind that UpdateAvailable will return before the download is complete as DownloadDataAsync is non blocking. This leaves the WebClient object eligible for garbage collection and that could cause problems.
Example code for checking successful completion:
private void DownloadDataCallback (Object sender, DownloadDataCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled)
{
byte[] data = e.Result;
}
else
{
}
}
I've read the other responses and it has been suggested that you should not update the progress bar directly due to cross thread issues. I disagree as WebClient operates in the same way as BackgroundWorker with event notifications marshaled to the UI thread. This means your code is safe without using Invoke on the controls.
Alan.
|
|
|
|
|
Hello,
I have an application made in visual C# which is largely dependant on communicating with an online MS Access database (hosted on jabry.com). It will have several users who are going to update the information on the database, so having it locally is not an option. I have tested it locally, and it works without any problems. I have no idea how to write the connection string for when it is ONLINE though. I've tried a lot of stuff... but nothing is working.
Any help would be greatly appreciated!
|
|
|
|
|
Define "online"?
If you're talking about user who have to get to the Access database over the Internet, then you've picked the wrong database engine entirely.
|
|
|
|
|