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