Hi, I'm new to EF and I'm just trying to do a simple insert, but it errors out on SaveChanges().
The method is like this:
Public Sub InsertSickDeparture(ByVal _sd As SickDeparture) Implements ISickDepartureRepository.InsertSickDeparture
Try
context.SickDepartures.Add(_sd)
context.SaveChanges()
Catch ex As Exception
Throw ex
End Try
End Sub
The database fields all have valid values
The stack trace is this:
Stack trace: at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method, Boolean disposeInstance, Object& instance)
at System.Web.UI.WebControls.ObjectDataSourceView.ExecuteInsert(IDictionary values)
at System.Web.UI.DataSourceView.Insert(IDictionary values, DataSourceViewOperationCallback callback)
at System.Web.UI.WebControls.DetailsView.HandleInsert(String commandArg, Boolean causesValidation)
at System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
at System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source, EventArgs e)
at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
at System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object source, EventArgs e)
at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
at System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
When it breaks I can see that the properties that have 'object reference not set to an instance of an object' are (and I don't know what these properties are):
This is the most common exception in .NET... it just mean that you try to call a member of a variable that is not initialized (null). You need to initialize this variable before you can call its members
If I examine the values of SickDeparture then there are many values that are nothing. However, the only values that I can supply values for - i.e. those that I am writing to the database - have proper values attributed to them.
I'm not sure what else I can do to initialize whatever needs to be initialized.
yes I've done that, and all of them are valid, and _sd has all the values it should need to insert into the database.
I also changed my code to this:
Public Sub InsertSickDeparture(ByVal AppNo As Int32, ByVal Startdate As Date, ByVal _type As String) Implements ISickDepartureRepository.InsertSickDeparture
Dim _sd As New SickDeparture()
_sd.AppNo = AppNo
_sd.StartDate = Startdate
_sd.Type = _type
_sd.Author = HttpContext.Current.Request.ServerVariables("LOGON_USER")
_sd.Timestamp = DateTime.Now
Using _context As New StaffContext
Try
_context.SickDepartures.Add(_sd)
_context.SaveChanges()
Catch ex As Exception
Throw ex
End Try
End Using
End Sub
According to your description, it seems that your error is a little strange……Are you sure that nothing is null for updating? including primary key or other relative instances or navigate properties?
ID109
Member
57 Points
54 Posts
EF SaveChanges gives Object reference not set to an instance of an object
Jan 16, 2013 08:18 AM|LINK
Hi, I'm new to EF and I'm just trying to do a simple insert, but it errors out on SaveChanges().
The method is like this:
Public Sub InsertSickDeparture(ByVal _sd As SickDeparture) Implements ISickDepartureRepository.InsertSickDeparture Try context.SickDepartures.Add(_sd) context.SaveChanges() Catch ex As Exception Throw ex End Try End SubThe database fields all have valid values
The stack trace is this:
When it breaks I can see that the properties that have 'object reference not set to an instance of an object' are (and I don't know what these properties are):
CurrentmoduleEventindex
CurrentModuleIndex
CurrentNotificationFlags
IsChangeInRequestHeaders
IsChangeInResponseHeaders
IsChangeInResponseStatus
IsChangeInServerVars
IsChangeInUserPrincipal
IsSendResponseHeaders
Any help would be greatly appreciated
Nick Asiimwe
Participant
1459 Points
643 Posts
Re: EF SaveChanges gives Object reference not set to an instance of an object
Jan 16, 2013 08:36 AM|LINK
Why not set a break point at
and then examine the values in
Sincerely,
Nick
ASP.NET, SQL Server [Database Engine, SSIS & SSRS],Entity Framework, C# and VB.NET
ID109
Member
57 Points
54 Posts
Re: EF SaveChanges gives Object reference not set to an instance of an object
Jan 16, 2013 08:48 AM|LINK
Hi,
thanks for the reply. The only values at the point of SaveChanges that have 'Object reference not set'...etc... are :
MetaDescription
MetaKeywords
Title
which doesn't help me much
Nick Asiimwe
Participant
1459 Points
643 Posts
Re: EF SaveChanges gives Object reference not set to an instance of an object
Jan 16, 2013 09:11 AM|LINK
This is the most common exception in .NET... it just mean that you try to call a member of a variable that is not initialized (null). You need to initialize this variable before you can call its members
This thread might be of help http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net?lq=1
Sincerely,
Nick
ASP.NET, SQL Server [Database Engine, SSIS & SSRS],Entity Framework, C# and VB.NET
ID109
Member
57 Points
54 Posts
Re: EF SaveChanges gives Object reference not set to an instance of an object
Jan 17, 2013 11:20 AM|LINK
If I examine the values of SickDeparture then there are many values that are nothing. However, the only values that I can supply values for - i.e. those that I am writing to the database - have proper values attributed to them.
I'm not sure what else I can do to initialize whatever needs to be initialized.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: EF SaveChanges gives Object reference not set to an instance of an object
Jan 18, 2013 12:18 AM|LINK
Hi,
Please check whether your "context", "SickDepartures" or "_sd" is null or not.
ID109
Member
57 Points
54 Posts
Re: EF SaveChanges gives Object reference not set to an instance of an object
Jan 18, 2013 10:04 AM|LINK
Hi,
yes I've done that, and all of them are valid, and _sd has all the values it should need to insert into the database.
I also changed my code to this:
Public Sub InsertSickDeparture(ByVal AppNo As Int32, ByVal Startdate As Date, ByVal _type As String) Implements ISickDepartureRepository.InsertSickDeparture Dim _sd As New SickDeparture() _sd.AppNo = AppNo _sd.StartDate = Startdate _sd.Type = _type _sd.Author = HttpContext.Current.Request.ServerVariables("LOGON_USER") _sd.Timestamp = DateTime.Now Using _context As New StaffContext Try _context.SickDepartures.Add(_sd) _context.SaveChanges() Catch ex As Exception Throw ex End Try End Using End SubBut still get the same error.
thanks
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: EF SaveChanges gives Object reference not set to an instance of an object
Jan 19, 2013 01:33 AM|LINK
Hi,
At which statement is the error thrown out?
ID109
Member
57 Points
54 Posts
Re: EF SaveChanges gives Object reference not set to an instance of an object
Jan 21, 2013 07:48 AM|LINK
SaveChanges()
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: EF SaveChanges gives Object reference not set to an instance of an object
Jan 21, 2013 08:08 AM|LINK
Hi again ID109,
According to your description, it seems that your error is a little strange……Are you sure that nothing is null for updating? including primary key or other relative instances or navigate properties?