Hyperlink using vb.net

layla

Member
Joined
Apr 26, 2005
Messages
21
Programming Experience
Beginner
I have data from a mysql database being displayed in a treeview structure using vb.net. The last childnode that is coded for needs to be a huperlink to the actual site that the hyperlink represents. Please please please does anyone know the code to do this, as I dont have a clue and have been working on it for days.

I want childnode2 (childrow3) HUGO to be the link to e.g. http://www.google.co.uk

Thanks alot in advance

Layla

Here is some of the code:



For Each childrow In parentrow.GetChildRows("ParentToRelation2")



For Each childrow2 In childrow.GetChildRows("Relation2ToChild")



childnode = parentnode.Nodes.Add(childrow2("names") & " " & childrow2(("GO_number"))) 'update treeview child

childs.Add(childrow2("names")) 'update seen array

End If

For Each childrow3 In childrow.GetChildRows("Relation2ToGene")

childnode2 = childnode.Nodes.Add(childrow3("HUGO"))
' update treeview gene

Next

Next childrow2

Next childrow

Next parentrow

TreeView2.EndUpdate()

End Sub

 
You use the TreeView.AfterSelect event handler to handle a node being clicked. There is a help topic entitled "Determining Which TreeView Node Was Clicked (Windows Forms)" that will show you how to do just that. If there is a specific node that you want to look out for, then you need to test for that node in that event handler and act accordingly. If you want every leaf node to be a hyperlink, you would have to test whether the selected node has any children via its Nodes property. Once you know that you want to open a web page, you would do so in the user's default browser by passing the desired URL to Process.Start.
 
thankyou

Thankyou, thats been a great help. I have got each node to go to the same weblink as follows:

Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

' Determine by checking the Node property of the TreeViewEventArgs.

MessageBox.Show(e.Node.Text)

If e.Node.Text = "Condition" Then

Process.Start(http://www.google.co.uk)

End If

End Sub

BUT:
There are three levels in my tree. The second node is the one that will be a hyperlink. Each second node is to go to a different link. Will this just be lots of "IF" statements? i.e. If e.node.text = "" Then etc etc? Or is there a better way to do this?

Also I can I just confirm something......Although I have all the links stored in the underlying mysql database I am guessing I still have to hardcode them within vb.net?

Thanks again in advance

Layla
 
I'm not sure how you have your database or application structured, but if it's viable then here's what I would do.

Have two columns in your database table that contain the friendly name for the link and the URL, with the friendly name being the primary key. Retrieve the entire table from the database and populate the TreeView with nodes that contain the friendly names. When the user clicks a node, pass the friendly name to the Rows.Find method of the DataTable that contains the link data to get the row that contains that friendly name and its corresponding URL. Then you simply read the URL from the appropriate field of the row and you are Bob's nephew.

You may need things a little more complex than this but it's a good basis.
 
code

Thanks again, I understand the logic, but I am stuck with the code. Can I explain the mysql structure to you a bit more. My table that I use to display the "friendly" names is in column 2, after the primary id column. The hyperlink addresses are stored in column 3.

The treeview structure will present column2 ("friendly" names) at the 2nd level, i.e. childnode2. 1) How do I code to determine if a childnode has been clicked on or not? 2) How do I code to go back to the database though to get this hyperlink? (I know I will need a connection string....?)

Thanks again
 
Assuming that you are retrieving all the friendly names from the database in order to populate the TreeView, I'd say you should get all the URLs at the same time. That way you don't have to go back to the database each time a node is clicked.

Once you have the node, you know whether it is a leaf node by checking the Nodes.Count property, which will be zero if it has no child nodes. Also I believe if you double-click a non-leaf node it will be expanded or collapsed, which you can test for in the AfterSelect event handler.

Assuming all your friendly names are unique, you can still set that as the primary key of the DataTable even though it is not the primary key for the database table. You would just need to set the MissingSchemaAction property of the DataAdapter to Add, and not AddWithKey, and no primary key will be set automatically. You can then set the PrimaryKey property of the DataTable yourself. Here's some incomplete code to do much of this:
VB.NET:
 Private urlTable As New DataTable

	Private Sub PopulateTree()
		Dim connection As New OleDbConnection("connection string here")
		Dim adapter As New OleDbDataAdapter("SELECT Name, URL FROM MyTable", connection)

		adapter.Fill(urlTable)
		urlTable.PrimaryKey = New DataColumn() {urlTable.Columns("Name")}

		'Add nodes to TreeView here.
	End Sub

	Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
		If e.Action = TreeViewAction.ByMouse Then
			Process.Start(CStr(urlTable.Rows.Find(e.Node.Text)("URL")))
		End If
	End Sub
 
I think error is minor

Thankyou for that, it was very helpful. I have tried to do as best what you said....I have my code below. I have within vb.net set the friendly names ("names") as a primarykey, although in my real mysql schema, the "id" is the primary key, if i type "describe table child".

The childnode in my treeview structure is this "friendly name". I am guessing that we are comparing this node string with the primary key "names" in the database table and then via Process.Start(CStr(childtable.Rows.Find(e.Node.Text)("weblink"))), we are finding that row.

When I run the application and click on the "names" node, my current error highlights the line:
Process.Start(CStr(childtable.Rows.Find(e.Node.Text)("weblink"))) and reads:

An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe
Additional information: Object reference not set to an instance of an object.

please HELP

This is my code so far:
Dim
db2 As DataSet
db2 =
New DataSet

Dim childtable As New DataTable

Dim version1 As New OdbcConnection("my connection string")
Dim child As New OdbcDataAdapter("SELECT id, names, weblink from child", version1)

version1.Open()

child.Fill(db2, "child")

'Adapter.Fill(urlTable)

child.MissingSchemaAction = MissingSchemaAction.Add

Dim mycolumn As DataColumn

mycolumn = New DataColumn

mycolumn.DataType = System.Type.GetType("System.String")

mycolumn.ColumnName = "names"

childtable.Columns.Add(mycolumn)


'gives the childtable the friendly names primary key

childtable.PrimaryKey = New DataColumn() {childtable.Columns("names")}



If e.Action = TreeViewAction.ByMouse Then

Process.Start(CStr(childtable.Rows.Find(e.Node.Text)("weblink")))

End If


 
Why are you adding an extra column to your DataTable? You are adding a new column and making that the PrimaryKey, but it never has any values put in it so Find will never return a row. You simply set the existing column that contains the friendly name to be the PrimaryKey, as I did in my code. Also, if you were going to set the MissingSchemaAction property of the data adapater you would need to do it before the call to Fill, because that's where it has an effect. The good news is that the default for that property is Add, so it will already contain the correct value. You can therefore remove that line.
 
added primary key but still missing

I did try this way, but I had the error (highlighted in bold in the code):

An unhandled exception of type 'System.Data.MissingPrimaryKeyException' occurred in system.data.dll
Additional information: Table doesn't have a primary key.

I thought I made names the primary key??


Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
Dim db2 As DataSet
db2 = New DataSet

Dim childtable As New DataTable

Dim childrow As DataRow

Dim database As New OdbcConnection (connection string put here!)
Dim child As New OdbcDataAdapter("SELECT id, names, weblink from child", database)

database.Open()
child.Fill(database, "child")
Dim mycolumn As DataColumn
mycolumn = New DataColumn

childtable.PrimaryKey = New DataColumn() {childtable.Columns("names")}

If e.Action = TreeViewAction.ByMouse Then
Process.Start(CStr(childtable.Rows.Find("names")("weblink")))
End If

Thankyou
 
Look at the code you have posted and think about what each line is doing. You are creating a new DataTable and assigning it to the childTable variable, but you never Fill it with data. You fill another DataTable altogether, named "child", that is part of the db2 DataSet, but you never set its primary key. Because the childTable DataTable is never filled, it has no columns, so it cannot have a primary key. Also, I see you are still trying to create a new DataColumn called mycolumn. DO NOT do that. Just do what I did in the code I posted. Fill a single DataTable and then set the PrimaryKey of that table. Whether that table is stand-alone or part of a DataSet is up to you, but you only need ONE DataTable.
 
I see!

Ok, the primary key error has been fixed, but now I vannot compile the code because, I had declared child as a new datatable and also as a new odbcadapter and I need both. The table in my database is called child so I guess at least i am changing the primary key of the correct table! But the declare is a bit of an issue...

Try
Dim db2 As DataSet
db2 = New DataSet

Dim child As New DataTable

Dim childrow As DataRow

Dim db As New OdbcConnection("DRIVER={MySQL ODBC 3.51 Driver}

Dim child As New OdbcDataAdapter("SELECT id, names, weblink from child where flag_id=2", db)

db.Open()

child.Fill(db2, "child")

child.PrimaryKey = New DataColumn() {Child.Columns("names")}

If e.Action = TreeViewAction.ByMouse Then
Process.Start(CStr(child.Rows.Find("names")("weblink")))
childrow = child.Rows.Find("names")
 
How about using descriptive names for your variables, like "childTable" for the DataTable and "childAdapter" for the DataAdapter. Other than that it looks like you're on the right track. One final thing: if you are simply calling Fill once then there is no need to explicitly call Open on the connection. Fill will open and close the connection for you. You only need to do it yourself if you are filling multiple tables so that the connection is not closed after each one. If you do call Open on the connection, make sure you call Close when you are done.
 
weblink

I changed, the child to childadapter. The code is compiling and the treenodes at the grandparent, parent and child level are opening fine. The only problem is that the weblinks are not opening. I think there is a problme at the highlighted line below. If I hardcode a weblink to open its fine, but somehow I dont think the link can be read from the database. (im so close :( )

Try
Dim db2 As DataSet
db2 = New DataSet

Dim child As New DataTable

Dim childrow As DataRow

Dim db As New OdbcConnection("DRIVER={MySQL ODBC 3.51 Driver}

Dim childadapter As New OdbcDataAdapter("SELECT id, names, weblink from child where flag_id=2", db)

db.Open()

childadapter.Fill(db2, "child")

child.PrimaryKey = New DataColumn() {Child.Columns("names")}

If e.Action = TreeViewAction.ByMouse Then
Process.Start(CStr(child.Rows.Find("names")("weblink")))

end if

db.close
 
Then you need to set a breakpoint and see exactly what CStr(child.Rows.Find("names")("weblink")) contains. We don't know what is in your database from here. You have to use the tools available to you to check the values of your variables and make sure they are what you expect.
 
Back
Top