Click here to Skip to main content
12,248,789 members (37,160 online)
Rate this:
 
Please Sign up or sign in to vote.
When I am trying to add a new object using entity context , error occured.
Please tell me any solution
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 22-Sep-13 23:40pm
mohi2485
Edited 23-Sep-13 0:34am
phil.o77.3K
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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
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.
OriginalGriff 23-Sep-13 7:47am
   
Well, you know it can't be the actual "context" variable, as the previous line would have failed. So, you need to look at all of the parameters of the entity returned by your method, and work out which one is null - the debugger may help you, but without access to your PC, we can't pinpoint the problem. Start with the documentation for the SaveChanges method (if any) and see if it provides any example code - is there an item you need to set before you call it?

Sorry, but it's very much something that only you can do here!
mohi2 24-Sep-13 3:34am
   
I Solve this problem sir.
When m using BigInt for primarykey of table. error occured.
but when m using Int its working fine.
can u tell me why we cant use bigint??
OriginalGriff 24-Sep-13 4:01am
   
:laugh:
Without seeing what's going on for myself? No chance! :)

Glad you sorted it though - do you really think you are going to need a 64bit primary key? :O 2,147,483,648 is a heck of a lot of records - and that fits in a 32bit int!
Er Daljeet Singh 25-Sep-13 7:37am
   
i dont think int / bigint is an issue.
because same case is working fine in my case.
OriginalGriff 25-Sep-13 7:41am
   
Yes, but your Context.SaveChanges method is probably rather different from the OPs - and he can't change that...

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 23 Sep 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100