Search code examples
c#sql-insertsqldatasource

Cannot insert the value NULL into column 'UserId'


I am getting this exception and can not figure out why: Cannot insert the value NULL into column 'UserId'

This is my code:

<asp:TextBox ID="FirstNameBox" runat="server" />
<br />
<br />
<asp:TextBox ID="LastNameBox" runat="server" />
<asp:SqlDataSource
    ID="InsertText"
    runat="server"
    ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"
    InsertCommand="INSERT INTO UserForm (UserId,FirstName,LastName) VALUES  (@UserId,@FiName,@LaName)">

   <InsertParameters>
      <asp:ControlParameter Name="FiName" ControlID="FirstNameBox" PropertyName="Text" />
      <asp:ControlParameter Name="LaName" ControlID="LastNameBox" PropertyName="Text" />
      <asp:Parameter Name="UserId" />
   </InsertParameters>
 </asp:SqlDataSource>

This is what I have in my code behind file:

public void button1_Click(object sender, System.EventArgs e)
{
   InsertText.Insert();
}

Solution

  • I just grabbed the userid and performed an update command.

    Guid userGuid = (Guid)Membership.GetUser(User.Identity.Name).ProviderUserKey;
    
    UserIdLabel.Text = userGuid.ToString();