C#错误“的类型初始值设置...抛出异常 [英] C# Error "The type initializer for ... threw an exception

查看:288
本文介绍了C#错误“的类型初始值设置...抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此错误仅在某些计算机上发生。通过读取堆栈信息,当我在静态类中调用此静态方法(FormatQuery)时会出现一些问题:

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?

错误信息:

   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)


推荐答案

我尝试过您的代码:

   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();

在此行中抛出异常:

   foreach (DataRowView item in chekedListBox.CheckedItems)

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

也许你也面临同样的问题。而不是转换到 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 + ""];

因为CheckedListBox中的项目是 object 类型。

Because items in CheckedListBox are of object type.

这篇关于C#错误“的类型初始值设置...抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆