Archive for the ‘.NET’ Category

Using MyODBC with ASP.NET in IIS7 on Vista x64

That’s a heck of a title, but it’s a problem I hit recently. I have a bunch of ASP.NET sites that use MySQL as their datastore, but I hadn’t tried the on IIS7 yet. It took a while to get them to work at all (I had to set permissions on web.config and the other website files so that they could be read by both the Users group and the IIS_IUSRS group), but then I was left with an error about my MySQL connection. “ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified” – another very general error that basically means “Something is wrong with your ODBC driver, somewhere.”

After some searching, I learned two things. The first is that if you’re running 64-bit you can’t use the standard ODBC Data Source Administrator in Administrative Tools with MySQL. You’ve got to go to C:\Windows\SysWOW64\odbcad32.exe and set up your DSN, if that’s your thing. The other thing is that the MyODBC driver is 32-bit only. So to use it at all, you need to make sure you’re calling it from 32-bit apps only. That means you’ve got to tweak the Application Pool you’re using to run all its ASP.NET applications as 32-bit. To do this, go to Administrative Tools > Internet Information Services (IIS) Manager (or just hit the Windows key and type “IIS”). Then go to “Application Pools” and select whichever application pool your ASP.NET app uses (or create a new one just for your MySQL apps. Click “Advanced Settings…” and set “Enable 32-Bit Applications”. Now the AppPool will use the 32-bit .NET CLR to run your app, and it’ll be able to see your MyODBC driver (whether you use a DSN or not).

IIS7 32-bit Application Pool

Creating nested custom configuration sections in ASP.NET 2.0

This weekend I decided to go through the hodgepodge of common code that's shared between a lot of my ASP.NET websites and refactor it a bit. I'd only just learned about the magic of HttpModules and HttpHandlers, and I immediately saw a lot of canidates in my copy-paste code and global.asax handlers where a HttpModule would be a better solution. One of these was the code I was using to redirect old pages to new pages whenever I moved them. For example, at some point I had moved http://www.numbera.com/rome/tools.aspx to http://www.numbera.com/rome/tools/, and I wanted anyone who visited the old URL to get redirected to the new one. Previously, I just had some code in global.asax that hooked Application_OnError, checked to see if it was an HttpException (a 404 file not found, specifically), and then redirected if it knew where the file really was. Pretty simple, but not very general. So I broke it out into an HttpModule that basically did the same thing, but no longer required me to cut and paste code into my global.asax. However, one improvement I wanted to make was to allow for configuration through my web.config file, instead of having to hardcode an if/else tree for each redirect. I basically wanted to have a section in my web.config like this:

XML:
  1. <configsections>
  2.     <sectiongroup name="brh.web">
  3.        <section name="redirectOldUrls"
  4.          type="Brh.Web.RedirectConfigurationHandler, Brh.Web.Utility"
  5.        />
  6.    </sectiongroup>
  7.   </configsections>
  8.  
  9.   <brh .web>
  10.     <redirectoldurls>
  11.       <redirect filePattern="tools.aspx" url="~/tools/" />
  12.       <redirect filePattern="strategy.aspx" url="~/strategy/" />
  13.         <redirect filePattern="military_people.aspx" url="~/people/" />
  14.         <redirect filePattern="history.aspx" url="~/history/" />
  15.         <redirect filePattern="teacher.aspx" url="~/teacher/" />
  16.     </redirectoldurls>
  17.   </brh>

(more...)