【发布时间】:2017-01-04 06:59:36
【问题描述】:
当我尝试在数据库中保存更改时遇到问题。遇到异常
值不能为空
检查我的代码我不确定发生了什么
public class GrupeArtikala : Form, IGrupeArtikalaView
{
#region Properties
private DataTable dt = new DataTable();
private DataSet ds = new DataSet();
private BindingSource bindingSource1 = new BindingSource();
private MySqlConnection conn;
private MySqlDataAdapter adapter;
#endregion
private void GrupeArtikala_Load(object sender, EventArgs e)
{
grupeArtikalaGrid.DataSource = bindingSource1;
GetData("SELECT * FROM grupe");
}
private void saveToolStripButton_Click(object sender, EventArgs e)
{
// Update the database with the user's changes.
adapter.Update((DataTable)bindingSource1.DataSource);
}
private void GetData(string selectCommand)
{
try
{
conn = new MySqlConnection(Properties.Settings.Default.ConnectionString);
adapter = new MySqlDataAdapter(selectCommand, conn);
// Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand. These are used to
// update the database.
MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(adapter);
// Populate a new data table and bind it to the BindingSource.
dt = new DataTable();
dt.Locale = System.Globalization.CultureInfo.InvariantCulture;
adapter.Fill(dt);
grupeArtikalaGrid.DataSource = dt;
// Resize the DataGridView columns to fit the newly loaded content.
grupeArtikalaGrid.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
}
堆栈跟踪
在 System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
在 MVPLearing.GrupeArtikala.saveToolStripButton_Click(对象发送者, EventArgs e) 在 E:\Radni\C#\WinForms-Ucenje\MVPLearing\MVPLearing\GrupeArtikala.cs:line 308 在 System.Windows.Forms.ToolStripItem.RaiseEvent(对象键, EventArgs e) 在 System.Windows.Forms.ToolStripButton.OnClick(EventArgs e) 在 System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e) 在 System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
在 System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e、ToolStripItemEventType)在 System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType 遇到)在 System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) 在 System.Windows.Forms.Control.WmMouseUp(消息和 m,鼠标按钮 按钮,Int32 点击)在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ScrollableControl.WndProc(Message& m) 在 System.Windows.Forms.ToolStrip.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& 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(味精和味精)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 原因,Int32 pvLoopData)在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文)在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文)在 System.Windows.Forms.Application.Run(Form mainForm) 在 MVPLearing.Program.Main() 在 E:\Radni\C#\WinForms-Ucenje\MVPLearing\MVPLearing\Program.cs:第 19 行
在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集, System.AppDomain.ExecuteAssembly(String) 处的 String[] args) 装配文件,证据装配安全,字符串 [] 参数)在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态)在 System.Threading.ThreadHelper.ThreadStart()
【问题讨论】:
-
也许只是
adapter.Update(dt); -
现在我没有得到任何异常,但更改没有保存到数据库中
-
不确定是否有帮助,但删除或注释掉
grupeArtikalaGrid.DataSource = bindingSource1; -
您之前遇到过错误,因为您从未将
bindingSource1.DataSource设置为任何内容。例如bindingSource1.DataSource = dt; -
我在
GetData方法中设置了bindingSource1.DataSource = dt;,然后又得到了一些
标签: c# .net datagridview