我目前正在为一个学校项目编写一个网页。这个网站应该是一个简单的在线商店,人们可以在这里订购艺术品的印刷品。我正在处理的特定页面有一个下拉列表 (ddlArt),它绑定到我的数据库并显示可用的不同艺术作品的列表。当用户选择其中一个项目时,该项目的所有信息将从数据库中提取并以各种标签等显示在页面上。唯一的事情是,当我尝试运行该页面时,我收到一个空引用异常错误,提示“对象引用未设置为对象的实例”。今年早些时候我在家庭作业中遇到了同样的错误,并设法解决了它,但我不记得我做了什么,直到下周我才能从学校得到帮助,所以我想我' d在这里试试我的运气。这是我的代码:
Private selectedArt As Art
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
ddlArt.DataBind()
End If
selectedArt = Me.GetSelectedArt
lblArtID.Text = selectedArt.ArtID()
lblArtName.Text = selectedArt.ArtName()
lblCaption.Text = selectedArt.Caption()
lblDescription.Text = selectedArt.Description()
imgArt.ImageUrl = "~/images/" & selectedArt.FileName()
End Sub
Private Function GetSelectedArt() As Art
Dim artTable As DataView = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
artTable.RowFilter = "ArtID = '" & ddlArt.SelectedValue & "'"
Dim artRow As DataRowView = artTable(0)
Me.imgArt.ImageUrl = "~/images/" & artRow("FileName")
Dim art As New Art
art.ArtID = artRow("ArtID").ToString
art.ArtName = artRow("ArtName").ToString
art.Caption = artRow("Caption").ToString
art.Description = artRow("LongDescription").ToString
art.FileName = artRow("FileName").ToString
Return art
End Function
这是 Art 类的代码,以防有人感兴趣:
Public Class Art
Public Property ArtID As Integer
Public Property ArtName As String
Public Property ArtType As String
Public Property Caption As String
Public Property FileName As String
Public Property Description As String
End Class
当我收到错误时,它会突出显示artTable.RowFilter = "ArtID = '" & ddlArt.SelectedValue & "'"
GetSelectedArt 函数中的行。我尝试将它与我提到的更正的家庭作业进行比较,但我似乎找不到问题所在。我的VB有点模糊,因为我已经有一段时间没有真正上课了。有什么建议么?非常感谢!