0

I have an Access VBA project from where I refer to a COM Interop .TLB written in C#. This C# code simply queries the SQL Server database and returns values via a simple LINQ-to-Entity query.

I'm getting the same error mentioned in this question:

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid

However, in my case, it is a Access VBA in a .ADP application that refers to my .Net 4.0 TLB, instead of another .Net project.

I'm aware that if it were another .Net project, I could add the EF connection string XML in its app.config or web.config. But what is the fix if my 'calling' application is Access 2003 VBA?

Here's the VBA code that calls the .Net code

Dim CandidatePassword As String
Dim abc As New MISHash.Password

Dim PasswordStatus As Boolean
CandidatePassword = InputBox("Enter your password")
PasswordStatus = abc.IsValidPassword("myusername", CandidatePassword) ' FAILS HERE
If PasswordStatus Then
    MsgBox "Password valid."
Else
    MsgBox "Password failed."
End If

Please help. Thank you.

Update: Here is my C# code

using System.Linq;
using System.Runtime.InteropServices;
namespace MISHash
{

public class Password
{

   public Password()
   {

   }

   [ComVisible(true)] 
   public  void HashAndSave(string SomePassword)
    {
        string hashed = BCrypt.HashPassword(SomePassword, BCrypt.GenerateSalt(12));
        //save the hashed password in the database
    }

   [ComVisible(true)]
   public bool IsValidPassword(string CandidateUserName, string CandidatePassword)
    {

        string OriginalHashedPassword;
        using (MyDBEntities mycontext = new MyDBEntities())
        {
            OriginalHashedPassword = (from usr in mycontext.Users
                                       where usr.UserName.Equals(CandidateUserName)
                                       select usr.Password).FirstOrDefault();
        }
        bool matches = BCrypt.CheckPassword(CandidatePassword, OriginalHashedPassword);
        return matches;
    }
}
 }
Community
  • 1
  • 1
FMFF
  • 1,652
  • 4
  • 32
  • 62

1 Answers1

1

See this similar question:
Can I use / access the app.config from .net code, when called via COM

These two seem like your best options:

  1. Manually create a secondary AppDomain
  2. Convert to a VSTO project

Edit

You can also try passing a hard-coded connection string in the constructor:

MyDBEntities mycontext = new MyDBEntities("Server=.\SQLEXPRESS;Database=School;Trusted_Connection=true;Integrated Security=True;MultipleActiveResultSets=True"))
Community
  • 1
  • 1
Rami A.
  • 10,302
  • 4
  • 44
  • 87
  • My connection string looks like this: metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=DBSERVER;Persist Security Info=True;User ID=dbuser;Password=dbpwd;MultipleActiveResultSets=True;Connect Timeout=120;Application Name=EntityFramework" How can we pass this to the constructor? – FMFF Jan 02 '12 at 19:55
  • I tried the approach suggested under your Edit; instead of connection string, I built and used EntityConnection connection object as suggested [here](http://msdn.microsoft.com/en-us/library/bb738533.aspx) and it worked. Thank you so much for showing me the way. – FMFF Jan 02 '12 at 22:05