Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

class

static

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) 
like image 881
Vytas Avatar asked Aug 04 '09 07:08

Vytas


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

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?

like image 157
Anderson Imes Avatar answered Sep 21 '22 06:09

Anderson Imes


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.

like image 36
Kevin Morwood Avatar answered Sep 22 '22 06:09

Kevin Morwood