无法将值NULL插入列'Title',表中 [英] Cannot insert the value NULL into column 'Title', table

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

问题描述

我有一个ListView,并且正在尝试编辑用户发表的评论,之后,它将在我的数据库中更新。但是,当我在listView中选择更新按钮时遇到此错误。我哪里出错了?



错误信息:

无法插入 value  NULL  into ' 标题',表格。 
列不允许空值。更新失败。
该声明已被终止。





来源错误:

在执行当前Web请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息





我的.cs代码:

 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;

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

解决方案

你的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数据库中的空值

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


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

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