我有一个Windows窗体项目,除了设计人员创建的“保存”按钮以外,其他所有项目都运行良好。保存项目单击事件中的最后一条语句失败。这段代码是由设计人员生成的,我并不完全理解。有谁知道导致错误的原因吗?我希望它在导航记录时自动保存,并在可能的情况下使按钮成为单个记录。
private void cUSTOMERBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.cUSTOMERBindingSource.EndEdit();
dbCon = new SqlConnection(dbConString);
SqlDataAdapter dbAdapter = new SqlDataAdapter();
dbAdapter.SelectCommand = new SqlCommand("Select * From Customers");
SqlCommandBuilder dbBldr = new SqlCommandBuilder();
dbBldr.DataAdapter = dbAdapter;
this.tableAdapterManager.UpdateAll(this.bML_WMS245GDataSet);
}
异常详细信息和完整代码如下:
System.InvalidOperationException was unhandled
HResult=-2146233079
Message=Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
Source=Ballmill
StackTrace:
at Ballmill.BML_WMS245GDataSetTableAdapters.TableAdapterManager.UpdateAll(BML_WMS245GDataSet dataSet) in C:\Users\Jerry\documents\visual studio 2015\Projects\Ballmill\Ballmill\BML_WMS245GDataSet.Designer.cs:line 2102
at Ballmill.Customer.cUSTOMERBindingNavigatorSaveItem_Click(Object sender, EventArgs e) in C:\Users\Jerry\documents\visual studio 2015\Projects\Ballmill\Ballmill\Customer.cs:line 29
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.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 Ballmill.Program.Main() in C:\Users\Jerry\documents\visual studio 2015\Projects\Ballmill\Ballmill\Program.cs:line 19
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()
InnerException:
public partial class Customer : Form
{
public string dbConString = "Data Source=localhost\\BALLMILL;Initial Catalog=Ballmill;Integrated Security=True";
public SqlConnection dbCon = null;
public SqlDataReader dbRdr = null;
public SqlCommand dbCommand = null;
public Customer()
{
InitializeComponent();
}
private void cUSTOMERBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.cUSTOMERBindingSource.EndEdit();
dbCon = new SqlConnection(dbConString);
SqlDataAdapter dbAdapter = new SqlDataAdapter();
dbAdapter.SelectCommand = new SqlCommand("Select * From Customers");
SqlCommandBuilder dbBldr = new SqlCommandBuilder();
dbBldr.DataAdapter = dbAdapter;
this.tableAdapterManager.UpdateAll(this.bML_WMS245GDataSet);
}
private void Customer_Load(object sender, EventArgs e)
{
this.cUSTOMERTableAdapter.Fill(this.bML_WMS245GDataSet.CUSTOMER);
SqlConnection dbCon = new SqlConnection(dbConString);
SqlDataReader rdrCustomers = null;
try
{
dbCon.Open();
SqlCommand sqlCustomers = new SqlCommand("SELECT CustomerCode FROM Customer", dbCon);
rdrCustomers = sqlCustomers.ExecuteReader();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error accessing database");
return;
}
while (rdrCustomers.Read())
{
listCustomers.Items.Add(rdrCustomers["CustomerCode"].ToString());
}
}
private void listCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
cUSTOMERBindingSource.Position = listCustomers.SelectedIndex;
}
}