2016-08-27 10 views
0

データベースに変更を保存しようとすると問題が発生します。私のコードをチェック例外c#datagridview update値をnullにすることはできません

値がnull

することはできませんgeting午前は

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

を何が起こっているのかSHUREておりませんのStackTrace

(データテーブルのdataTable)System.Data.Common.DbDataAdapter.Updateで

がMVPLearing.GrupeArtikala.saveToolStripButton_Clickで
(オブジェクト送信者、 のEventArgs e)の Eで:\ Radni \ C#\リサイズ-Ucenje \ MVPLearing \ MVPLearing \ GrupeArtikala .cs: System.Windows.Forms.ToolStripItem.HandleClickでSystem.Windows.Forms.ToolStripButton.OnClick(のEventArgs e)にSystem.Windows.Forms.ToolStripItem.RaiseEvent(オブジェクトキー、 のEventArgs電子)のライン308 (EventArgs e)at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e 、ToolStripItemEventTypeは System.Windows.Forms.Control.WmMouseUp(AT System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgsのMEA)で System.Windows.Forms.ToolStripItem.FireEvent(のEventArgs電子、ToolStripItemEventTypeが満たさ )で)会っ のSystem.Windows.FormsでSystem.Windows.Forms.ScrollableControl.WndProc(メッセージ& M)で System.Windows.Forms.Control.WndProc(メッセージ& M)でのメッセージ& M、MouseButtons ボタンのInt32はクリック) .ToolStrip.WndProc(メッセージ& m) でSystem.Windows.Forms.Control.ControlNativeWindow.OnMessage(メッセージ&m) でSys System.Windows.Forms.NativeWindow.DebuggableCallbackでtem.Windows.Forms.Control.ControlNativeWindow.WndProc(メッセージ& M)(のIntPtr hWndは、MSGのInt32、のIntPtr WPARAM、LPARAMのIntPtr) System.Windows.Forms.UnsafeNativeMethodsで.DispatchMessageW のSystem.Windows.Formsで(MSG & MSG)
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoopで(のIntPtr dwComponentID、のInt32理由、のInt32 pvLoopData) .Application.ThreadContext.RunMessageLoopInner(Int32 理由、ApplicationContextコンテキスト)で System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 理由 EでMVPLearing.Program.Main()で System.Windows.Forms.Application.Run(フォームのMainFormで、ApplicationContextのコンテキスト)):\ Radni \ C#\リサイズ-Ucenje \ MVPLearing \ MVPLearing \のProgram.cs :ライン19
Microsoft.VisualStudio.HostingProcess.HostProcでSystem.AppDomain.ExecuteAssemblyでSystem.AppDomain._nExecuteAssembly(RuntimeAssemblyアセンブリ、 のString []引数)(文字列 assemblyFile、証拠assemblySecurity、文字列[]引数)で。 RunUsersAssembly()
at System.Threading.ThreadHelper。ThreadStart_Context(オブジェクトの状態)System.Threading.ExecutionContext.RunInternalで
(のExecutionContext のExecutionContext、ContextCallbackコールバック、オブジェクト状態、ブール preserveSyncCtx) System.Threading.ExecutionContext.Run(のExecutionContext のExecutionContextに、ContextCallbackコールバック、状態オブジェクト、私はトンの1を想定 System.Threading.ThreadHelper.ThreadStart(で System.Threading.ExecutionContext.Run(のExecutionContext のExecutionContext、ContextCallbackコールバック、オブジェクトの状態)でのブール preserveSyncCtx))

+0

多分ちょうど 'adapter.Update(DT);' – Slai

+0

は、今私が得るすべての例外が、変更は、それは助けるが、 'grupeArtikalaGrid.DataSource = bindingSource1を削除するかコメントアウトするかどうかわからデータベース – Ivan

+0

ないに保存されていないいけない;' – Slai

答えて

0

彼のフィールドは、データテーブル(メモリテーブル)に "not null"とマークされていますが、データベースではnullです。

データテーブルの設定を確認し、データベーステーブルのNULL可能な列からデータを取り込んだすべての列でnullを許可します。

+0

すべてのフィールドは 'ヌル 'のみに設定されていますIDプライマリキーはヌルではなく、AIでもう一度です – Ivan

+0

例外のタイプは何ですか? –

+0

check update!私はスクリーンショットをアップロードします – Ivan

関連する問題