我试图通过传递相应的文本,在列表框中查找并删除该项,从列表框中删除特定值。使用此代码:
private void UpdateGUIAfterTableSend(String listboxVal)
{
ExceptionLoggingService.Instance.WriteLog("Reached frmMain.UpdateGUIAfterTableSend");
try
{
listBoxWork.DataSource = null;
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块为什么没有记录正在抛出的异常。
更新
在Steve的评论之后,我取消了将数据源设置为null的注释。则例外情况是:
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
对于Adi Lester:
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)
{
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);
UpdateGUIAfterTableSend(tblname);
}
}
finally
{
Cursor.Current = curse;
}
}