Tim

Footprints in the snow of a warped mind

Importing/Referencing DLLs in Visual Studio

Where to find me

Flickr Icon  Twitter Icon  Linked In Icon  FaceBook Icon  Windows Live Alerts Butterfly  RSS 2.0 

Business Protection by Crisis Cover
DDD South West

Tag Cloud

AJAX (4) Analysis (3) ASP (6) ASP.Net (59) Error Reporting (4) Web Service (2) WSDL (1) Atlas (2) Born In The Barn (1) Business (87) Business Start-up Advice (30) Client (17) Expanding Your Business (21) Recruitment (1) C# (21) Canoeing (4) Canoe Racing (5) Cheshire Ring Race (5) Racing (2) Training (4) CIMA (1) Cisco (1) 7970G (1) CMS (1) Code Management (1) Cohorts (4) Commerce4Umbraco (1) Content (1) Content Management (1) Content Management System (1) CSS (3) dasBlog (5) DDD (1) Design (10) Icons (1) Development (24) eCommerce (10) Employment (2) General (39) Christmas (6) Fun and Games (11) Internet (22) Random (46) RX-8 (8) Hacking (1) Helpful Script (3) Home Cinema (2) Hosting (2) HTML (1) IIS (11) iPhone (1) JavaScript (4) jQuery (1) Marketing (6) Email (1) Multipack (1) MVC (1) Networking (3) Nintendo (1) Nuget (1) OS Commerce (1) Payment (1) Photography (1) PHP (1) PowerShell (2) Press Release (1) Productivity (3) Random Thought (1) Security (2) SEO (5) Server Maintenance (6) Server Management (11) Social Media (2) Social Networking (3) Experiment (1) Software (10) Office (5) Visual Studio (14) Windows (4) Vista (1) SQL (8) SQL Server (19) Statistics (2) Stored Procedure (1) Sublime Text 2 (1) TeaCommerce (1) Testing (2) The Site Doctor (127) Turnover Challenge (1) Twitter (3) uCommerce (11) Umbraco (30) 2009 (1) 2011 (1) Web Development (66) WebDD (33) Wii (1) XSLT (1)

Blog Archive

Search

<May 2012>
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

Recent Comments

Blog Archive

Various Links

Blogs I Read

[Feed] Google Blog
Official Google Webmaster Central Blog
[Feed] Matt Cutts
Gadgets, Google, and SEO
[Feed] Ol' Deano's Blog
My mate Dean's blog on my space, equally as random as mine but not off on as much of a tangent!
[Feed] Sam's Blog
Sam is one of my younger brothers studying Product Design and Manufacture at Loughborough, this is his blog :) Enjoy!

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

newtelligence dasBlog 2.2.8279.16125

Send mail to the author(s) Email Me (Tim Gaunt)

© 2012 Tim Gaunt.

Sign In

# Tuesday, February 13, 2007

Importing/Referencing DLLs in Visual Studio

Tuesday, February 13, 2007 8:03:03 PM (GMT Standard Time, UTC+00:00)

A couple of people have got stuck on various lists/forums I’m on moving from ASP to ASP.Net and the differences there are, the one first major sticking point I had was referencing DLLs –so don’t worry you’re not the only one! So this is a really simple look at what you need to look at and how you reference DLLs –if you’ve ever added a DLL before you’ll probably find that this is too simplistic for you but read on anyway!
Firstly, referencing a DLL is basically a way of including someone else’s code within your project (or a common codebase that you re-use), this also includes controls, useful/common functions or just additional functionality such as Crystal reports.
Before you can use someone else’s code (i.e. Phil Whinstanley’s error reporting class) within your code you have to include a reference to the relevant DLL. The first thing I would do is create a folder somewhere that’s easily accessible to all machines that may need to reference the DLL i.e. “c:\Useful DLLs\”. Then, within this folder, I would create the following sub folders:

  • c:\Useful DLLs\.Net 1.1
  • c:\Useful DLLs\.Net 2.0
  • c:\Useful DLLs\.Net 3.0
  • c:\Useful DLLs\ASP.Net 1.1
  • c:\Useful DLLs\ASP.Net 2.0
  • c:\Useful DLLs\ASP.Net 3.0

This is something that I’ve only recently started doing after having multiple releases for the same DLL. For each DLL place a copy within the relevant folder.
Next, load your project within Visual Studio, right click the solution (this is the very top of the tree) and select “Add Reference":

A window will then popup that looks something like this:

Depending on what sort of reference this is, the majority of the time I would expect you’ll be needing to use the “Browse” tab –this allows you to navigate the FSO and find the DLL to reference (which should be somewhere in c:\useful dlls\). Once you’ve found it select the DLL and click Add.

Your DLL is now referenced and you should be able to start using it straight away. Depending on what you need to do with it you’ll also need to add Page and/or Codebehind imports. To check that it has imported correctly, in Visual Studio 2003 you should be able to see it in the references folder or in Visual Studio 2005 you will need to click into the "Class View" tab of the Solution explorer:

How do I identify the namespace?

I had someone ask me a while ago why the his code was throwing a compilation error, it turned out that although he had named the DLL MyDLL, the namespaces within the DLL he wanted to reference was MyNamesapce so how can you identify the namespace?

The easiest way to do this is to use something called the Object Explorer, this should list all the referenced DLLs for a given project and allow you to navigate the namespaces, classes and objects within the class. To open the Object Explorer click on the View menu and then “Object Explorer” within the “Windows” menu. Navigating the DLL is easy, you can either search through it using the search box at the top or alternatively navigate using the object tree.

The best way to work out what declaration you need to add is to locate the object, method or control you plan on using either using the tree navigation or searching, then selecting it. Once selected you will notice the bottom pane of the Object Explorer will change and the namespace will be listed, this is what you need to add as your reference. If you need to enter the assembly name, you can identify this easily as it’s the name given to the top node of the tree –this should have a little grey icon next to it.

If the DLL is adding a control to the page

You’ll need to reference the namespace at the top of the page like this:

<%@ Register TagPrefix="TSD" Namespace="TheSiteDoctor.WebControls" Assembly="TheSiteDoctor" %>

You can use whatever prefix you like for the control, I tend to keep it between 2 and 4 characters in length for ease i.e. “TSD” but that’s up-to-you. Adding the control is done in the same way you add the standard controls:

<TSD:SuggestionTypeRadioButtonList runat="server" ID="radCategories" CssClass="inputRadio" ValidationGroup="suggestion" />

You’re all set :)

If however this is a control set that you plan on re-using throughout the application I would opt to add a reference within the web.config, this means you don’t need to repeatidly add the reference for each page. To do this you’ll need to add the following to your web.config file:

<system.web>
   <pages validateRequest="false">
     <controls>
       <add tagPrefix="TSD" namespace="TheSiteDoctor.WebControls" assembly="TheSiteDoctor" />
     </controls>
   </pages>
</system.web>

If the DLL is adding functionality to the codebehind or you want to use the control within the codebehind

If you want to use the control or add the control to the page dynamically you will need to include a reference to the namespace within the codebehind –in the same way you do the System namespaces. This is really simple, at the top of the page you should see a few “using” statements or in VB “Imports”, you’ll just need to add the referenced DLLs namespace below (or above –or- in the middle!) of these others, as long as it’s with the other statements you’ll be fine. You can then reference the various methods and properties of the control.

using TheSiteDoctor.WebControls;

public partial class SuggestionsPage : System.Web.UI.Page
{
    protected void btnAddEntry_Click(object sender, EventArgs e)
    {
        int cat = radCategories.CategoryId;

        //Do something with it here...
    }
}

I hope that helps you getting started with this new way of importing common code, it’s fairly intuitive once you’ve done it once or twice, but those first few “Could not find xyz –Are you missing an Assembly or Reference” messages do drive you nuts ;)

 

Don't forget to follow me on Twitter.

Wednesday, April 04, 2007 10:20:54 PM (GMT Daylight Time, UTC+01:00)
I wonder why it didn't work by clicking on:
Project > Add Reference? It seems to work when you Right click on References from the Solution Explorer Window and Clicking on Add Reference... as you point out.

... thanks for the help though.
Wednesday, April 11, 2007 1:25:42 PM (GMT Daylight Time, UTC+01:00)
Hi Kidus,

I'm not sure, it seems to work fine doing it that way here too, I just find the solution explorer is faster :)

What version of Visual Studio are you using?

Tim
Tuesday, February 10, 2009 5:16:25 AM (GMT Standard Time, UTC+00:00)
Hi,

I read your tutorial. I am developing a MFC visual c++ project in VS2003. I need to access the code provided in an external dll. I followed your instructions. But when I browse the relevant dll and click ok; an error is given: "Error Adding reference to the project"

I am stuck there without being able to proceed. Any help would be greatly appriciated.
Hasini
Name
E-mail
(will show your gravatar icon)
Home page

Comment (HTML not allowed)  

Live Comment Preview