Click here to Skip to main content
15,886,592 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
hi all
i have a VB code to populate tree view from database.my code is
If rec2.EOF = False And rec2.BOF = False Then
    Do While rec2.EOF = False
        If dept1 <> rec2.Fields(0) Then
            d1 = d1 + 1
            TreeView1.Nodes.Add , , "*" & CStr(rec2.Fields(0)) & "*", rec2.Fields(0)
            If UCase(rec2.Fields(2)) = "ONE" Then
                TreeView1.Nodes.Add "*" & rec2.Fields(0) & "*", tvwChild, "*" & CStr(rec2.Fields(3)) & "*", Left(rec2.Fields(1), 3) & " " & rec2.Fields(4)
                one1 = rec2.Fields(3)
            ElseIf UCase(rec2.Fields(2)) = "TWO" Then
                TreeView1.Nodes.Add "*" & one1 & "*", tvwChild, "*" & CStr(rec2.Fields(3)) & "*", Left(rec2.Fields(1), 5) & " " & rec2.Fields(4)
                two1 = rec2.Fields(3)
            ElseIf UCase(rec2.Fields(2)) = "THREE" Then
                TreeView1.Nodes.Add "*" & two1 & "*", tvwChild, "*" & CStr(rec2.Fields(3)) & "*", Left(rec2.Fields(1), 7) & " " & rec2.Fields(4)
                three1 = rec2.Fields(3)
            ElseIf UCase(rec2.Fields(2)) = "FOUR" Then
                TreeView1.Nodes.Add "*" & three1 & "*", tvwChild, "*" & CStr(rec2.Fields(3)) & "*", Left(rec2.Fields(1), 9) & " " & rec2.Fields(4)
                four1 = rec2.Fields(3)
            ElseIf UCase(rec2.Fields(2)) = "FIVE" Then
                TreeView1.Nodes.Add "*" & four1 & "*", tvwChild, "*" & CStr(rec2.Fields(3)) & "*", Left(rec2.Fields(1), 11) & " " & rec2.Fields(4)
                five1 = rec2.Fields(3)
            End If
            dept1 = rec2.Fields(0)
        Else
            If UCase(rec2.Fields(2)) = "ONE" Then
                TreeView1.Nodes.Add "*" & rec2.Fields(0) & "*", tvwChild, "*" & CStr(rec2.Fields(3)) & "*", Left(rec2.Fields(1), 3) & " " & rec2.Fields(4)
                one1 = rec2.Fields(3)
            ElseIf UCase(rec2.Fields(2)) = "TWO" Then
                TreeView1.Nodes.Add "*" & one1 & "*", tvwChild, "*" & CStr(rec2.Fields(3)) & "*", Left(rec2.Fields(1), 5) & " " & rec2.Fields(4)
                two1 = rec2.Fields(3)
            ElseIf UCase(rec2.Fields(2)) = "THREE" Then
                TreeView1.Nodes.Add "*" & two1 & "*", tvwChild, "*" & CStr(rec2.Fields(3)) & "*", Left(rec2.Fields(1), 7) & " " & rec2.Fields(4)
                three1 = rec2.Fields(3)
            ElseIf UCase(rec2.Fields(2)) = "FOUR" Then
                TreeView1.Nodes.Add "*" & three1 & "*", tvwChild, "*" & CStr(rec2.Fields(3)) & "*", Left(rec2.Fields(1), 9) & " " & rec2.Fields(4)
                four1 = rec2.Fields(3)
            ElseIf UCase(rec2.Fields(2)) = "FIVE" Then
                TreeView1.Nodes.Add "*" & four1 & "*", tvwChild, "*" & CStr(rec2.Fields(3)) & "*", Left(rec2.Fields(1), 11) & " " & rec2.Fields(4)
                five1 = rec2.Fields(3)
            End If
            dept1 = rec2.Fields(0)
        End If
        rec2.MoveNext
    Loop
End If
End Sub


What I have tried:

treeview is not populating plese someone help me
Posted
Updated 9-May-17 9:41am
Comments
Dave Kreskowiak 9-May-17 10:17am    
"It doesn't work" is not a good problem description. What DOES happen? Is there an error thrown?

No, we're not going to convert your code for you. There are plenty of online code conversion sites that will convert chunks of code for you. But if the VB.NET code doesn't work, the C# version isn't going to work either.
F-ES Sitecore 9-May-17 10:49am    
Looks like VB6, not VB.net
Dave Kreskowiak 9-May-17 10:57am    
Yuk! Even worse.
CHill60 9-May-17 10:59am    
The "What I have tried:" section is for you to put the C# code that you have tried. We don't know why the treeview is not populating unless we can see your code
[no name] 9-May-17 11:01am    
VB6 to C# is not a conversion. It's called rewriting it from scratch.

Don't even try: rewrite it from scratch using the functionality of the VB6 code as a specification instead. C# uses the .NET framework, and it's controls (as well as the properties and methods they rely on) are not the same as the VB6 equivalent.

If you wrote the VB6 code, then you need to understand C# to make it work in the wider context of a .NET application anyway - and if you didn't, then find some .NET related code that does the same job instead!
 
Share this answer
 
As my colleagues stated: you have to rewrite the code from scratch. This is quite easy, if you know what above code is doing.

There are some objects in use, such as recordsets (underlined):
VB
If rec2.EOF = False And rec2.BOF = False Then
    Do While rec2.EOF = False


Even if you can create recordset object (HOW TO: Open ADO Connection and RecordSet Objects in Visual C# .NET[^]) you shouldn't do that, because it's pretty old technology.

I'd recommend to use ADO.NET[^] to connect to the database. Then you'll be able to use Command to fetch data into Reader. You can loop through the data stored in a reader to populate TreeView. Because you didn't provide what database you use, i can't provide more details.

For further details, please see:
ADO.NET Code Examples[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900