Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
When I am trying to add a new object using entity context , error occured.
Please tell me any solution
C#
using ( var context = new rporimc2Entities()) {
   tbl_area_r obj = new tbl_area_r();
   obj.areaFlag = "T";
   obj.createdBy = 1;
   obj.fkPropertyId = 1;
   obj.areaName = "test area";
   context.tbl_area_r.AddObject(obj);
   context.SaveChanges();
}


[edit]Added exception message from comment - phil.o
System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=MySql.Data.Entity
  StackTrace:
       at MySql.Data.Entity.ListFragment.WriteSql(StringBuilder sql)
       at MySql.Data.Entity.SelectStatement.WriteSql(StringBuilder sql)
       at MySql.Data.Entity.InsertStatement.WriteSql(StringBuilder sql)
       at MySql.Data.Entity.SqlFragment.ToString()
       at MySql.Data.Entity.InsertGenerator.GenerateSQL(DbCommandTree tree)
       at MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
       at System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree)
       at System.Data.Common.DbProviderServices.CreateCommand(DbCommandTree commandTree)
       at System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
       at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(UpdateTranslator translator, Dictionary`2 identifierValues)
       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)
       at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
       at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
       at System.Data.Objects.ObjectContext.SaveChanges()
       at exmple.Program.add() in c:\users\lenovo\documents\visual studio 2010\Projects\exmple\exmple\Program.cs:line 28
       at exmple.Program.Main(String[] args) in c:\users\lenovo\documents\visual studio 2010\Projects\exmple\exmple\Program.cs:line 35
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

[/edit]
Posted
Updated 22-Sep-13 23:34pm
v3
Comments
phil.o 23-Sep-13 4:51am    
Which line throws the NullReferenceException?
mohi2 23-Sep-13 5:01am    
context.SaveChanges();
phil.o 23-Sep-13 5:23am    
Could we have the full exception message, including the stack trace of the exception?
mohi2 23-Sep-13 5:29am    
System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=MySql.Data.Entity
StackTrace:
at MySql.Data.Entity.ListFragment.WriteSql(StringBuilder sql)
at MySql.Data.Entity.SelectStatement.WriteSql(StringBuilder sql)
at MySql.Data.Entity.InsertStatement.WriteSql(StringBuilder sql)
at MySql.Data.Entity.SqlFragment.ToString()
at MySql.Data.Entity.InsertGenerator.GenerateSQL(DbCommandTree tree)
at MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
at System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree)
at System.Data.Common.DbProviderServices.CreateCommand(DbCommandTree commandTree)
at System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(UpdateTranslator translator, Dictionary`2 identifierValues)
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)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Objects.ObjectContext.SaveChanges()
at exmple.Program.add() in c:\users\lenovo\documents\visual studio 2010\Projects\exmple\exmple\Program.cs:line 28
at exmple.Program.Main(String[] args) in c:\users\lenovo\documents\visual studio 2010\Projects\exmple\exmple\Program.cs:line 35
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

1 solution

Put a breakpoint on the first line, and follow the code through with single-step. Look at the variables as you go, comparing what you actually have before the code executes with what you expect.

At some point, one of the variables will be null - it is then your problem to work out why.
But with that little info, we can't help much - use the debugger and you should at least be able to work out which one if null, if not immediately why it is null.
 
Share this answer
 
Comments
mohi2 23-Sep-13 5:12am    
I already check it. there is no null value. dont get the cause.
OriginalGriff 23-Sep-13 5:27am    
So which line does it occur on?
I see it's Context.SaveChanges - so step into that method, and repeat the process.
mohi2 23-Sep-13 6:27am    
error in this line => context.SaveChanges();
OriginalGriff 23-Sep-13 6:45am    
So step into the method, and find out which line within that is throwing the exception...
mohi2 23-Sep-13 7:30am    
savechanges is built in method sir.

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