我的代码生成并运行,但是当我在运行时引用dbcontext时,出现“ System.NullReferenceException:'对象引用未设置为对象的实例。'”。我在解决方案中的许多其他地方都使用了上下文,并且它可以正常工作,但其他地方是c#。这是VB。
Imports DataServices
Imports Previdence.Domain.Model
Imports Previdence.Business.Model.Report
Namespace UserControls
Partial Class RemissionControl
Inherits UserControl
Private previdenceContext As PrevidenceContext
Private patient As Subject
Private remissionButtonStatus As Boolean?
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Dim snapshotId As Guid = Utility.StringToGuid(Request.QueryString("snapshotId"))
patient = (From su In previdenceContext.Subjects
Join ep In previdenceContext.Episodes On su.SubjectId Equals ep.SubjectId
Join sn In previdenceContext.Snapshots On ep.SubjectId Equals sn.SnapshotId
Where sn.SnapshotId = snapshotId
Select su).FirstOrDefault()
remissionButtonStatus = patient.RemissionButtonOn
If remissionButtonStatus = True Then
remissionRButtonYes.Checked = True
remissionRButtonNo.Checked = False
Else
remissionRButtonYes.Checked = False
remissionRButtonNo.Checked = True
End If
End Sub
'TODO: getting null reference error on the dbcontext
Private Sub remissionRButtonYes_click() Handles remissionRButtonYes.CheckedChanged
If remissionRButtonYes.Checked = True Then
patient.RemissionButtonOn = True
Else patient.RemissionButtonOn = False
End If
previdenceContext.SaveChanges()
End Sub
Private Sub remissionRButtonNo_click() Handles remissionRButtonNo.CheckedChanged
If remissionRButtonNo.Checked = True Then
patient.RemissionButtonOn = False
Else patient.RemissionButtonOn = True
End If
previdenceContext.SaveChanges()
End Sub
End Class
End Namespace
答案 0 :(得分:0)
这应该是IntelliSense捕获的错误类型。
无论如何,如果替换声明,您将避免该错误:
Private previdenceContext As PrevidenceContext
与Private previdenceContext As New PrevidenceContext
,但是您的代码仍然无法使用,因为previdenceContext
没有数据。
您可能缺少一行代码来填充previdenceContext
。
答案 1 :(得分:0)
由于在类的多个成员中使用previdenceContext
,因此可以在类级别上声明它,并在Sub New()
中实例化它。必须处理上下文,因此您的类需要实现IDisposable
并处理previdenceContext
。
Partial Class RemissionControl
Implements IDisposable
Inherits UserControl
Private previdenceContext As PrevidenceContext
Sub New()
' This is the parameterless constructor. If you have a constructor with
' parameter such as passing the connection string name, use it. Do what you
' you do in your C# code.
previdenceContext New PrevidenceContext()
End Sub
' Your class implementation ...
#Region "IDisposable Support"
Private disposedValue As Boolean ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
If Not disposedValue Then
If disposing Then
' TODO: dispose managed state (managed objects).
If previdenceContext IsNot Nothing Then previdenceContext.Dispose()
End If
' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
' TODO: set large fields to null.
End If
disposedValue = True
End Sub
' TODO: override Finalize() only if Dispose(disposing As Boolean) above has code to free unmanaged resources.
'Protected Overrides Sub Finalize()
' ' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above.
' Dispose(False)
' MyBase.Finalize()
'End Sub
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above.
Dispose(True)
' TODO: uncomment the following line if Finalize() is overridden above.
' GC.SuppressFinalize(Me)
End Sub
#End Region
End Class