2012-08-02 151 views
1

当我尝试插入在我的UPDATE SQL语句下编辑的注释用户时遇到此错误。由于我的UserId是唯一的标识符,我不知道如何解决这个错误。无法将值NULL插入到'UserId'列中,表

错误显示:

Cannot insert the value NULL into column 'UserId', table 

我的.cs代码:

int CommentID = int.Parse(ListView1.DataKeys[e.ItemIndex].Value.ToString()); 
    ListViewItem item = ListView1.Items[e.ItemIndex]; 
    TextBox Title = (TextBox)item.FindControl("Title"); 
    TextBox commentContent = (TextBox)item.FindControl("commentContent"); 

    string connectionString = ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString1"].ConnectionString; 
    using (SqlConnection conn = new SqlConnection(connectionString)) 
    { 
     string sql = "Update Comments set Title = @Title, [email protected] where CommentID = @CommentID"; 
     using (SqlCommand cmd = new SqlCommand(sql, conn)) 
     { 
      cmd.Parameters.AddWithValue("@Title", TextBox1.Text.Trim()); 
      cmd.Parameters.AddWithValue("@commentContent", TextBox2.Text.Trim()); 
      cmd.Parameters.AddWithValue("@CommentID", CommentID); 
      conn.Open(); 
      cmd.ExecuteNonQuery(); 
      conn.Close(); 
     } 
    } 
+5

你确定你发布了正确的代码吗?它显示一条Update语句,并且该消息抱怨插入。桌子上的任何触发器? – 2012-08-02 08:41:06

+1

我怀疑你在评论表上有一个触发器造成这种情况。我猜你在注释表上有一个UserID列允许NULLS(并且至少有一个NULL值),而触发器插入的表在它的UserID列上有一个NOT NULL定义。 – Bridge 2012-08-02 08:43:40

回答

1

我认为在表中的用户ID列有NOT NULL constraint.Check你的表模式。

+1

是的,我的用户ID不为空。因为它是我评论表中的外键。 – kelly 2012-08-02 08:42:22

0

我认为UserID是外键,你必须定义一个外键

评论中包含用户ID作为外键,你必须设置用户ID,因为关键不能为空

1

在我看来, ,UserID具有非空约束。检查你的数据库。

相关问题