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