2012-09-20 65 views
1

運行.NET 4.0版時,我試圖插入到使用SqlDataSource.Insert()的表中我得到「無法將值NULL插入列'firstName'...」ASP.NET - 」無法將值NULL插入列'firstName'。「。」當試圖執行SQL INSERT

該標記是由sql嚮導生成的,除了我添加的「DefaultValue =」僅用於測試。 因此,使用DefaultValue =就地,INSERT可以工作,但刪除後我會收到錯誤消息。 SQL服務器抱怨的列是表的PK,因此不能爲空,但運行調試器時,在調用SqlDataSource.Insert()之前填充字段。

任何想法我做錯了什麼?

下面是標記: -

<h2> 
    <asp:Label ID="PageTitle" runat="server"></asp:Label> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
     DeleteCommand="DELETE FROM [profiles] WHERE [firstName] = @firstName" 
     InsertCommand="INSERT INTO [profiles] ([firstName], [lastName], [dateOfBirth], [email]) VALUES (@firstName, @lastName, @dateOfBirth, @email)" 
     SelectCommand="SELECT * FROM [profiles] WHERE ([firstName] = @firstName)" 
     UpdateCommand="UPDATE [profiles] SET [lastName] = @lastName, [dateOfBirth] = @dateOfBirth, [email] = @email WHERE [firstName] = @firstName"> 
     <DeleteParameters> 
      <asp:Parameter Name="firstName" Type="String" /> 
     </DeleteParameters> 
     <InsertParameters> 
      <asp:Parameter Name="firstName" Type="String" DefaultValue="Bri"/> 
      <asp:Parameter Name="lastName" Type="String" DefaultValue="Moss"/> 
      <asp:Parameter DbType="Date" Name="dateOfBirth" DefaultValue="13/04/1961"/> 
      <asp:Parameter Name="email" Type="String" DefaultValue="[email protected]"/> 
     </InsertParameters> 
     <SelectParameters> 
      <asp:Parameter DefaultValue="Ken" Name="firstName" Type="String" /> 
     </SelectParameters> 
     <UpdateParameters> 
      <asp:Parameter Name="lastName" Type="String" /> 
      <asp:Parameter DbType="Date" Name="dateOfBirth" /> 
      <asp:Parameter Name="email" Type="String" /> 
      <asp:Parameter Name="firstName" Type="String" /> 
     </UpdateParameters> 
    </asp:SqlDataSource> 
</h2> 

這裏是調用SqlDataSource.Insert()。 。 。

protected void ProfileSubmit_Click(object sender, EventArgs e) 
{ 
    Page.Validate(); 
    if (Page.IsValid) 
    { 
     SqlDataSource1.Insert(); 
    } 
} 

以下是錯誤信息: -

Server Error in '/' Application. 
-------------------------------------------------------------------------------- 
Cannot insert the value NULL into column 'firstName', table 'C:\USERS\KATHARINA\DOCUMENTS\VISUAL STUDIO 2010\PROJECTS\HEALTHTRACK\HEALTHTRACK\APP_DATA\ASPNETDB.MDF.dbo.profiles'; column does not allow nulls. INSERT fails. 
The statement has been terminated. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'firstName', table 'C:\USERS\KATHARINA\DOCUMENTS\VISUAL STUDIO 2010\PROJECTS\HEALTHTRACK\HEALTHTRACK\APP_DATA\ASPNETDB.MDF.dbo.profiles'; column does not allow nulls. INSERT fails. 
The statement has been terminated. 
Source Error: 
Line 21:    if (Page.IsValid) 
Line 22:    { 
Line 23:     SqlDataSource1.Insert(); 
Line 24:    } 
Line 25:   } 
+0

在表中,您可能已經給列「firstName」賦予非null約束。如果您有SqlServerManagementStudio嘗試連接數據庫,並使用數據(您正在使用此示例提供的)激發插入查詢,並查看結果。 – 2012-09-20 14:27:28

回答

-1

長滿青苔的:它不是連接字符串的問題,如果這將是你的數據庫不會在首位已連接。 它連接到數據庫,這就是爲什麼錯誤是由於它無法將空值插入到表中造成的。 在您的數據庫表中允許爲空的顏色名字

+0

嗨dpk_0702,同意如果連接字符串錯誤,連接首先不會成功。但是我不能讓「firstName」列爲空,因爲它是表的主鍵,加上表單文本框(也是「firstName」)當我嘗試執行INSERT時,有一個值。儘管感謝您的建議。 – mossy

+1

我覺得在這裏我們遇到了問題。像firstname這樣的字段不應該是主鍵。主鍵有幾個規則(查看一些關於關係數據庫的理論書籍)。其中一個是:除了作爲主鍵以外,主鍵應該沒有其他含義。因此,我會創建一個名爲'id'的新字段,它可以得到一個簡單的id。在此之後,您可以使字段的名字爲空。 – reto

+0

「asp.net數據教程」的一個簡短的谷歌帶領我到這邊:http://www.asp.net/web-forms/tutorials/data-access/editing,-inserting,-and-deleting-data/ an-overview-of-insertion-updating-and-deletion-data-cs – reto

0

綁定到(插入)參數的字段是什麼?如果您保留DefaultValue,則表示它有效,那意味着firstname的參數爲空或未解決,因此正在使用DefaultValue中的值。相反,如果您不提供DefaultValue參數爲空/未定義,則插入將失敗(因爲db列限制不允許爲空)。

MSDN Sample

H個

+0

嗨EdSF,綁定到插入參數的字段也被稱爲「名字」。我猜INSERT命令中的「@firstName」是指這個表單上的這個字段。我完全同意你的分析,但是我不明白爲什麼這個字段在作爲INSERT命令的一部分使用時是NULL,因爲我在調試器中立即查看了「SqlDataSource1」之前的「firstName.Text」的值。插();」 。 。 。看看你現在提供的例子,謝謝你的建議。 – mossy

相關問題