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

181,914

Solution 1

I tried your code:

CheckedListBox cb = new CheckedListBox();
for (var i = 1; i < 11; i++)
  cb.Items.Add("Item " + i, i % 3 == 0);

string fmt = RHelper.FormatQuery(cb);
Console.WriteLine(fmt);
Console.ReadLine();

It threw an exception at this line:

foreach (DataRowView item in chekedListBox.CheckedItems)

// Unable to cast object of type 'System.String' to type 'System.Data.DataRowView'.

Maybe you are also facing the same kind of problem. Instead of casting to DataRowView, try making the following changes:

foreach (var item in chekedListBox.CheckedItems)
{
    ID = ID + item.ToString(); // item["" + FieldName + ""];

Because items in CheckedListBox are of object type.

Solution 2

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?

Solution 3

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.

Solution 4

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

Solution 5

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

Share:
181,914
Vytas
Author by

Vytas

Updated on August 16, 2020

Comments

  • Vytas
    Vytas almost 4 years

    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)
    
  • Vytas
    Vytas almost 15 years
    I dont think so, because this code worked well in another project. I think this may be some other problems (I Copied this module from another project to this...). The cal to this method look like this(when I put arguments for SQL):"@StateID", ReportHelper.FormatQuery(db_InvoiceStateID). db_InvoiceStateID here is a checkedListBox component.
  • Mike
    Mike over 14 years
    This is exactly the problem I had - valuing the static members doesn't happen until after the constructor is invoked. Thanks!
  • BG100
    BG100 over 12 years
    Genius answer... this was exactly my problem as well. I found it very hard to debug this as the exception was masking the real error. Thanks for your help.
  • vapcguy
    vapcguy over 7 years
    And so where did you go to change this? Details, man, details!