I am working on a homemade forum for an exam and i have just discovered a wierd problem. If i input more than 50 characters into the textarea made for post content, the data is commited to the database but when i try to show the post, my post object fails due to a constraint exception.
anybody know what might be causing this?
the field in the database is of the type text.
Line 139: DataTable daldata = dalPosts.GetDataFirstPostInThreadId(id);
Line 140:
Line 141: Post postObject = new Post(Convert.ToInt32(daldata.Rows[0]["id"]), Convert.ToInt32(daldata.Rows[0]["fk_user_id"]), Convert.ToInt32(daldata.Rows[0]["fk_thread_id"]), daldata.Rows[0]["contents"].ToString(), Convert.ToDateTime(daldata.Rows[0]["开发者_开发知识库submissiondate"]), Convert.ToBoolean(daldata.Rows[0]["isdeleted"]));
Line 142: return postObject;
Line 143:
error details:
System.Data.ConstraintException was unhandled by user code
Message=Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
Source=System.Data
StackTrace:
at System.Data.DataTable.EnableConstraints()
at System.Data.DataTable.set_EnforceConstraints(Boolean value)
at System.Data.DataTable.EndLoadData()
at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
at DALTableAdapters.PostTableAdapter.GetDataFirstPostInThreadId(Int32 threadid) in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\bp2010\62c352df\ee467897\App_Code.rvxbfwcl.5.cs:line 7570
at DataBLL.PostBLL.ObjectFirstInThread(Int32 id) in c:\Users\Stjerneklar\Documents\My Dropbox\SP2010 Ballonparken\BP2010\App_Code\DataBLL\PostBLL.cs:line 139
at DataBLL.Thread..ctor(String name, Int32 id, Int32 fk_category_id) in c:\Users\Stjerneklar\Documents\My Dropbox\SP2010 Ballonparken\BP2010\App_Code\DataBLL\ThreadBLL.cs:line 67
at DataBLL.threadBLL.SelectByCategoryId(Int32 fk_category_id) in c:\Users\Stjerneklar\Documents\My Dropbox\SP2010 Ballonparken\BP2010\App_Code\DataBLL\ThreadBLL.cs:line 176
at ForumThreads.createThread(Object sender, EventArgs e) in c:\Users\Stjerneklar\Documents\My Dropbox\SP2010 Ballonparken\BP2010\ForumThreads.aspx.cs:line 49
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
I have no other content in the related tables that might affect things, and if i shorten the text everything works fine.
Edit: Figured it out in the end(the night before handing in the assignment) the offending field was defined as having a maxlenght of 50 chars in my dataset.
As far as I can see, is possible that you created the TableAdapter once and then change the schema of the table in the db, allowing more characters in the column. If this is the case, go to the table adapter, select the column (in the visual editor) and edit the properties (MaxLength).
You are using TableAdapters. TableAdapters are a fast way to create data access but are very hard to mantain... I strongly recommend to stop using them and switch to plain ADO.NET core (DataAdapters, DataReaders, etc).
Hope it helps!
I don't know what's causing it, but when you get a ConstraintException you should always look at the errors in your DataTable for more specific guidance about what went wrong.
And once you've found the rows with errors you can examine the RowError property on each one to see exactly what the problem was.
精彩评论