【发布时间】:2015-02-10 19:14:49
【问题描述】:
我试图通过传递相应的文本、在列表框中查找它并删除该项目来从列表框中删除特定值。使用此代码:
private void UpdateGUIAfterTableSend(String listboxVal)
{
ExceptionLoggingService.Instance.WriteLog("Reached frmMain.UpdateGUIAfterTableSend");
try
{
listBoxWork.DataSource = null; // <= It seems necessary to set this to null to circumvent "Value does not fall within the expected range"
for (int i = listBoxWork.Items.Count - 1; i >= 0; --i)
{
if (listBoxWork.Items[i].ToString().Contains(listboxVal))
{
listBoxWork.Items.RemoveAt(i);
}
}
}
catch (Exception ex)
{
String msgInnerExAndStackTrace = String.Format("{0}; Inner Ex: {1}; Stack Trace: {2}", ex.Message, ex.InnerException, ex.StackTrace);
ExceptionLoggingService.Instance.WriteLog(String.Format("From frmMain.UpdateGUIAfterTableSend: {0}", msgInnerExAndStackTrace));
}
}
...但是,它失败了;问题记录为:
Date: 2/10/2015 1:48:07 PM
Message: Reached frmMain.UpdateGUIAfterTableSend
Date: 2/10/2015 1:48:07 PM
Message: From application-wide exception handler: System.InvalidOperationException: InvalidOperationException
at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()
at HHS.frmMain.SendDeliveries()
at HHS.frmMain.menuItemSEND_Deliveries_Click(Object sender, EventArgs e)
at System.Windows.Forms.MenuItem.OnClick(EventArgs e)
at System.Windows.Forms.Menu.ProcessMnuProc(Control ctlThis, WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Form.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
at System.Windows.Forms.Application.Run(Form fm)
at HHS.Program.Main()
请注意,记录异常的是“应用程序范围的异常处理程序”,而不是 UpdateGUIAfterTableSend() 中的 catch 块
列表框通过设置它的数据源来填充,如下所示:
listBoxWork.DataSource = workTables;
workTables 是由查询填充的字符串列表:
List<String> workTables = hhsdbutils.GetWorkTableNames();
我对导致问题的原因(及其解决方案)以及为什么 UpdateGUIAfterTableSend() 中的 catch 块没有记录正在引发的异常感到困惑。
更新
在史蒂夫发表评论后,我将数据源的设置取消注释为空。那么例外是:
Date: 2/10/2015 2:19:58 PM
Message: From frmMain.UpdateGUIAfterTableSend: Value does not fall within the expected range.; Inner Ex: ; Stack Trace: at System.Windows.Forms.ListBox._VerifyNoDataSource()
at System.Windows.Forms.ListBox.ObjectCollection.RemoveAt(Int32 index)
at HHS.frmMain.UpdateGUIAfterTableSend(String listboxVal)
更新 2
对于阿迪·莱斯特:
private void SendDeliveries()
{
ExceptionLoggingService .Instance.WriteLog("Reached frmMain.SendDeliveries");
Cursor curse = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
try
{
bool firstRecord = false;
bool lastRecord = false;
foreach (String tblname in listBoxWork.Items)
{
// Ignore INV tables
if (tblname.IndexOf("INV") == 0) continue;
String tblSiteNum = hhsdbutils.GetSiteNumForTableName(tblname);
String fileName = HHSUtils.GetGeneratedDSDFileName(tblSiteNum);
String xmlData = hhsdbutils.GetDSDDataAsXMLFromTable(tblname, fileName);
String uri = String.Format("{0}delivery/sendXML/duckbill/platypus/{1}", HHSConsts.BASE_REST_URL, fileName);
fileXferImp = HHSConsts.GetFileTransferMethodology();
fileXferImp.SendDataContentsAsXML(uri, xmlData, tblname, siteNum, firstRecord, lastRecord);
String tableRefVal = HHSUtils.GetTableRefValForTableName(tblname, "DSD");
hhsdbutils.DeleteTableReference(tableRefVal, "DSD");
hhsdbutils.DropTable(tblname, tblSiteNum); // <- Will actually do this only after creating replacement tables in code
UpdateGUIAfterTableSend(tblname);
}
}
finally
{
Cursor.Current = curse;
}
}
【问题讨论】:
-
异常来自
SendDeliveries- 显示该方法的代码。 -
已评论;在我这样做之前它就爆炸了(现在它又爆炸了,所以我不知道......我会再次测试它,只为 Nils Lofgrins 注释掉它)
-
如果您将 DataSource 设置为 null,那么据我所知,ListBox 中不会有任何项目。
-
向我们展示引发异常的代码。
-
@SriramSakthivel:不,他们还留在那里——这就是问题所在;应该删除的没有被删除。
标签: c# arraylist invalidoperationexception