I have a windows forms project that is working well except for the save button that was created by the designer. The last statement in the save item click event fails. This code was generated by the designer and I don't totally understand it. Does anyone have an idea what is causing the error? I would preferably like it to auto save when navigating off the record and make the button a single record save if that is possible.
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);
}
The exception detail and the full code is below:
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;
}
}