仅在某些计算机中出现此错误.通过读取堆栈信息,当我在静态类中调用此静态方法("FormatQuery")时会出现一些问题:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
所以有什么问题?我该如何解决?项目配置或重新布局模式有什么问题或者是什么?
错误信息:
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)
Run Code Online (Sandbox Code Playgroud)
And*_*mes 61
Type Initializer异常表示无法创建类型.当您只是引用该类时,通常会在调用方法之前发生这种情况.
这里的代码是您的类型的完整文本吗?我会寻找类似失败的任务.获得应用程序设置和这种性质的东西我看到了很多.
static class RHelper
{
//If this line of code failed, you'd get this error
static string mySetting = Settings.MySetting;
}
Run Code Online (Sandbox Code Playgroud)
您还可以使用类型的静态构造函数来查看此内容.
无论如何,这堂课还有吗?
如果一个类尝试获取web.config.
例如,该类有一个静态变量 ClientID
private static string ClientID = System.Configuration.ConfigurationSettings.AppSettings["GoogleCalendarApplicationClientID"].ToString();
Run Code Online (Sandbox Code Playgroud)
但web.config不包含“GoogleCalendarApplicationClientID”键,则任何静态函数调用或任何类实例创建都会引发错误
我尝试了你的代码:
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();
Run Code Online (Sandbox Code Playgroud)
它在这一行引发了异常:
foreach (DataRowView item in chekedListBox.CheckedItems)
// Unable to cast object of type 'System.String' to type 'System.Data.DataRowView'.
Run Code Online (Sandbox Code Playgroud)
也许你也面临着同样的问题。不要转换为DataRowView,而是尝试进行以下更改:
foreach (var item in chekedListBox.CheckedItems)
{
ID = ID + item.ToString(); // item["" + FieldName + ""];
Run Code Online (Sandbox Code Playgroud)
因为 CheckedListBox 中的项目是对象类型。