The INSERT statement conflicted with the FOREIGN KEY constraint exception, but data stored
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_JobList_aspnet_Membership". The conflict occurred in database "C:\JOBPOST\APP_DATA\ASPNETDB.MDF", table "dbo.aspnet_Membership", column 'UserId'.The statement has been terminated.
It always throws this exception, but the data has been successfully stored in the database.I test the situation many times with diffirent method.Right now I only have 2 UserId in aspnet_membership, Use that uniqueidentifier UsrId column as the FK_JobList_aspnet_Membership. The primary Key table is aspnet_membership, the foreign key table is my JobList, which itself has index JobId primary key
I can't figure out where i am wrong. 2 UsrId only from aspnet.mdf, just add 1 joblist table,why it always throw exception, but successfully store the data.
The code:
JobPostDataContext db = new JobPostDataContext();
JobList newJob = new JobList();
MembershipUser curUser = Membership.GetUser(User.Identity.Name);
Guid user = new Guid(curUser.ProviderUserKey.ToString());
newJob.UserId =user;
newJob.JobTitle = ((TextBox)DetailsView1.FindControl("TB_JobTitle")).Text;
db.JobLists.InsertOnSubmit(newJob);
db.SubmitChanges();
Exception Details: System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_JobList_aspnet_Membership". The conflict occurred in database "C:\JOBPOST\APP_DATA\ASPNETDB.MDF", table "dbo.aspnet_Membership", column 'UserId'.
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.
Stack Trace:
[SqlException (0x80131904): The INSERT statement conflicted with the FOREIGN KEY constraint "FK_JobList_aspnet_Membership". The conflict occurred in database "C:\DOCUMENTS AND SETTINGS\QI\DESKTOP\CS526\JOBPOST\APP_DATA\ASPNETDB.MDF", table "dbo.aspnet_Membership", column 'UserId'.
The statement has been terminated.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +2030802
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5009584
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2275
System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +33
System.Data.SqlClient.SqlDataReader.get_MetaData() +86
Sy开发者_StackOverflowstem.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +311
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +987
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12
System.Data.Common.DbCommand.ExecuteReader() +12
System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +1266
System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +113
System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +344
System.Data.Linq.StandardChangeDirector.DynamicInsert(TrackedObject item) +151
System.Data.Linq.StandardChangeDirector.Insert(TrackedObject item) +235
System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) +337
System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) +378
System.Data.Linq.DataContext.SubmitChanges() +23
System.Web.UI.WebControls.LinqToSqlWrapper.SubmitChanges(DataContext dataContext) +9
System.Web.UI.WebControls.LinqDataSourceView.InsertDataObject(Object dataContext, Object table, Object newDataObject) +89
System.Web.UI.WebControls.LinqDataSourceView.InsertObject(Object newEntity) +204
System.Web.UI.WebControls.QueryableDataSourceView.ExecuteInsert(IDictionary values) +105
System.Web.UI.WebControls.ContextDataSourceView.ExecuteInsert(IDictionary values) +94
System.Web.UI.WebControls.LinqDataSourceView.ExecuteInsert(IDictionary values) +29
System.Web.UI.DataSourceView.Insert(IDictionary values, DataSourceViewOperationCallback callback) +89
System.Web.UI.WebControls.DetailsView.HandleInsert(String commandArg, Boolean causesValidation) +379
System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +574
System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source, EventArgs e) +95
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object source, EventArgs e) +112
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +125
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +169
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +9
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
The very first thing I would do is put a debugger on the code that submits changes and then see if it's called twice.
Sounds like it might be executed once successfully, then a second time with a failure.
精彩评论