Im stuck....
Old forum URL: forums.lhotka.net/forums/t/7518.aspx
gbk posted on Thursday, August 27, 2009
Hey there
CSLA is very new to me, and im only starting to use it recently. So the problem that i have might be a piece of cake for the rest of you.
Im trying to update but cant seem to get it right. Im using connectionManager and stored procs to manipulate data in tables. I got it right to retrieve the data but when i try to update it using the myAcc.Save() method then it gives me an error that says.
DataPortal.Update failed (System.Data.SqlClient.SqlException: Cannot insert duplicate key row in object 'dbo.Accounts' with unique index 'IX_Accounts_AccountNo'.
The statement has been terminated.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at AccSageProject.Library.Account.DataPortal_Insert() in C:\Users\TEMP\Documents\Celeste\AccSageV1.4.2\WindowsApplication1\Account.vb:line 261
at dm(Object , Object[] )
at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\CSLA\Source\cslacs\Csla\Reflection\MethodCaller.cs:line 332)
Please HELP... or provide any other way to help me update
Thanks in advanceskagen00 replied on Thursday, August 27, 2009
Is it your intention to have AccountNo be unique? (i.e. may be entirely valid to not be unique if accounts are held within different institutions of course).
If they can overlap, you just need to change your index to not be unique.
Is this insert somehow getting run twice when you're saving? (just throw a breakpoint on it). Tom_W replied on Friday, August 28, 2009
Hi gbk
Just to state the very obvious, and I'm sure you've realised all of this, but just in case:
- The error is actually from the database and is being passed back via the CSLA classes.
- The cause is that your code is trying to insert a duplicate AccountNo value into your Accounts table.
The reason for this are likely to be one (or more of) of:
- You have hard coded the AccountNo into your class for testing and so when you create and save the second account a dupe accountno is created
- You are running the insert twice (although this should be difficult as CSLA will take care of working out if the object needs to be inserted or updated - as long as you aren't resetting any properties like IsNew manually)
- You already have that AccountNo in the table (e.g. if you imported the data or are using an existing database)
- The code is inserting instead of updating - put a break point on both the _Insert and _Update data access code in your Account class and make sure that the right one is being called. As above, this shouldn't happen as CSLA will work it out for itself, but it's worth checking.
HTH
Copyright (c) Marimer LLC