Click here to Skip to main content
15,881,089 members

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

kellycx asked:

Open original thread
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:
C#
Cannot insert the value NULL into column 'Title', table. 
Column does not allow nulls. UPDATE fails.
The statement has been terminated.


Source Error:
C#
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:
C#
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();
}
Tags: C#, SQL, ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900