Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys,

I couldn't find the solution and I am getting crazy about it.

Pls I really need help.
HTML
The Error : hata
An error occurred while updating the entries. See the inner exception for details.

C#
public void Ky_DepoInbound()
     {
         int id = 0;
         try
         {

             MEKMAR_DBEntities mekmar = new MEKMAR_DBEntities();
             Mekmar_Server_Data.DataDataContext server = new Mekmar_Server_Data.DataDataContext();

             foreach (var st in mekmar.DEPO_INBOUNT)
             {
                 DEPO_INBOUNT inb = (from m in mekmar.DEPO_INBOUNT where m.DP_IN_ID == st.DP_IN_ID select m).Single();

                 //tablonun silinmesi
                 mekmar.DeleteObject(inb);
             }
             mekmar.SaveChanges();


             foreach (var st in server.DEPO_INBOUNTs.ToList())
             {
                 DEPO_INBOUNT dpinb = new DEPO_INBOUNT();
                 dpinb.DP_IN_ID =st.DP_IN_ID;
                 id = dpinb.DP_IN_ID;
                 if (st.URUN_ID.HasValue) dpinb.URUN_ID = st.URUN_ID.Value;
                 else dpinb.URUN_ID = st.URUN_ID;
                 if (st.BIRIM_FIYAT.HasValue) dpinb.BIRIM_FIYAT = st.BIRIM_FIYAT.Value;
                 else dpinb.BIRIM_FIYAT = 0;
                 if (st.KUTU_ADET.HasValue) dpinb.KUTU_ADET = st.KUTU_ADET.Value;
                 else dpinb.KUTU_ADET = 0;
                 dpinb.DP_GN_ID = st.DP_GN_ID;
                 if (st.KUTU_SQF.HasValue) dpinb.KUTU_SQF = st.KUTU_SQF.Value;
                 else dpinb.KUTU_SQF = 0;
                 if (st.RF_ID.HasValue) dpinb.RF_ID = st.RF_ID.Value;
                 else dpinb.RF_ID = 0;
                 if (st.MUSTERI_ID.HasValue) dpinb.MUSTERI_ID = st.MUSTERI_ID.Value;
                 else dpinb.MUSTERI_ID = 0;
                 if (st.DP_BL_ID.HasValue) dpinb.DP_BL_ID = st.DP_BL_ID.Value;
                 else dpinb.DP_BL_ID = 0;
                 if (st.KASA_ADET.HasValue) dpinb.KASA_ADET = st.KASA_ADET.Value;
                 else dpinb.KASA_ADET = 0;
                 if (st.DEVIR_ID.HasValue) dpinb.DEVIR = st.DEVIR.Value;
                 else dpinb.DEVIR_ID = 0;
                 if (st.DEVIR_ID.HasValue) dpinb.DEVIR_ID = st.DEVIR_ID.Value;
                 else dpinb.DEVIR_ID = 0;
                 //tablonun eklenmesi
                 mekmar.AddToDEPO_INBOUNT(dpinb);

             }
             mekmar.SaveChanges();
         }

         catch (Exception hata)
         {                                          System.Windows.Forms.MessageBox.Show(hata.ToString()+"\n"+id.ToString());
         }

     }

Awaiting solutions..

Regards

Haluk
Posted
Updated 3-Oct-12 2:38am
v2
Comments
Zoltán Zörgő 3-Oct-12 8:43am    
See the inner exception for details. Could we see the inner exception?
n.podbielski 3-Oct-12 8:57am    
Can I ask you where are you from?
Zoltán Zörgő 3-Oct-12 9:11am    
See my profile. But I can't see what is the importance of this.
n.podbielski 3-Oct-12 9:32am    
Heh. You are right it's not important. But i don't see any possibility for private message on CP.
Zoltán Zörgő 3-Oct-12 9:39am    
That's right, and might be a missing feature. But this is not the goal of this site.
But you have not posted the inner exception text of your exception.

1 solution

If I understand your code correctly...
in the 2nd foreach you are creating a copy of each element and add that copy to your entities set. The problem is that you are also duplicating what must be your primary key (Id i suppose). When you save changes this is conflictual... You can't have 2 objects with the same key in your set.
 
Share this answer
 
Comments
haluk_78 3-Oct-12 9:42am    
System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'DP_IN_ID', table 'MEKMAR_DB.dbo.DEPO_INBOUNT'; column does not allow nulls. INSERT fails.
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()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
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.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
--- End of inner exception stack trace ---
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Objects.ObjectContext.SaveChanges()
at Mekmar_Yedek_Alma.Islemler.Ky_DepoInbound() in C:\Users\ferdi\Documents\Visual Studio 2010\Projects\Mekmar_Yedek_Alma\Mekmar_Yedek_Alma\Islemler.cs:line 88
Guirec 3-Oct-12 9:49am    
I guess you understand the issue the same way as we are when reading the exception...
haluk_78 3-Oct-12 9:52am    
I got you but there is not any problem with this one(same way):

private void Ky_Yuzey()
{
MEKMAR_DBEntities mekmar = new MEKMAR_DBEntities();
Mekmar_Server_Data.DataDataContext server = new Mekmar_Server_Data.DataDataContext();

foreach (var st in mekmar.YUZEYKENARISLEM.ToList())
{
YUZEYKENARISLEM yz = (from m in mekmar.YUZEYKENARISLEM where m.YUZEY_KENAR_ISLEM_ID == st.YUZEY_KENAR_ISLEM_ID select m).Single();
mekmar.DeleteObject(yz);


}
mekmar.SaveChanges();

foreach (var st in server.YUZEYKENARISLEMs.ToList())
{
YUZEYKENARISLEM yuzey = new YUZEYKENARISLEM();
yuzey.YUZEY_KENAR_ISLEM_ID = st.YUZEY_KENAR_ISLEM_ID;
yuzey.ISLEM_ADI = st.ISLEM_ADI;
mekmar.AddToYUZEYKENARISLEM(yuzey);
}

mekmar.SaveChanges();
}
Guirec 3-Oct-12 10:01am    
But what are you trying to achieve?
You retrieve a list of entities from db
You enumerate the list a first time. In that enumeration you (in a weird way) delete the object from the entities set.
Then you save : at that point your table should be empty.
You enumerate the list a second time. In this new enumeration you create a copy of the object to add it again to your table.

In the end your method is just doing nothing but consuming system resource.... I mean I don't see the point in removing all records of a table and recreate them at identical.
haluk_78 4-Oct-12 3:08am    
We use 2 Databases. DataContent is an another data source.
MEKMAR_DBEntities is a local database.
Mekmar_Server_Data is a database in the server.
So, I get the data from server and then copy to the local. This process is avoid to repeat data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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