无法将值 NULL 插入到列“UserId"中 [英] Cannot insert the value NULL into column 'UserId'

查看:31
本文介绍了无法将值 NULL 插入到列“UserId"中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此异常,但无法弄清楚原因:无法将值 NULL 插入到列 'UserId'

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

这是我的代码:

<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();
}

推荐答案

我只是获取了用户 ID 并执行了更新命令.

I just grabbed the userid and performed an update command.

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

UserIdLabel.Text = userGuid.ToString();

这篇关于无法将值 NULL 插入到列“UserId"中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆