1

Running .NET Version 4.0, I'm trying to INSERT into a table using the SqlDataSource.Insert() I get "Cannot insert the value NULL into column 'firstName' . . ."

The markup was generated by the sql wizard, except for the "DefaultValue=" which I added just to test. So with the DefaultValue= in place, the INSERT works, but with this removed I get the error message. The column that SQL server is complaining about is the PK for the table so cannot be nullable, but running the debugger, the field IS populated prior to the call to SqlDataSource.Insert().

Any ideas what I am doing wrong?

Here is the markup :-

<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="bri@hotmail.com"/>
        </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>

Here is the call to SqlDataSource.Insert() . . .

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

Here is the error message :-

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:         }
4

2 回答 2

0

绑定到(插入)参数的字段是什么?当您说如果您保留它有效时DefaultValue,这意味着用于的参数为firstname空或未解析,因此DefaultValue正在使用中的值。

相反,如果您不提供 aDefaultValue 并且参数为空/未定义,则插入将失败(因为 db 列限制不允许空)。

请参阅MSDN 示例

Hth

于 2012-09-20T14:27:42.973 回答
-1

苔藓:这不是连接字符串的问题,如果是您的数据库首先没有连接。它正在连接到数据库,这就是导致错误无法向您的表中插入空值的原因。数据库表中的firstName列允许为 null 。

于 2012-09-20T14:22:24.847 回答