2011-01-31 65 views
0

我通過DataAdapter1.Update()方法將新行插入到數據集dsStaff1中。 我收到以下錯誤:插入新行時數據集更新失敗

Cannot insert the value NULL into column 'Enabled', table 'dbo.tblStaff'; column does not allow nulls. INSERT fails. The statement has been terminated.

我有一個布爾字段在tblStaff「已啓用」,這是關鍵字在SQL Server中,我想。它默認爲'真'。我無法真正改變這個領域的名字。有沒有解決辦法?或者我做錯了什麼? PS:我正在通過sqlcommand生成器生成插入,更新命令。

回答

1

如果您有默認值,那麼您的SQL命令構建器必須發送NULL(DBNull.Value)來覆蓋默認值。默認將僅適用,如果:

  • ,如果你沒有在INSERT指定的列值
  • ,或者如果您使用關鍵字DEFAULT

其中一個2種形式:

--Enabled is not mentioned, will use default 
INSERT (col1, col1) VALUES (col1, col2) 

--Enabled will use DEFAULT 
INSERT (col1, col1, Enabled) VALUES (col1, col2, DEFAULT) 
+0

嗨,謝謝你的回覆。那麼如何解決這個問題呢?我不知道插入命令是什麼,因爲它是自動生成的。我是否需要使用Sqlcommand參數顯式編寫插入命令?我在底部粘貼了我的代碼。它在更新時失敗 – hrishi 2011-02-01 02:11:11

0

只是通過一個固定值到數據集的更新參數

<asp:Parameter Name="col1" Type="boolean" DefaultValue="true"/> 
0

我的代碼:它在更新時失敗。

 Dim dr As System.Data.DataRow 

     dr = DsStaff1.Tables(0).NewRow() 

     dr.BeginEdit() 
     dr.Item("FirstName") = txtName.Text 
     dr.Item("LastName") = txtSurname.Text 

     dr.Item("StaffID") = txtStaffID.Text 
     dr.Item("Enabled") = cbActive.Checked 
     dr.EndEdit() 
     DsStaff1.Tables(0).Rows.Add(dr) 

     DataAdapter1.Update(DsStaff1) 

我也是用使用TableMappings在DataAdapter1

 DataAdapter1.TableMappings.Add("Table", DsStaff1.Tables("tblStaff").ToString) 

附:更新和刪除現有的行可以正常工作。這只是插入一個新行不起作用。