0

I have the annoying task of hooking a c# aspx page up to postgresql with Npgsql.

The problem that i have is that when i try to import Npgsql, i get the following aspx error message returned:

CS0246: The type or namespace name 'Npgsql' could not be found (are you missing a using directive or an assembly reference?)

I have a javascript function that calls the aspx page below to get data from the db:

<%@ language="C#"%>
<%@ Import Namespace="Npgsql" %>
<%

  // PostgeSQL-style connection string
  string connstring = String.Format("Server=localhost;Port=5432;UID=posgres;Password=pass;Database=postgres_db;";
  NpgsqlConnection conn = new NpgsqlConnection(connstring);      
  conn.Open();
%>

Im assuming i need to set it up in my web.config, but i cannot for the life of me it out.

Any help would be greatly appreciated! Thanks.

1 Answer 1

1

Your problem is the Npgsql.dll that is not being references. You only import the Namespace.

It's not best practice to hack SQL into a ASP.NET document... The solution would be adding the DLL to the web.config file:

 ...
  <system.web>         
    <compilation debug="true" targetFramework="4.5">                              
      <assemblies> 
        <add assembly="Npgsql, Version=2.0.11.93, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />  
  ...

But I recommend to create a class library project or at least a Web Application project using Visual Studio (Express version is FREE). Then right click on the directory named references and add the DLL there. Why? N-Tier (http://en.wikipedia.org/wiki/Multitier_architecture): better organization of code by it's intent. Easier to find errors and change/maintain code...

Sign up to request clarification or add additional context in comments.

3 Comments

I thought that i would need to add something in the web.conf, ill give your code a try, however, if i just have a few aspx files (not part of a web application project) and im calling in the assemblies in the web.conf file, how would i access that from my aspx files? just 'import' and use it the way i have in my code, or do i need to so something fancy?
Oh darn, just got a chance to test this out now, and im getting an error when the page loads 'Could not load file or assembly 'Npgsql, Version=2.0.11.93, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7' or one of its dependencies. The system cannot find the file specified.'
Of course you need to set the exact version/public key of YOUR Npgsql.dll

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.