Tuesday, January 20, 2009

OleDbConnection.GetSchema MS Access list of Tables, Columns, Key columns

I have submitted a couple of community content pieces of code here.

They give details on how to use the ole getschema method to retrieves the schema from an access database to get tables, columns, or key columns.

Thursday, January 15, 2009

NUnit error - Could not load type 'custom.namespace' from assembly 'System.Web...

I was recently having some issues using NUnit to test my code when the code was using the .net membership profile provider.  Scott Davis summed it up nicely:

I've implemented it easily in my site and it works great, but have you been able to unit test it?  On my first attempt, the inherited "Create" method returned a DefaultProfile object, which caused a casting failure.  I figured out that the Unit Tests were not using the web.config file to see the custom Profile Provider I configured, then I figured out you need to copy the web.config to the unit test project, and rename it to app.config.  However now I get the error.  

"Test method TestMyProject.UserControllerTest.CreateNewUserTest threw exception:  System.TypeLoadException: Could not load type 'MyRootNamespace.Profile.UserProfile' from assembly 'System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.."

I don't know why it is not looking the DLL that contains my custom "UserProfile" class.  Any ideas?  It works in my website, but not in the Unit Test.  The UnitTest project references the project containing the Profile class.  I'm using VS2008 built-in Unit Testing.

A little further down I found a link to a forum where the answer was eventually discovered from here and explained a little further:
My config file was something like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="MeanWormConnectionString" connectionString="Server=localhost;Database=MeanWormDB;Trusted_Connection=true"/>
  </connectionStrings>

  <system.web>

    <membership defaultProvider="MeanWormMembershipProvider">
      <providers>
        <remove name="AspNetSqlMembershipProvider"/>
        
          <add applicationName="MeanWorm" requiresQuestionAndAnswer="false"
            requiresUniqueEmail="true" minRequiredNonalphanumericCharacters="0"
            enablePasswordReset="true" passwordFormat="Hashed" connectionStringName="MeanWormConnectionString"
            name="MeanWormMembershipProvider" type="MeanWorm.Domain.Providers.MeanWormMembershipProvider" />
        
      </providers>
    </membership>
  </system.web>
  
</configuration>

 

 His config file was this:

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="MeanWormConnectionString" connectionString="Server=localhost;Database=MeanWormDB;Trusted_Connection=true"/>
  </connectionStrings>

  <system.web>

    <membership defaultProvider="MeanWormMembershipProvider">
      <providers>
        <remove name="AspNetSqlMembershipProvider"/>
        
          <add applicationName="MeanWorm" requiresQuestionAndAnswer="false"
            requiresUniqueEmail="true" minRequiredNonalphanumericCharacters="0"
            enablePasswordReset="true" passwordFormat="Hashed" connectionStringName="MeanWormConnectionString"
            name="MeanWormMembershipProvider" type="MeanWorm.Domain.Providers.MeanWormMembershipProvider,MeanWorm.Domain" />
        
      </providers>
    </membership>
  </system.web>
  
</configuration>

 

 

Can you spot the difference? It's in the "type" attribute of the add provider tag. If you have a custom provider and do not specify the partial assembly name to find the class in, nunit flips out and tries to load the class from the default assembly, which is System.Web.dll.

If you want more information on how the type attribute works check out this post.

Wednesday, January 14, 2009

NUnit: Could not load file or assembly 'Microsoft.Practices.ObjectBuilder

I use NUnit for testing and was getting this error when using one of the Microsoft app blocks to write stuff to the event log.
Turns out the answer was simple, the Microsoft.Practices.ObjectBuilder.dll simply didn't get copied locally in the test project even though it was set to copy local. I manually copied in the dll and it works great.

Tuesday, January 13, 2009

Create Strong Name Key - Access Denied Error

I recently ran into this error when trying to generate a new strong name key file.

I found the solution here:
I had to give my user account access to the key container in C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys. It turns out, even administrator rights were disabled on my machine! I am not sure if this was the default installation (I rebuilt the machine a few weeks ago) or if this is modified when I install WinFX, or if when I granted ASP.NET and NETWORK SERVICE access it somehow lost the inherited permissions that were granted when I installed the OS. In any case, at least it seems predictable, you need access to the key store in order to generate keys, makes sense!

Monday, January 12, 2009

Copy and Paste from Excel to MS Access says corrupt clipboard

I was trying to copy a massive amount of information from MS Excel 2007 to MS Access 2007.

After going through all the standard troubleshooting like rebooting and making sure all versions were compatible, I mentioned the issue to my co-worker.  Fortunately for me my co-worker had already experience and spent time figuring out the issue; when I heard what the problem was I was glad he was the one who did the leg work figuring it out.  I didn't do that much work with Excel and Access so I wasn't sure when the problem had begun.

Turns out a relatively recent Microsoft Excel security update ( KB958437)  was the culprit.  As soon as I got that uninstalled copy and paste between the two apps started working again.

Add or Remove Programs list not populating

I spent several days trying to figure out why my Add or Remove Programs list no longer populated.  It would just sit there saying populating, but not actually displaying any programs.

Finally I happened accross this thread, where someone was talking about an extra drive of their causing problems.  Turns out I had an external hard drive that was corrupted, as soon as I turned it off the list started working again.

Friday, January 9, 2009

Using the Data Link Properties wizard in .NET code

I had a really hard time finding an example for this code so I decided to blog about it.

With .NET and Microsoft's dedication to documentation in the last couple of years I have really gotten spoiled.  It is pretty rare that I actually dig into .dll's anymore trying to find some obscure functions and how to use them.  These days if I can't find code examples using the Google search engine for something I want to do I will often just find another way around the problem, it just isn't worth spending time trying to dig into the lower levels for a very high level application.  ( Google Chrome is one exception to this, thanks Google for digging for the extra speed, I love it. )

In this case I spent a few minutes searching for a connection string builder.  I didn't find one so I started to role my own.  After a couple of hours of that and realising just how many options there are I decided to try searching again.  I knew one existed, Microsoft uses it in all their apps it seems, just had to find where it was and how to call it.

I found this guy:
who had essencially made a wrapper around what I was looking for, but there was no source code.  However the fact he did it reinforced my certainty that I would find what I wanted.

Next I ran into Elisabeth who gave me the big break with source code and everything:
[quote source="Elisabeth"]
using MSDASC;
using ADODB;

private string BuildConnectionString()
{
string strConnString = "";
object _con = null;
MSDASC.DataLinks _link = new MSDASC.DataLinks();
_con = _link.PromptNew();
if (_con == null) return string.Empty;
strConnString = ((ADODB.Connection)_con).ConnectionString;
return strConnString;
}
[/quote]

I did end up having to go to her source ( glad she linked to it ) to get the pretty names of the .dll's that needed to be referenced:
Microsoft ActiveX Data Objects 2.8 Library
Microsoft OLEDB Service Component 1.0 Type Library