970827 - 当我调用datarowcollection.add时发生一个未发现的异常 [英] 970827 - an unexcepted exception occurs when I call datarowcollection.add

查看:76
本文介绍了970827 - 当我调用datarowcollection.add时发生一个未发现的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中:

使用System; 
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;
使用System.Data.SQLite;
使用System.Diagnostics;

命名空间WindowsFormsApplication1
{
public partial class List:Form
{
public List()
{
InitializeComponent() ;
}

private void List_Load(object sender,EventArgs e)
{
comboBox1.SelectedIndex = 1;
}

private void comboBox1_SelectedIndexChanged(object sender,EventArgs e)
{
var cmd = new SQLiteCommand(select * from students,DB.Connection);
var ad = new SQLiteDataAdapter(cmd);
var dt = new DataTable();
ad.Fill(dt);
listBox1.DataSource = dt;
listBox1.DisplayMember =name;
}

private void btnAdd_Click(object sender,EventArgs e)
{
Debug.Assert(listBox1.DataSource是DataTable);
var dt = listBox1.DataSource as DataTable;
dt.Rows.Add();
btnEdit.Enabled = btnRemove.Enabled = dt.Rows.Count!= 0;
}

private void btnEdit_Click(object sender,EventArgs e)
{
Debug.Assert(listBox1.SelectedItem is DataRowView);
new StudentsForm2(listBox1.SelectedItem as DataRowView).ShowDialog(this);
}

private void btnRemove_Click(object sender,EventArgs e)
{
Debug.Assert(listBox1.DataSource是DataTable);
var dt = listBox1.DataSource as DataTable;
dt.Rows.RemoveAt(listBox1.SelectedIndex);
btnEdit.Enabled = btnRemove.Enabled = dt.Rows.Count!= 0;
}

private void List_KeyDown(object sender,KeyEventArgs e)
{
if(e.KeyCode == Keys.Escape)
{
关闭();
}
}

}
}



当表为空时,我得到以下异常:

Quote:

System.Windows.Forms中出现'System.ArgumentOutOfRangeException'类型的第一次机会异常。 dll



附加信息:InvalidArgument ='0'的值对'SelectedIndex'无效。

。 comboBox1保存表名,但由于我现在只有一个表,所以除非调用comboBox1_SelectedIndexChanged,否则它不会被使用。



这里是异常点的堆栈跟踪被抛出:

Quote:

在System.Environment.GetStackTrace(例外e,布尔需要文件信息)
System.Environment.get_StackTrace上的
()
System.Windows.Forms.ListBox.set_SelectedIndex(Int32 value)中的

System.Windows.Forms.ListControl中的
.DataManager_PositionChanged(Object sender,EventArgs e)

at System.Windows.Forms.CurrencyManager.OnPositionChanged(EventArgs e)

at System.Windows.Forms.CurrencyManager.ChangeRecordState( Int32 newPosition,Boolean validating,Boolean endCurrentEdit,Boolean firePositionChange,Boolean pullData)

at System.Windows.Forms.CurrencyManager.List_ListChanged(Object sender,ListChangedEventArgs e)

a t System.Data.Data.OnListChanged(ListChangedEventArgs e)

在System.Data.DataView.IndexListChanged(Object sender,ListChangedEventArgs e)

在System.Data.DataView。 IndexListChangedInternal(ListChangedEventArgs e)

at System.Data.DataViewListener.IndexListChanged(ListChangedEventArgs e)

at System.Data.Index。<> c。< onlistchanged> b__88_0(DataViewListener监听器,ListChangedEventArgs args,布尔arg2,布尔arg3)

在System.Data.Listeners`1.Notify [T1,T2,T3](T1 arg1,T2 arg2,T3 arg3,Action `4 action)

at System.Data.Index.OnListChanged(ListChangedEventArgs e)

at System.Data.Index.OnListChanged(ListChangedType changedType,Int32 index)

在System.Data.Index.InsertRecord(Int32记录,布尔值fireEvent)

在System.Data.Index.ApplyChangeAction(Int32记录,Int32动作,Int32 changeRecord)

在System.Data.Index.RecordStateChanged(Int32记录, SystemView.DataTable.RecordStateChanged(Int32 record1,DataViewRowState oldState1,DataViewRowState newState1,Int32 record2,DataViewRowState oldState2,DataViewRowState newState2)
at $ Data $ Home $ DataDataRableState System.Data.DataTable.SetNewRecordWorker(DataRow row,Int32 proposedRecord,DataRowAction action,Boolean isInMerge,Boolean suppressEnsurePropertyChanged,Int32 position,Boolean fireEvent,Exception& deferredException)

在System.Data.DataTable.InsertRow(DataRow行,Int64 proposedID,Int32 pos,Boolean fireEvent)

at System.Data.DataRowCollection.Add(Object [在C:\ Users \ hamidi \Documents\Visual Studio 2010 \Projects\m_rezaee \ WindowsFormsApplication1中的WindowsFormsApplication1.List.btnAdd_Click(Object sender,EventArgs e)中的

\ WindowsFormsApplication1 \List.cs:第44行

在System.Windows.Forms.Control.OnClick(EventArgs e)

在System.Windows.Forms.Button。 OnClick(EventArgs e)

在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

在System.Windows.Forms.Control.WmMouseUp(消息& m, MouseButtons按钮,Int32点击)

在System.Windows.Forms.Control.WndProc(消息& m)

在System.Windows.Forms.ButtonBase.WndProc(消息& m)

在System.Windows.Forms.Button.WndProc(消息& m)
$ b System.Windows.Forms.Control.ControlNativeWindow.OnMessage上的$ b(消息& m)

在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息& m)

在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32) msg,IntPtr wparam,IntPtr lparam)

在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)

在System.Windows.Forms.Application.ComponentManager。 System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData)

在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)

System.Windows.Forms.Application.Run(Form mainForm)的

at WindowsFormsApplication1.Program.Main()in C:\ Users\hamidi \ Documentments \ Visual Studio 2010 \Projects\m_rezaee \ WindowsFormsApplicat在System.AppDomain.ExecuteAssembly的System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args)

处的ion1 \ WindowsFormsApplication1 \Program.cs:第18行

at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

at System.Threading.ThreadHelper.ThreadStart_Context(Object state) )

在System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)

在System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback回调,Object state,Boolean preserveSyncCtx)

在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)

在System.Threading.ThreadHelper。 ThreadStart()





我尝试过:



我找不到任何关于它为什么会发生的线索。

解决方案

你的堆栈跟踪已经提供你有你需要的线索:

C:\ Users \ benidi \ Files \ Visual Studio 2010 \Projects\m_rezaee \ WindowsFormsApplication1 \ WindowsFormsApplication1 \Program.cs:line 18



\ WindowsFormsApplication1 \List.cs:第44行


在Visual Studio 2010中,当调试器被告知在所有异常时停止时,无论是否是否处理异常。例如,如果您编写这样的代码:



 尝试 
{
throw new 异常( test);
}
catch
{
// < span class =code-comment>什么都不做
}



调试器在抛出异常的行停止,即使它被抓住了处理。请注意,如果在不属于我们自己的代码中抛出异常,也会发生这种情况。我们可以调用我们没有编写的类的方法,我们只是使用它们。这些方法可能是以处理异常并做出响应的方式编写的。如果在这种情况下调用整个应用程序,则不会出现异常,因为它是在方法内部处理的。但是如果你通过调试器运行它们,你会看到抛出这些异常。

在这段代码中,似乎发生了这样的事情。我不想从Debug / Exceptions ...菜单中取消选中超出范围的异常。如果我的代码的任何部分表现不佳,我更愿意被告知。但是不应该通知这样的异常,因为它们处理得当。

为此,我需要:

1.检查Debug / Exceptions中的所有异常。 Visual Studio的菜单。

2.选中工具/选项/调试/常规中的启用我的代码(仅管理)选项。

因此,调试器只会停止关于您自己的代码抛出的异常。



PS。我在这里找到了信息和解决方案。


In the following code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class List : Form
    {
        public List()
        {
            InitializeComponent();
        }

        private void List_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 1;
		}

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
			var cmd = new SQLiteCommand("select * from students", DB.Connection);
			var ad = new SQLiteDataAdapter(cmd);
			var dt = new DataTable();
			ad.Fill(dt);
			listBox1.DataSource = dt;
			listBox1.DisplayMember = "name";
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			Debug.Assert(listBox1.DataSource is DataTable);
			var dt = listBox1.DataSource as DataTable;
			dt.Rows.Add();
			btnEdit.Enabled = btnRemove.Enabled = dt.Rows.Count != 0;
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			Debug.Assert(listBox1.SelectedItem is DataRowView);
			new StudentsForm2(listBox1.SelectedItem as DataRowView).ShowDialog(this);
		}

		private void btnRemove_Click(object sender, EventArgs e)
		{
			Debug.Assert(listBox1.DataSource is DataTable);
			var dt = listBox1.DataSource as DataTable;
			dt.Rows.RemoveAt(listBox1.SelectedIndex);
			btnEdit.Enabled = btnRemove.Enabled = dt.Rows.Count != 0;
		}

		private void List_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				Close();
			}
		}

    }
}


when the table is empty, I get the following exception:

Quote:

A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll

Additional information: InvalidArgument=Value of '0' is not valid for 'SelectedIndex'.


in method btnAdd_Click when it calls dt.Rows.Add(). comboBox1 holds tables names, but since I only have one table at the moment it's not used unless for comboBox1_SelectedIndexChanged to be called.

Here's the stack trace at the point the exception is thrown:

Quote:

at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Environment.get_StackTrace()
at System.Windows.Forms.ListBox.set_SelectedIndex(Int32 value)
at System.Windows.Forms.ListControl.DataManager_PositionChanged(Object sender, EventArgs e)
at System.Windows.Forms.CurrencyManager.OnPositionChanged(EventArgs e)
at System.Windows.Forms.CurrencyManager.ChangeRecordState(Int32 newPosition, Boolean validating, Boolean endCurrentEdit, Boolean firePositionChange, Boolean pullData)
at System.Windows.Forms.CurrencyManager.List_ListChanged(Object sender, ListChangedEventArgs e)
at System.Data.DataView.OnListChanged(ListChangedEventArgs e)
at System.Data.DataView.IndexListChanged(Object sender, ListChangedEventArgs e)
at System.Data.DataView.IndexListChangedInternal(ListChangedEventArgs e)
at System.Data.DataViewListener.IndexListChanged(ListChangedEventArgs e)
at System.Data.Index.<>c.<onlistchanged>b__88_0(DataViewListener listener, ListChangedEventArgs args, Boolean arg2, Boolean arg3)
at System.Data.Listeners`1.Notify[T1,T2,T3](T1 arg1, T2 arg2, T3 arg3, Action`4 action)
at System.Data.Index.OnListChanged(ListChangedEventArgs e)
at System.Data.Index.OnListChanged(ListChangedType changedType, Int32 index)
at System.Data.Index.InsertRecord(Int32 record, Boolean fireEvent)
at System.Data.Index.ApplyChangeAction(Int32 record, Int32 action, Int32 changeRecord)
at System.Data.Index.RecordStateChanged(Int32 record, DataViewRowState oldState, DataViewRowState newState)
at System.Data.DataTable.RecordStateChanged(Int32 record1, DataViewRowState oldState1, DataViewRowState newState1, Int32 record2, DataViewRowState oldState2, DataViewRowState newState2)
at System.Data.DataTable.SetNewRecordWorker(DataRow row, Int32 proposedRecord, DataRowAction action, Boolean isInMerge, Boolean suppressEnsurePropertyChanged, Int32 position, Boolean fireEvent, Exception& deferredException)
at System.Data.DataTable.InsertRow(DataRow row, Int64 proposedID, Int32 pos, Boolean fireEvent)
at System.Data.DataRowCollection.Add(Object[] values)
at WindowsFormsApplication1.List.btnAdd_Click(Object sender, EventArgs e) in C:\Users\hamidi\Documents\Visual Studio 2010\Projects\m_rezaee\WindowsFormsApplication1\WindowsFormsApplication1\List.cs:line 44
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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsFormsApplication1.Program.Main() in C:\Users\hamidi\Documents\Visual Studio 2010\Projects\m_rezaee\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()



What I have tried:

I couldn't find any clue about why it happens.

解决方案

Your stack trace has already provided you with the clues you need:
C:\Users\hamidi\Documents\Visual Studio 2010\Projects\m_rezaee\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 18
and
\WindowsFormsApplication1\List.cs:line 44
Go look at what is happening there. stick in a break point at those line and inspect the objects and variables used there


In Visual Studio 2010, when the debugger is told to stop at all exceptions, it does it regardless of whether the exception is handled or not. For example, if you write such a code:

try
{
  throw new Exception("test");
}
catch
{
  // do nothing
}


the debugger stops at the line which throws exception, even if it's caught and handled. Note that this also happens if an exception is thrown inside the codes which don't belong to our own. We may call methods of classes we're not written and we just use them. The methods may have been written in a way that handle exceptions and do something in response. If you call the whole application in this case, you won't get an exception, because it's handled inside the methods. But if you run them via the debugger, you see that these exceptions are thrown.
In this code, it seems that such a thing is happened. I prefer not to uncheck the out of range exception from the Debug/Exceptions... menu. I prefer to be informed if any part of my code is not performing well. But such an exceptions should not be informed, because they're properly handled.
For this, I need to:
1. check all exceptions in Debug/Exceptions... menu of Visual Studio.
2. check the option Enable Just My Code (Managed only) in Tools/Options/Debugging/General.
So, debugger will only stops on exceptions which are thrown by your own code.

PS. I found the info and solution here.


这篇关于970827 - 当我调用datarowcollection.add时发生一个未发现的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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