0

I'm currently working on coding a web page for a school project. This site is supposed to be a simple online store where people can order prints of artwork. The specific page I'm working on has a Drop Down List (ddlArt) that is bound to my database and displays a list of the different art pieces available. When the user selects one of the items, all the information on that item is pulled from the database and displayed on the page in a variety of labels and such. The only thing is that I'm getting a null reference exception error saying "Object reference not set to an instance of an object" when I go to try to run the page. I got the same error on a homework assignment earlier in the year and managed to get it fixed, but I can't remember what I did and I can't get help from school until next week, so I thought I'd try my luck on here. Here's my code:

    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

And here's the code for the Art class, in case anybody is interested:

    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

When I get the error, it highlights the artTable.RowFilter = "ArtID = '" & ddlArt.SelectedValue & "'" line in the GetSelectedArt function. I've tried comparing it to my corrected homework assignment that I mentioned, but I can't seem to find the problem. My VB is a little fuzzy because it's been awhile since I actually took the class. Any suggestions? Thanks a bunch!

7
  • 1
    Have you tried hovering over the variables in the statement while paused in the debugger to find out which one is Nothing? That's a good place to start. Commented May 11, 2013 at 0:46
  • Okay I just tried this and, out of all the code highlighted by the error, artTable is currently Nothing. Commented May 11, 2013 at 0:50
  • is ArtID numeric field ? Commented May 11, 2013 at 0:57
  • Yes, ArtID has a column type of integer in the database Commented May 11, 2013 at 1:02
  • Where is DataSource for ddlArt.DataBind()? when Page Loads is there any selected value in dropdown? Commented May 11, 2013 at 2:07

2 Answers 2

1

If I understand your comment above correctly, on the initial page load there is nothing in the ddlArt, because the user must first choose an art type.

If that is correct, then your answer to my question is your answer.

For whatever reason (and without seeing at least the Select statement), artTbl is not getting instantiated, which is why you're seeing the Object reference not set to an instance of an object error.

One way to fix this (without knowledge of your SqlDataSource it's hard to give a precise answer) is to modify your Page Load method so that GetSelectedArt is only called when the user has selected an item from the drop down list. Right now GetSelectedArt is called every time the page loads.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)     Handles Me.Load

    If Not IsPostBack Then
        ddlArt.DataBind()
    Else
        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 If
End Sub

However, the above modification will only prevent GetSelectedArt from being called on the initial page load. If your SqlDataSource.Select command is still returning nothing, then you're still going to have this problem.

A better solution would be to call the GetSelectedArt on the ddlArt.SelectedIndexChanged event handler. This way you'll know that you have (or should have) a valid SelectedValue from ddlArt.

Also, if you don't populate the drop down until the user selects an art type from the radio button list, why are you binding the drop down list on the initial page load (and what are you binding it to)? Or is the drop down list and detail information on a different page from the radio button list?

Sign up to request clarification or add additional context in comments.

1 Comment

Aha! Putting everything under the ddlArt.SelectedIndexChanged event handler seemed to do the trick. Thank you for your time!
0

May be .. with ArtID as integer

artTable.RowFilter = "ArtID = " & format(ddlArt.SelectedValue) 

2 Comments

Hmm, just tried that and I'm still getting the same error. I tried a CInt type conversion and also tried the ToString() method and neither of them worked either.
Re: your edit... that's not it either. I know we're close - I remember playing around with a nearly-identical line when I got the error before, but I just can't remember what I did...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.