当我尝试保存数据库中的更改时,我遇到了问题。发生异常
值不能为空
检查我的代码是不是真的发生了什么
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);
}
}
}
栈跟踪
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
在MVPLearing.GrupeArtikala.saveToolStripButton_Click(对象发送者, EventArgs e)in E:\ Radni \ C#\的WinForms,Ucenje \ MVPLearing \ MVPLearing \ GrupeArtikala.cs:行 308在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)
在System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs) e,ToolStripItemEventType符合) System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType符合) System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)at at System.Windows.Forms.Control.WmMouseUp(Message& m,MouseButtons 按钮,Int32点击)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) 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr) hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 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 MVPLearing.Program.Main()in E:\ Radni \ C#\ WinForms-Ucenje \ MVPLearing \ MVPLearing \ Program.cs:第19行
在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集, System.AppDomain.ExecuteAssembly上的String [] args)(String assemblyFile,Evidence assemblySecurity,String [] args)at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
在System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔值 preserveSyncCtx)at System.Threading.ExecutionContext.Run(执行上下文 executionContext,ContextCallback回调,对象状态,布尔值 preserveSyncCtx)at System.Threading.ExecutionContext.Run(执行上下文 executionContext,ContextCallback回调,对象状态)at System.Threading.ThreadHelper.ThreadStart()
答案 0 :(得分:0)
我想其中一个字段在数据表(内存表)中标记为“not null”,但在数据库中为空。
检查数据表的设置,并在从数据库表中的可为空的列填充的所有列中允许null。