NullReferenceException with MySQL connection
<p>This is going to drive me crazy.</p>

<p>I built a simple web app for use on the intranet, it was working fine during local testing. When I deployed it onto the IIS server, it was giving me issues, which we think was related to a bad web.config file.</p>

<p>I went through and configured the web.config file exactly as the MySQL documentation says, and now the app won't work locally either. It's obviously related, but I can't figure out what's causing it.</p>

<p>With the MySQL settings, I'm getting a NullReferenceException with this stack trace:</p>

<pre><code>[NullReferenceException: Object reference not set to an instance of an object.]
MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(DbConnection connection) +56
System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +234
MySql.Data.Entity.MySqlManifestTokenResolver.ResolveManifestToken(DbConnection connection) +36
System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest&amp; providerManifest) +55
System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +56
System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +79
System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +112
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +635
System.Data.Entity.Internal.InternalContext.Initialize() +28
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +27
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +79
System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() +28
System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.IEnumerable.GetEnumerator() +64
System.Web.UI.WebControls.ListViewPagedDataSource.GetEnumerator() +305
System.Web.UI.WebControls.ListView.CreateItemsWithoutGroups(ListViewPagedDataSource dataSource, Boolean dataBinding, InsertItemPosition insertPosition, ArrayList keyArray) +215
System.Web.UI.WebControls.ListView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +952
System.Web.UI.WebControls.ListView.PerformDataBinding(IEnumerable data) +63
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +128
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +34
System.Web.UI.WebControls.ModelDataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +80
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143
System.Web.UI.WebControls.ListView.PerformSelect() +114
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +114
System.Web.UI.WebControls.ListView.CreateChildControls() +77
System.Web.UI.Control.EnsureChildControls() +92
System.Web.UI.Control.PreRenderRecursiveInternal() +42
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +883
</code></pre>

<p>Attempting to step through the code just causes it to fail as soon as the first item runs, doesn't seem to be linked to any particular line. This is what is in my web.config file now that I've changed it.</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;configuration&gt;
  &lt;configSections&gt;
    &lt;!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --&gt;
    &lt;section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /&gt;
  &lt;/configSections&gt;
  &lt;appSettings&gt;
    &lt;add key="PrinterTemplate" value="--redacted--" /&gt;
    &lt;add key="PrinterQueue" value="--redacted--" /&gt;
    &lt;add key="FileVault" value="--redacted--" /&gt;
    &lt;add key="AdminGroup" value="--redacted--" /&gt;
  &lt;/appSettings&gt;
  &lt;location path="Modify.aspx"&gt;
    &lt;system.web&gt;
      &lt;authorization&gt;
        &lt;allow roles="--redacted--" /&gt;
        &lt;deny users="*,?" /&gt;
      &lt;/authorization&gt;
    &lt;/system.web&gt;
  &lt;/location&gt;
  &lt;location path="AccessDenied.aspx"&gt;
    &lt;system.web&gt;
      &lt;authorization&gt;
        &lt;allow users="*" /&gt;
      &lt;/authorization&gt;
    &lt;/system.web&gt;
  &lt;/location&gt;
  &lt;connectionStrings&gt;
    &lt;add name="DefaultConnection" connectionString="DRIVER={MySQL ODBC 3.51 Driver};Server=servername;port=3306;Database=usermanuals;UId=username;password=password" providerName="MySql.Data.MySqlClient" /&gt;
  &lt;/connectionStrings&gt;
  &lt;system.web&gt;
    &lt;compilation debug="true" targetFramework="4.5.2" /&gt;
    &lt;httpRuntime targetFramework="4.5.2" /&gt;
    &lt;authentication mode="Windows" /&gt;
    &lt;authorization&gt;
      &lt;deny users="?" /&gt;
    &lt;/authorization&gt;
    &lt;pages&gt;
      &lt;namespaces&gt;
        &lt;add namespace="System.Web.Optimization" /&gt;
      &lt;/namespaces&gt;
      &lt;controls&gt;
        &lt;add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" /&gt;
      &lt;/controls&gt;
    &lt;/pages&gt;
  &lt;/system.web&gt;
  &lt;runtime&gt;
    &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt;
      &lt;dependentAssembly&gt;
        &lt;assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /&gt;
        &lt;bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /&gt;
      &lt;/dependentAssembly&gt;
      &lt;dependentAssembly&gt;
        &lt;assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" /&gt;
        &lt;bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /&gt;
      &lt;/dependentAssembly&gt;
    &lt;/assemblyBinding&gt;
  &lt;/runtime&gt;
  &lt;system.codedom&gt;
    &lt;compilers&gt;
      &lt;compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" /&gt;
      &lt;compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&amp;quot;Web\&amp;quot; /optionInfer+" /&gt;
    &lt;/compilers&gt;
  &lt;/system.codedom&gt;
  &lt;system.data&gt;
    &lt;DbProviderFactories&gt;
      &lt;remove invariant="MySql.Data.MySqlClient" /&gt;
      &lt;add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" /&gt;
    &lt;/DbProviderFactories&gt;
  &lt;/system.data&gt;
  &lt;entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6"&gt;
    &lt;defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/&gt;
    &lt;providers&gt;
      &lt;provider invariantName="MySql.Data.MySqlClient"
          type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/&gt;
      &lt;provider invariantName="System.Data.SqlClient"
          type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/&gt;
    &lt;/providers&gt;
  &lt;/entityFramework&gt;
&lt;/configuration&gt;
</code></pre>

<p>I've seen a lot of people say it was related to their actual usage of the connection string, but mine isn't triggering on the actual calls to the database, but as soon as the web app runs. Regardless, this is how I actually use the connection string in my code. All of my usages have the same structure, just a different command.</p>

<pre><code>using (OdbcConnection connection = new OdbcConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
    connection.Open();
    using (OdbcCommand command = new OdbcCommand("SELECT id, title FROM manuals", connection))
    using (OdbcDataReader dr = command.ExecuteReader())
    {
       //databindings and stuff.
    }
}
</code></pre>