Wednesday, February 18, 2009

Getting and Setting cookies

I was recently having a lot of trouble trying to set and read a cookie in asp.net.  I discovered that my big issue was not correctly using the Request and Response objects.  The Request object is only used for retrieving a cookie, while the Response object is only used for creating a cookie.

Another little anomaly I found.  Apparently the client doesn't send the experation date of a cookie when it sends the cookie to the server.  So, in asp.net when retrieving a cooky the experation date will always be MinDate even if the date is set correctly.

    HttpRequest oRequest = System.Web.HttpContext.Current.Request;
    HttpResponse oResponse = System.Web.HttpContext.Current.Response;

      HttpCookie oCookie = oRequest.Cookies[ConfigurationManager.AppSettings["anonUserCookie"]];
      if (oCookie == null) oCookie = new HttpCookie(ConfigurationManager.AppSettings["anonUserCookie"], Guid.NewGuid().ToString());
      oCookie.Expires = DateTime.UtcNow.AddMonths(1);
      oResponse.Cookies.Add(oCookie);

Monday, February 16, 2009

IE7 div centering with margin auto

I've been having some problems getting a div to center on a webpage.  All the websites I have visited said the fix was easy and gave me various forms of this as a solution:

<div style="text-align:center;">
<div style="text-align:left;">
Content goes here.
</div>
</div>


However, no matter what I put in my stylesheet I couldn't get it to work.  Turns out this fix has to be applied inline.  Putting it in a stylesheet doesn't work.  Once I got that figured out and grumbled for a bit everything worked great.

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.