I get a "An attempt was made to load a program with an incorrect format" error on a SQL Server replication project

C#Sql ServerReplication

C# Problem Overview


The exact error is as follows

> Could not load file or assembly 'Microsoft.SqlServer.Replication, > Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' > or one of its dependencies. An attempt was made to load a program with > an incorrect format.

I've recently started working on this project again after a two month move to another project. It worked perfectly before, and I've double checked all the references.

C# Solutions


Solution 1 - C#

The answer by baldy below is correct, but you may also need to enable 32-bit applications in your AppPool.

Source: http://www.alexjamesbrown.com/uncategorized/could-not-load-file-or-assembly-chilkatdotnet2-or-one-of-its-dependencies-an-attempt-was-made-to-load-a-program-with-an-incorrect-format/

Whilst setting up an application to run on my local machine (running Vista 64bit) I encountered this error:

> Could not load file or assembly ChilkatDotNet2 or one of its > dependencies. An attempt was made to load a program with an incorrect > format.

Obviously, the application uses ChilKat components, but it would seem that the version we are using, is only the 32bit version.

To resolve this error, I set my app pool in IIS to allow 32bit applications. Open up IIS Manager, right click on the app pool, and select Advanced Settings (See below)

enter image description here

Then set "Enable 32-bit Applications" to True.

enter image description here

All done!

Solution 2 - C#

I've found the solution. I've recently upgraded my machine to Windows 2008 Server 64-bit. The SqlServer.Replication namespace was written for 32-bit platforms. All I needed to do to get it running again was to set the Target Platform in the Project Build Properties to X86.

Solution 3 - C#

Change the value for Platform Target on your web project's property page to Any CPU.

enter image description here

Solution 4 - C#

Go to IIS -> Application Pool -> Advance Settings -> Enable 32-bit Applications

Solution 5 - C#

We recently had the issue when trying to run the code from Visual Studio. In that case you need to do
TOOLS > OPTIONS > Projects and Solutions > WEB PROJECTS and check the "Use the 64 bit version of IIS Express for web sites and projects".

Solution 6 - C#

For those who get this error in an ASP.NET MVC 3 project, within Visual Studio itself:

In an ASP.NET MVC 3 app I'm working on, I tried adding a reference to Microsoft.SqlServer.BatchParser to a project to resolve a problem where it was missing on a deployment server. (Our app uses SMO; the correct fix was to install SQL Server Native Client and a couple other things on the deployment server.)

Even after I removed the reference to BatchParser, I kept getting the "An attempt was made..." error, referencing the BatchParser DLL, on every ASP.NET MVC 3 page I opened, and that error was followed by dozens of page parsing errors.

If this happens to you, do a file search and see if the DLL is still in one of your project's \bin folders. Even if you do a rebuild, Visual Studio doesn't necessarily clear out everything in all your \bin folders. When I deleted the DLL from the bin and built again, the error went away.

Solution 7 - C#

I had this in an MVC5 app in Windows 10 against IIS Express. My solution was the following:

  • Tools =>
  • Options =>
    • Projects and Solutions =>
      • Web Projects =>
        • Use the 64 bit version of IIS Express for web sites and projects

Solution 8 - C#

change it to 32-bit (true) it works

if you get this Length cannot be less than zero. Parameter name: length issue in iis server configuation do the simple thing change the connection string in web.config file like your sql server name and server name and restart iis then try to load the page it works

Solution 9 - C#

If Publishing in Visual Studio 2012 when erroring try unchecking the "Procompile during publishing" option in the Publish wizard.

Un-check

Solution 10 - C#

in windows form application I do this, Right-click on Project->Properties->Build->Check Prefer 32-bit checkbox. Thanks all

Solution 11 - C#

  1. Delete the temp test directory located here C:\Users(User)\AppData\Local\Temp\VisualStudioTestExplorerExtensions\

  2. Set all projects to x64 in Visual Studio

  3. Set the default processor architecture to x64(Test/TestSettings/Default Processor Architecture).

Make sure to clean build the solution file.Hope this helps!

Solution 12 - C#

Changing the Target Framework in project properties from .NET Framework 4.7.1 to 4.6.2 worked for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbaldyView Question on Stackoverflow
Solution 1 - C#bboyle1234View Answer on Stackoverflow
Solution 2 - C#baldyView Answer on Stackoverflow
Solution 3 - C#Mazhar KarimiView Answer on Stackoverflow
Solution 4 - C#HarpalView Answer on Stackoverflow
Solution 5 - C#JustinView Answer on Stackoverflow
Solution 6 - C#Ryan LundyView Answer on Stackoverflow
Solution 7 - C#krillgarView Answer on Stackoverflow
Solution 8 - C#munwar shaiikView Answer on Stackoverflow
Solution 9 - C#SushiGuyView Answer on Stackoverflow
Solution 10 - C#Amirhossein YariView Answer on Stackoverflow
Solution 11 - C#Madhurya GandiView Answer on Stackoverflow
Solution 12 - C#sussexrickView Answer on Stackoverflow