且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法将值NULL插入列'Title',表中

更新时间:2023-11-30 20:09:58

你的textbOx1里面没有任何文字。它的空白。



确保它是必填字段。另外,在表中,将column属性设置为allow null。


无法将值NULL插入列'Title',表中。 
列不允许空值。 UPDATE失败。



这是自我解释的。您试图在设置为不允许空值的列中插入NULL。



设置默认值或设置列以允许NULL值。


我认为您已获得所需的所有信息。您应该单步执行调试器,例如参见本文: 10+强大使用Visual Studio调试技巧 [ ^ ]或此 http: //msdn.microsoft.com/en-us/library/aa290350%28v=VS.71%29.aspx [ ^ ]



每个人都是已经说过:

TextBox1是空的,因为你很可能会发现使用调试器。

你要么改变你的代码以包含一个检查,或者

允许SQL数据库中的空值​​lockquote>

I have a ListView and was trying to edit the comments made by users and after which, it will be updated in my database. However, i encounter this error when i select the update button in my listView. Where have i gone wrong?

Error Message:

Cannot insert the value NULL into column 'Title', table. 
Column does not allow nulls. UPDATE fails.
The statement has been terminated.



Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.



My .cs code:

MembershipUser currentUser = Membership.GetUser();
        Guid currentUserId = (Guid)currentUser.ProviderUserKey;
        int i = e.ItemIndex;
        TextBox tb = ListView1.Items[i].FindControl("commentContentTextBox") as TextBox;
        string test = tb.Text;
        int CommentID = Convert.ToInt16(ListView1.DataKeys[i].Value);

        string connectionString = ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(connectionString);

        string updateSQL = "UPDATE GenBook set Title=@Title, comment=@comment, package=@package, UserId=@UserId WHERE GenBookID = @GenBookID ";

        using (SqlConnection myConnection = new SqlConnection(connectionString))
        {
            myConnection.Open();

            SqlCommand myCommand = new SqlCommand(updateSQL, myConnection);
            myCommand.Parameters.AddWithValue("@GenBookID", GenBookID);
            myCommand.Parameters.AddWithValue("@Title", TextBox1.Text.Trim());
            myCommand.Parameters.AddWithValue("@comment", TextBox2.Text.Trim());
            myCommand.Parameters.AddWithValue("@UserId", currentUserId);
            myCommand.Parameters.AddWithValue("@package", package.Text.Trim());

            myCommand.ExecuteNonQuery();
            myConnection.Close();
            ListView1.EditIndex = -1;
            ListView1.DataBind();
}

Your textbOx1 dont have any text inside it. Its blank.

Make sure it is a required field. Else in table, set the column property to allow null.


Cannot insert the value NULL into column 'Title', table.
Column does not allow nulls. UPDATE fails.


It's self explanatory. You are trying to insert a NULL in a column that is set not to allow null values.

Either put a default value or set column to allow NULL value.


I think you have gotten all the information you need. You should step through the debugger, see for instance this article: 10+ powerful debugging tricks with Visual Studio[^] or this one http://msdn.microsoft.com/en-us/library/aa290350%28v=VS.71%29.aspx[^]

Either as everybody has said:
TextBox1 is empty, as you probarbly would find out using the debugger.
And you have either goit to change your code to include a check for this, or
Allow nulls in the SQL database