[c#] C# Error "The type initializer for ... threw an exception

This error occurs only in some computers. By reading the stack information, there is some problem when I call to this static method ("FormatQuery") in a static class:

using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlexCel.Report;
using FlexCel.XlsAdapter;
using ComboBox=System.Windows.Forms.ComboBox;

namespace XSoftArt.A
{
    static class RHelper
    {
        private static string FormatQuery(string FieldName, int Count,
            CheckedListBox chekedListBox)
        {
            string ID = string.Empty;
            int n = Count;

            foreach (DataRowView item in chekedListBox.CheckedItems)
            {
                ID = ID + item["" + FieldName + ""];
                if (n > 1)
                {
                    ID = ID + " , ";
                    n--;
                }
            }
            return ID;
        }

        public static string FormatQuery(CheckedListBox chekedListBox)
        {
            return FormatQuery(chekedListBox.ValueMember,
                chekedListBox.CheckedItems.Count, chekedListBox);
        }
    }

So, what's the problem? How do I solve it? Is there something wrong with the project configuration or debbuging mode or what?

Error information:

   at XSoftArt.EVS.ReportHelper.FormatQuery(CheckedListBox chekedListBox)
   at XSoftArt.EVS.NewEmailSelectClient.LoadList_v2(String search, TextBox txtbox)
   at XSoftArt.EVS.NewEmailSelectClient.LoadContacts()
   at XSoftArt.EVS.NewEmailSelectClient.button7_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

This question is related to c# class static

The answer is


I had the same error but in my case it was caused by mismatch in platform target settings. One library was set specifically to x86 while the main application was set to 'Any'...and then I moved my development to an x64 laptop.


A Type Initializer exception indicates that the type couldn't be created. This would occur typically right before your call to your method when you simply reference that class.

Is the code you have here the complete text of your type? I would be looking for something like an assignment to fail. I see this a lot with getting app settings and things of that nature.

static class RHelper
{
     //If this line of code failed, you'd get this error
     static string mySetting = Settings.MySetting;
} 

You can also see this with static constructors for types.

In any case, is there any more to this class?


If you have web services, check your URL pointing to the service. I had a simular issue which was fixed when I changed my web service URL.


I had this problem and like Anderson Imes said it had to do with app settings. My problem was the scope of one of my settings was set to "User" when it should have been "Application".


I got this error when I modified an Nlog configuration file and didn't format the XML correctly.


This can be caused by not having administrator permissions for Oracle Client. Add this in App.config file:

<IPermission class="Oracle.DataAccess.Client.OraclePermission,
 Oracle.DataAccess, Version=2.111.7.20, Culture=neutral,
 PublicKeyToken=89b483f429c47342" version= "1" Unrestricted="true"/>

This error was generated for me by having an incorrectly formatted NLog.config file.


This problem can occur if a class tries to get value of a non-existent key in web.config.

For example, the class has a static variable ClientID

private static string ClientID = System.Configuration.ConfigurationSettings.AppSettings["GoogleCalendarApplicationClientID"].ToString();

but the web.config doesn't contain the 'GoogleCalendarApplicationClientID' key, then the error will be thrown on any static function call or any class instance creation


I got this error when trying to log to an NLog target that no longer existed.


I got this error with my own code. My problem was that I had duplicate keys in the config file.


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to class

String method cannot be found in a main class method Class constructor type in typescript? ReactJS - Call One Component Method From Another Component How do I declare a model class in my Angular 2 component using TypeScript? When to use Interface and Model in TypeScript / Angular Swift Error: Editor placeholder in source file Declaring static constants in ES6 classes? Creating a static class with no instances In R, dealing with Error: ggplot2 doesn't know how to deal with data of class numeric Static vs class functions/variables in Swift classes?

Examples related to static

What is the equivalent of Java static methods in Kotlin? Creating a static class with no instances Static vs class functions/variables in Swift classes? Call static methods from regular ES6 class methods What is the difference between static func and class func in Swift? An object reference is required to access a non-static member Mocking static methods with Mockito @Autowired and static method The static keyword and its various uses in C++ Non-Static method cannot be referenced from a static context with methods and variables