我是编程新手,遇到了以下错误:
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=App_Web_hnmuvsif
StackTrace:
at Default3.DetailsView1_DataBound(Object sender, EventArgs e) in C:\projects\FPOS_v2\photo_view.aspx.vb:line 54
at System.Web.UI.WebControls.BaseDataBoundControl.OnDataBound(EventArgs e)
at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
at System.Web.UI.WebControls.DetailsView.DataBind()
at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()
at System.Web.UI.WebControls.DetailsView.EnsureDataBound()
at System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls()
at System.Web.UI.Control.EnsureChildControls()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
我使用New创建了一个新对象,但仍然遇到了同样的错误。
以下是代码:
If IsPostBack Then
Session("selectedPhotoID") = DetailsView1.DataKey.Value.ToString()
DataList1.DataBind()
End If
DetailsView1.Fields(0).Visible = False
DetailsView1.Fields(2).ShowHeader = False
DetailsView1.Fields(3).ShowHeader = False
DetailsView1.Fields(3).ItemStyle.HorizontalAlign = HorizontalAlign.Center
DetailsView1.Fields(3).ItemStyle.Font.Bold = True
DetailsView1.HorizontalAlign = HorizontalAlign.Center
'The code below loops through and selects the photo rather then selecting the first
If Not String.IsNullOrEmpty(Session("selectedPhotoID")) Then
Dim dv As New DetailsView
dv = DirectCast(sender, DetailsView)
If dv IsNot Nothing Then
Dim myDataRowView As DataRowView = TryCast(dv.DataItem, DataRowView)
For iRowIndex As Integer = 0 To myDataRowView.DataView.Count - 1
If myDataRowView.DataView(iRowIndex).Row(0).ToString() = Session("selectedPhotoID") Then
'found record
dv.PageIndex = iRowIndex
Exit For
End If
Next
End If
End If
答案 0 :(得分:1)
你需要告诉我们它所引发的代码行,但我猜是
Dim myDataRowView As DataRowView = TryCast(dv.DataItem, DataRowView)
是dv.DataItem一个DataRowView吗?如果没有,它返回Nothing,你没有检查。否则,你做得很好,所以其他地方也不多。
暂且不说:这段代码
Dim dv As New DetailsView
dv = DirectCast(sender, DetailsView)
可能只是
Dim dv As DetailsView = DirectCast(sender, DetailsView)
无需创建新的,然后立即扔掉它。 (不是100%确定我的语法,但不要调用新的)