1

I am trying to use a LInklabel to open a hyperlink i have on my access database. However, this is the first time using a linklabel. Any suggestions would be great!

con.Open();

        str = "Select * from loc where link ='" + facility+ "'";
        cmd = new OleDbCommand(str, con);
        dr = cmd.ExecuteReader();


        if (dr.Read())
        {
            linkLabel1.Text = dr.GetString(17);
        }
hex c
  • 705
  • 2
  • 13
  • 22
  • Any error or exception message? And are you sure your `loc` table has `18` column? What do you see when you debug your code? – Soner Gönül Oct 23 '13 at 10:57
  • The link does not open the hyperlink i have on access with no error. Sorry the table has 5 columns, the link on access database is on column 5. – hex c Oct 23 '13 at 10:59
  • It was a typo on the code i put here. the code is correct on VS. I just need to find a way to link them. – hex c Oct 23 '13 at 11:08

1 Answers1

1

The following code works for me:

private void Form1_Load(object sender, EventArgs e)
{
    using (var con = new OleDbConnection())
    {
        con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Public\Database1.accdb;";
        con.Open();
        using (var cmd = new OleDbCommand())
        {
            cmd.Connection = con;
            cmd.CommandText = "SELECT FirstName, website FROM Clients WHERE ID = 1";
            OleDbDataReader rdr = cmd.ExecuteReader();
            rdr.Read();
            String fName = rdr["FirstName"].ToString();
            String url = rdr["website"].ToString();
            if (url.Substring(0,1).Equals("#"))
            {
                // remove leading and trailing hash marks from URL
                //     as retrieved from a Hyperlink field in Access 
                url = url.Substring(1, url.Length - 2);
            }
            linkLabel1.Text = String.Format("Link to {0}'s website", fName);
            linkLabel1.Links.Add(0, linkLabel1.Text.Length, url);
        }
        con.Close();
    }
}

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    string target = e.Link.LinkData as string;
    System.Diagnostics.Process.Start(target);
}
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • Gord! That worked perfectly!! thank you! On my access database i had to change the datatype of my field from hyperlink to just text and it worked perfectly. It even works to open local folders. Thanks! – hex c Oct 24 '13 at 07:49
  • Has anyone come across MS documentation about how the field values of hyperlink fields show up as ## when queried from code? – JohnG79 Feb 09 '19 at 00:54
  • 1
    @JohnG79 - As mentioned in [this answer](https://stackoverflow.com/a/30000290/2144390) there's [this page](https://learn.microsoft.com/en-us/office/vba/api/access.application.hyperlinkpart). – Gord Thompson Feb 09 '19 at 01:12