0
votes

I have problem when i try to save changes in database. Am geting exception

Value cannot be null

Check my code am not shure what is happening

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);
        }
    }
}

enter image description here

StackTrace

at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
at MVPLearing.GrupeArtikala.saveToolStripButton_Click(Object sender, EventArgs e) in E:\Radni\C#\WinForms-Ucenje\MVPLearing\MVPLearing\GrupeArtikala.cs:line 308 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 MVPLearing.Program.Main() in E:\Radni\C#\WinForms-Ucenje\MVPLearing\MVPLearing\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()

1
maybe just adapter.Update(dt);Slai
Now i dont get any exception but changes is not saved to databaseIvan
not sure if it will help, but remove or comment out grupeArtikalaGrid.DataSource = bindingSource1;Slai
You were getting the error before because you never set bindingSource1.DataSource to anything. For example bindingSource1.DataSource = dt;Slai
I set bindingSource1.DataSource = dt; inside GetData method and again am getting the someIvan

1 Answers

0
votes

I suppose one of the fields is marked as "not null" in the data table (memory table), but it is null in the database.

Check the setup of your data table and allow null in all columns that are populated from nullable columns in the database table(s).